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{
34public:
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
50template<class TValue>
52{
53public:
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
78template<class TValue>
79class XnStringsHashT : public XnHashT<const XnChar*, TValue, XnStringsHashKeyManager, XnStringsNodeAllocator<TValue> >
80{
82
83public:
85
87 {
88 *this = other;
89 }
90
92 {
93 Base::operator=(other);
94 // no other members
95 return *this;
96 }
97};
98
99class XnStringsSet : public XnStringsHashT<void*>
100{
102
103public:
104 XnStatus Set(const XnChar* key)
105 {
106 return Base::Set(key, NULL);
107 }
108};
109
110#endif // _XN_STRINGS_HASH_T_H_
XnUInt8 XnHashCode
Definition: XnHashT.h:33
#define XN_DELETE(p)
Definition: XnOS.h:339
#define XN_NEW(type,...)
Definition: XnOS.h:329
XN_C_API void XN_C_DECL xnOSFree(const void *pMemBlock)
XN_C_API XnChar *XN_C_DECL xnOSStrDup(const XnChar *strSource)
XN_C_API XnStatus XN_C_DECL xnOSStrCRC32(const XnChar *cpString, XnUInt32 *nCRC32)
XnUInt32 XnStatus
Definition: XnStatus.h:33
Definition: XnHashT.h:78
XnStatus Set(const const XnChar * &key, const void * &value)
Definition: XnHashT.h:382
Definition: XnStringsHashT.h:33
static XnInt32 Compare(const XnChar *const &key1, const XnChar *const &key2)
Definition: XnStringsHashT.h:44
static XnHashCode Hash(const XnChar *const &key)
Definition: XnStringsHashT.h:35
Definition: XnStringsHashT.h:80
XnStringsHashT & operator=(const XnStringsHashT &other)
Definition: XnStringsHashT.h:91
XnStringsHashT()
Definition: XnStringsHashT.h:84
XnStringsHashT(const XnStringsHashT &other)
Definition: XnStringsHashT.h:86
Definition: XnStringsHashT.h:52
static void Deallocate(TLinkedNode *pNode)
Definition: XnStringsHashT.h:68
XnLinkedNodeT< TPair > TLinkedNode
Definition: XnStringsHashT.h:55
XnKeyValuePair< const XnChar *, TValue > TPair
Definition: XnStringsHashT.h:54
static TLinkedNode * Allocate(TPair const &pair)
Definition: XnStringsHashT.h:57
Definition: XnStringsHashT.h:100
XnStatus Set(const XnChar *key)
Definition: XnStringsHashT.h:104
Definition: XnHashT.h:40
TValue const & Value() const
Definition: XnHashT.h:50
TKey const & Key() const
Definition: XnHashT.h:49
T value
Definition: XnListT.h:48