OpenNI 1.5.7
XnStringsHashT.h
Go to the documentation of this file.
00001 /*****************************************************************************
00002 *                                                                            *
00003 *  OpenNI 1.x Alpha                                                          *
00004 *  Copyright (C) 2012 PrimeSense Ltd.                                        *
00005 *                                                                            *
00006 *  This file is part of OpenNI.                                              *
00007 *                                                                            *
00008 *  Licensed under the Apache License, Version 2.0 (the "License");           *
00009 *  you may not use this file except in compliance with the License.          *
00010 *  You may obtain a copy of the License at                                   *
00011 *                                                                            *
00012 *      http://www.apache.org/licenses/LICENSE-2.0                            *
00013 *                                                                            *
00014 *  Unless required by applicable law or agreed to in writing, software       *
00015 *  distributed under the License is distributed on an "AS IS" BASIS,         *
00016 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
00017 *  See the License for the specific language governing permissions and       *
00018 *  limitations under the License.                                            *
00019 *                                                                            *
00020 *****************************************************************************/
00021 #ifndef _XN_STRINGS_HASH_T_H_
00022 #define _XN_STRINGS_HASH_T_H_ 
00023 
00024 //---------------------------------------------------------------------------
00025 // Includes
00026 //---------------------------------------------------------------------------
00027 #include "XnHashT.h"
00028 
00029 //---------------------------------------------------------------------------
00030 // Code
00031 //---------------------------------------------------------------------------
00032 class XnStringsHashKeyManager
00033 {
00034 public:
00035     static XnHashCode Hash(const XnChar* const& key)
00036     {
00037         XnUInt32 nCRC = 0;
00038         xnOSStrCRC32(key, &nCRC);
00039 
00040         // convert from UINT32 to XnHashValue
00041         return nCRC % (1 << (sizeof(XnHashCode)*8));
00042     }
00043 
00044     static XnInt32 Compare(const XnChar* const& key1, const XnChar* const& key2)
00045     {
00046         return strcmp(key1, key2);
00047     }
00048 };
00049 
00050 template<class TValue>
00051 class XnStringsNodeAllocator
00052 {
00053 public:
00054     typedef XnKeyValuePair<const XnChar*, TValue> TPair;
00055     typedef XnLinkedNodeT<TPair> TLinkedNode;
00056 
00057     static TLinkedNode* Allocate(TPair const& pair)
00058     {
00059         XnChar* pKeyCopy = xnOSStrDup(pair.Key());
00060         if (pKeyCopy == NULL)
00061         {
00062             return NULL;
00063         }
00064 
00065         return XN_NEW(TLinkedNode, TPair(pKeyCopy, pair.Value()));
00066     }
00067 
00068     static void Deallocate(TLinkedNode* pNode)
00069     {
00070         XN_ASSERT(pNode != NULL);
00071         XN_ASSERT(pNode->value.Key() != NULL);
00072 
00073         xnOSFree(pNode->value.Key());
00074         XN_DELETE(pNode);
00075     }
00076 };
00077 
00078 template<class TValue>
00079 class XnStringsHashT : public XnHashT<const XnChar*, TValue, XnStringsHashKeyManager, XnStringsNodeAllocator<TValue> >
00080 {
00081     typedef XnHashT<const XnChar*, TValue, XnStringsHashKeyManager, XnStringsNodeAllocator<TValue> > Base;
00082 
00083 public:
00084     XnStringsHashT() : Base() {}
00085 
00086     XnStringsHashT(const XnStringsHashT& other) : Base()
00087     {
00088         *this = other;
00089     }
00090 
00091     XnStringsHashT& operator=(const XnStringsHashT& other)
00092     {
00093         Base::operator=(other);
00094         // no other members
00095         return *this;
00096     }
00097 };
00098 
00099 class XnStringsSet : public XnStringsHashT<void*>
00100 {
00101     typedef XnStringsHashT<void*> Base;
00102 
00103 public:
00104     XnStatus Set(const XnChar* key)
00105     {
00106         return Base::Set(key, NULL);
00107     }
00108 };
00109 
00110 #endif // _XN_STRINGS_HASH_T_H_