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:
85 
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_
XnHashT
Definition: XnHashT.h:76
xnOSStrDup
XN_C_API XnChar *XN_C_DECL xnOSStrDup(const XnChar *strSource)
XnStringsHashT::operator=
XnStringsHashT & operator=(const XnStringsHashT &other)
Definition: XnStringsHashT.h:91
XnStringsHashKeyManager::Hash
static XnHashCode Hash(const XnChar *const &key)
Definition: XnStringsHashT.h:53
XnStringsHashKeyManager::Compare
static XnInt32 Compare(const XnChar *const &key1, const XnChar *const &key2)
Definition: XnStringsHashT.h:62
XnStatus
XnUInt32 XnStatus
Definition: XnStatus.h:32
XnKeyValuePair::Value
TValue const & Value() const
Definition: XnHashT.h:49
xnOSFree
XN_C_API void XN_C_DECL xnOSFree(const void *pMemBlock)
XnKeyValuePair
Definition: XnHashT.h:38
XnStringsHashT::XnStringsHashT
XnStringsHashT(const XnStringsHashT &other)
Definition: XnStringsHashT.h:86
XnLinkedNodeT< TPair >
XnHashT< const XnChar *, void *, XnStringsHashKeyManager, XnStringsNodeAllocator< void * > >::Set
XnStatus Set(const const XnChar * &key, const void * &value)
Definition: XnHashT.h:381
XN_NEW
#define XN_NEW(type,...)
Definition: XnOS.h:328
XnStringsHashT
Definition: XnStringsHashT.h:79
XnLinkedNodeT::value
T value
Definition: XnListT.h:65
XnStringsNodeAllocator::Allocate
static TLinkedNode * Allocate(TPair const &pair)
Definition: XnStringsHashT.h:57
XnHashT< const XnChar *, TValue, XnStringsHashKeyManager, XnStringsNodeAllocator< TValue > >::operator=
XnHashT & operator=(const XnHashT &other)
Definition: XnHashT.h:316
XnStringsNodeAllocator::Deallocate
static void Deallocate(TLinkedNode *pNode)
Definition: XnStringsHashT.h:68
XnStringsHashT::XnStringsHashT
XnStringsHashT()
Definition: XnStringsHashT.h:84
XnStringsNodeAllocator::TLinkedNode
XnLinkedNodeT< TPair > TLinkedNode
Definition: XnStringsHashT.h:55
XnHashT.h
xnOSStrCRC32
XN_C_API XnStatus XN_C_DECL xnOSStrCRC32(const XnChar *cpString, XnUInt32 *nCRC32)
XnStringsSet::Set
XnStatus Set(const XnChar *key)
Definition: XnStringsHashT.h:104
XnStringsNodeAllocator::TPair
XnKeyValuePair< const XnChar *, TValue > TPair
Definition: XnStringsHashT.h:54
XnHashCode
XnUInt8 XnHashCode
Definition: XnHashT.h:32
XnStringsSet
Definition: XnStringsHashT.h:99
XnStringsHashKeyManager
Definition: XnStringsHashT.h:32
XN_DELETE
#define XN_DELETE(p)
Definition: XnOS.h:338
XnKeyValuePair::Key
TKey const & Key() const
Definition: XnHashT.h:48
XnStringsNodeAllocator
Definition: XnStringsHashT.h:51