OpenNI 1.5.7
XnStringsHashT.h
Go to the documentation of this file.
1 /*****************************************************************************
2 * *
3 * OpenNI 1.x Alpha *
4 * Copyright (C) 2012 PrimeSense Ltd. *
5 * *
6 * This file is part of OpenNI. *
7 * *
8 * Licensed under the Apache License, Version 2.0 (the "License"); *
9 * you may not use this file except in compliance with the License. *
10 * You may obtain a copy of the License at *
11 * *
12 * http://www.apache.org/licenses/LICENSE-2.0 *
13 * *
14 * Unless required by applicable law or agreed to in writing, software *
15 * distributed under the License is distributed on an "AS IS" BASIS, *
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
17 * See the License for the specific language governing permissions and *
18 * limitations under the License. *
19 * *
20 *****************************************************************************/
21 #ifndef _XN_STRINGS_HASH_T_H_
22 #define _XN_STRINGS_HASH_T_H_
23 
24 //---------------------------------------------------------------------------
25 // Includes
26 //---------------------------------------------------------------------------
27 #include "XnHashT.h"
28 
29 //---------------------------------------------------------------------------
30 // Code
31 //---------------------------------------------------------------------------
33 {
34 public:
35  static XnHashCode Hash(const XnChar* const& key)
36  {
37  XnUInt32 nCRC = 0;
38  xnOSStrCRC32(key, &nCRC);
39 
40  // convert from UINT32 to XnHashValue
41  return nCRC % (1 << (sizeof(XnHashCode)*8));
42  }
43 
44  static XnInt32 Compare(const XnChar* const& key1, const XnChar* const& key2)
45  {
46  return strcmp(key1, key2);
47  }
48 };
49 
50 template<class TValue>
52 {
53 public:
56 
57  static TLinkedNode* Allocate(TPair const& pair)
58  {
59  XnChar* pKeyCopy = xnOSStrDup(pair.Key());
60  if (pKeyCopy == NULL)
61  {
62  return NULL;
63  }
64 
65  return XN_NEW(TLinkedNode, TPair(pKeyCopy, pair.Value()));
66  }
67 
68  static void Deallocate(TLinkedNode* pNode)
69  {
70  XN_ASSERT(pNode != NULL);
71  XN_ASSERT(pNode->value.Key() != NULL);
72 
73  xnOSFree(pNode->value.Key());
74  XN_DELETE(pNode);
75  }
76 };
77 
78 template<class TValue>
79 class XnStringsHashT : public XnHashT<const XnChar*, TValue, XnStringsHashKeyManager, XnStringsNodeAllocator<TValue> >
80 {
82 
83 public:
84  XnStringsHashT() : Base() {}
85 
86  XnStringsHashT(const XnStringsHashT& other) : Base()
87  {
88  *this = other;
89  }
90 
92  {
93  Base::operator=(other);
94  // no other members
95  return *this;
96  }
97 };
98 
99 class XnStringsSet : public XnStringsHashT<void*>
100 {
101  typedef XnStringsHashT<void*> Base;
102 
103 public:
104  XnStatus Set(const XnChar* key)
105  {
106  return Base::Set(key, NULL);
107  }
108 };
109 
110 #endif // _XN_STRINGS_HASH_T_H_
XnStatus Set(const const XnChar *&key, const void *&value)
Definition: XnHashT.h:382
static XnInt32 Compare(const XnChar *const &key1, const XnChar *const &key2)
Definition: XnStringsHashT.h:44
static TLinkedNode * Allocate(TPair const &pair)
Definition: XnStringsHashT.h:57
Definition: XnHashT.h:39
Definition: XnStringsHashT.h:51
XnStatus Set(const XnChar *key)
Definition: XnStringsHashT.h:104
XnUInt32 XnStatus
Definition: XnStatus.h:33
XnLinkedNodeT< TPair > TLinkedNode
Definition: XnStringsHashT.h:55
TValue const & Value() const
Definition: XnHashT.h:50
XN_C_API void XN_C_DECL xnOSFree(const void *pMemBlock)
XnStringsHashT()
Definition: XnStringsHashT.h:84
XnStringsHashT(const XnStringsHashT &other)
Definition: XnStringsHashT.h:86
#define XN_NEW(type,...)
Definition: XnOS.h:325
Definition: XnHashT.h:77
XN_C_API XnChar *XN_C_DECL xnOSStrDup(const XnChar *strSource)
XnStringsHashT & operator=(const XnStringsHashT &other)
Definition: XnStringsHashT.h:91
XnUInt8 XnHashCode
Definition: XnHashT.h:33
T value
Definition: XnListT.h:48
Definition: XnStringsHashT.h:32
TKey const & Key() const
Definition: XnHashT.h:49
#define XN_DELETE(p)
Definition: XnOS.h:335
Definition: XnStringsHashT.h:79
static XnHashCode Hash(const XnChar *const &key)
Definition: XnStringsHashT.h:35
XnKeyValuePair< const XnChar *, TValue > TPair
Definition: XnStringsHashT.h:54
static void Deallocate(TLinkedNode *pNode)
Definition: XnStringsHashT.h:68
Definition: XnStringsHashT.h:99
XN_C_API XnStatus XN_C_DECL xnOSStrCRC32(const XnChar *cpString, XnUInt32 *nCRC32)