HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_Hash.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_Hash.h ( UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_Hash__
12 #define __UT_Hash__
13 
14 #include "UT_API.h"
15 #include <string.h>
16 #include <stdlib.h>
17 #include <malloc.h>
18 
19 #include <SYS/SYS_Deprecated.h>
20 #include <SYS/SYS_Math.h>
21 #include "UT_String.h"
22 #include "UT_SmallObject.h"
23 
25 
26 class SYS_DEPRECATED_REPLACE(16.0, "SYShash") UT_API UT_Hash
27 #if !SYS_IS_GCC_GE(4,7)
28  : public UT_SmallObject<UT_Hash,
32 #endif // GCC 4.7+ crashes in some cases with UT_Hash as a small object.
33 {
34  public:
35  virtual ~UT_Hash() { }
36 
37  // return 0 on equality
38  //
39  // Often, the comparison function for a UT_Hash is more expensive
40  // than computing the unsigned integer hash code. However, it's
41  // possible for the compare() function to be called even when
42  // there is no collision on the hash codes, therefore, if you
43  // have the hash codes cached, its often efficient to do
44  // a quick comparison of the hash codes before computing the
45  // expensive compare function.
46  virtual int compare(const UT_Hash &a) const = 0;
47 
48  // Copies data from another UT_Hash into this one.
49  virtual void copy(const UT_Hash &a) = 0;
50 
51  // return hash value
52  // should never change i.e. should rely on const data
53  virtual unsigned hash() const = 0;
54 
55  virtual UT_Hash* copy() const = 0;
56 
57  virtual int64 getMemoryUsage(bool inclusive) const = 0;
58 };
59 
60 class SYS_DEPRECATED_REPLACE(16.0, "SYShash<string>") UT_API UT_Hash_String :
61  public UT_Hash
62 {
63  private:
64  char *myData;
65  public:
66  UT_Hash_String( const char *s )
67  {
68  myData = strdup( s );
69  }
70 
71  virtual ~UT_Hash_String()
72  {
73  free(myData);
74  }
75 
76  virtual int compare( const UT_Hash & a) const
77  {
78  return strcmp(myData, ((const UT_Hash_String&)a).myData);
79  }
80 
81  virtual void copy(const UT_Hash &a)
82  {
83  free(myData);
84  myData = strdup(static_cast<const UT_Hash_String &>(a).myData);
85  }
86 
87  virtual unsigned hash() const
88  {
89  return UT_String::hash(myData);
90  }
91 
92  virtual UT_Hash* copy() const
93  {
94  return new UT_Hash_String( myData );
95  }
96 
97  virtual int64 getMemoryUsage(bool inclusive) const
98  {
99  return (inclusive ? sizeof(*this) : 0)
100  + (::strlen(myData) + 1) * sizeof(char);
101  }
102 
103  const char* getString() const
104  {
105  return myData;
106  }
107 };
108 
109 class SYS_DEPRECATED_REPLACE(16.0, "SYShash<int>") UT_API UT_Hash_Int :
110  public UT_Hash {
111  private:
112  int myData;
113  unsigned myKey;
114  public:
115  UT_Hash_Int( int a ) : myData( a )
116  {
117  myKey = (unsigned) a;
118  myKey = SYSwang_inthash(myKey);
119  }
120 
121  virtual ~UT_Hash_Int()
122  {
123  }
124 
125  virtual int compare( const UT_Hash & a) const
126  {
127  return !(myData==((const UT_Hash_Int&)a).myData);
128  }
129 
130  virtual void copy(const UT_Hash &a)
131  {
132  myData = static_cast<const UT_Hash_Int &>(a).myData;
133  myKey = static_cast<const UT_Hash_Int &>(a).myKey;
134  }
135 
136  virtual unsigned hash() const
137  {
138  return myKey;
139  }
140 
141  virtual UT_Hash* copy() const
142  {
143  return new UT_Hash_Int( myData );
144  }
145 
146  virtual int64 getMemoryUsage(bool inclusive) const
147  {
148  return inclusive ? sizeof(*this) : 0;
149  }
150 
151  int getData() const { return myData; }
152 };
153 
154 class SYS_DEPRECATED_REPLACE(16.0, "SYShash<int64>") UT_API UT_Hash_Int64 : public UT_Hash {
155  private:
156  int64 myData;
157  uint64 myKey;
158  public:
159  UT_Hash_Int64( int64 a ) : myData( a )
160  {
161  myKey = (uint64) a;
162  myKey = SYSwang_inthash64(myKey);
163  }
164 
165  virtual ~UT_Hash_Int64()
166  {
167  }
168 
169  virtual int compare( const UT_Hash & a) const
170  {
171  return !(myData==((const UT_Hash_Int64&)a).myData);
172  }
173 
174  virtual void copy(const UT_Hash &a)
175  {
176  myData = static_cast<const UT_Hash_Int64 &>(a).myData;
177  myKey = static_cast<const UT_Hash_Int64 &>(a).myKey;
178  }
179 
180  virtual unsigned hash() const
181  {
182  return myKey;
183  }
184 
185  virtual UT_Hash* copy() const
186  {
187  return new UT_Hash_Int64( myData );
188  }
189 
190  virtual int64 getMemoryUsage(bool inclusive) const
191  {
192  return inclusive ? sizeof(*this) : 0;
193  }
194 
195  int64 getData() const { return myData; }
196 };
197 
198 class SYS_DEPRECATED_REPLACE(16.0, "SYShash<void *>") UT_API UT_Hash_Ptr : public UT_Hash {
199  private:
200  const void *myData;
201  public:
202  UT_Hash_Ptr( const void *p )
203  : myData(p)
204  {
205  }
206 
207  virtual ~UT_Hash_Ptr()
208  {
209  }
210 
211  virtual int compare(const UT_Hash &a) const
212  {
213  return !(myData==((const UT_Hash_Ptr&)a).myData);
214  }
215 
216  virtual void copy(const UT_Hash &a)
217  {
218  myData = static_cast<const UT_Hash_Ptr &>(a).myData;
219  }
220 
221  virtual unsigned hash() const
222  {
223  unsigned key;
224  key = SYSpointerHash(myData);
225  key = SYSwang_inthash(key);
226  return key;
227  }
228 
229  virtual UT_Hash* copy() const
230  {
231  return new UT_Hash_Ptr(myData);
232  }
233 
234  virtual int64 getMemoryUsage(bool inclusive) const
235  {
236  return inclusive ? sizeof(*this) : 0;
237  }
238 
239  /// Get the pointer as a given type
240  template <typename T>
241  const T *asPointer() const { return (const T *)(myData); }
242 
243  /// Casting operators
244  operator const void *() const
245  {
246  return myData;
247  }
248  operator void *() const
249  {
250  return const_cast<void *>(myData);
251  }
252 };
253 
254 class SYS_DEPRECATED_REPLACE(16.0, "SYShash<const void *>") UT_API
255  UT_Hash_Const_Ptr : public UT_Hash {
256  private:
257  const void *myData;
258  public:
259  UT_Hash_Const_Ptr( const void *p ) : myData(p)
260  {
261  }
262 
263  virtual ~UT_Hash_Const_Ptr()
264  {
265  }
266 
267  virtual int compare(const UT_Hash &a) const
268  {
269  return !(myData==((const UT_Hash_Const_Ptr&)a).myData);
270  }
271 
272  virtual void copy(const UT_Hash &a)
273  {
274  myData = static_cast<const UT_Hash_Const_Ptr &>(a).myData;
275  }
276 
277  virtual unsigned hash() const
278  {
279  unsigned key;
280  key = SYSpointerHash(myData);
281  key = SYSwang_inthash(key);
282  return key;
283  }
284 
285  virtual UT_Hash* copy() const
286  {
287  return new UT_Hash_Const_Ptr(myData);
288  }
289 
290  virtual int64 getMemoryUsage(bool inclusive) const
291  {
292  return inclusive ? sizeof(*this) : 0;
293  }
294 };
295 
296 SYS_DEPRECATED_POP_DISABLE()
297 
298 #endif
299 
virtual int compare(const UT_Hash &a) const
Definition: UT_Hash.h:76
virtual int64 getMemoryUsage(bool inclusive) const
Definition: UT_Hash.h:97
virtual void copy(const UT_Hash &a)
Definition: UT_Hash.h:130
virtual unsigned hash() const
Definition: UT_Hash.h:180
#define UT_SMALLOBJECT_PAGESIZE_DEFAULT
virtual UT_Hash * copy() const
Definition: UT_Guid.h:126
virtual int64 getMemoryUsage(bool inclusive) const
Definition: UT_Hash.h:190
#define SYS_DEPRECATED_PUSH_DISABLE()
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
virtual unsigned hash() const
Definition: UT_Hash.h:87
UT_Hash_String(const char *s)
Definition: UT_Hash.h:66
virtual int compare(const UT_Hash &a) const
Definition: UT_Hash.h:169
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
const char * getString() const
Definition: UT_Hash.h:103
#define UT_API
Definition: UT_API.h:12
virtual ~UT_Hash_Int()
Definition: UT_Hash.h:121
#define SYS_DEPRECATED_REPLACE(__V__, __R__)
UT_Hash_Int64(int64 a)
Definition: UT_Hash.h:159
long long int64
Definition: SYS_Types.h:106
SYS_FORCE_INLINE uint32 hash() const
Definition: UT_String.h:865
#define UT_SMALLOBJECT_THREADSAFE_ON
unsigned long long uint64
Definition: SYS_Types.h:107
virtual int64 getMemoryUsage(bool inclusive) const
Definition: UT_Guid.h:130
virtual UT_Hash * copy() const
Definition: UT_Hash.h:141
int getData() const
Definition: UT_Hash.h:151
virtual void copy(const UT_Hash &a)
Definition: UT_Hash.h:174
virtual ~UT_Hash_String()
Definition: UT_Hash.h:71
virtual void copy(const UT_Hash &a)
Definition: UT_Hash.h:81
int64 getData() const
Definition: UT_Hash.h:195
virtual UT_Hash * copy() const
Definition: UT_Hash.h:185
virtual int compare(const UT_Hash &a) const
Definition: UT_Hash.h:125
virtual int64 getMemoryUsage(bool inclusive) const
Definition: UT_Hash.h:146
#define UT_SMALLOBJECT_CLEANPAGES_DEFAULT
typedef int
Definition: png.h:1175
virtual ~UT_Hash()
Definition: UT_Hash.h:35
virtual UT_Hash * copy() const
Definition: UT_Hash.h:92
virtual ~UT_Hash_Int64()
Definition: UT_Hash.h:165
virtual unsigned hash() const
Definition: UT_Guid.h:122
virtual int compare(const UT_Hash &a) const
Definition: UT_Guid.h:102
virtual unsigned hash() const
Definition: UT_Hash.h:136
UT_Hash_Int(int a)
Definition: UT_Hash.h:115