HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 
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  ~UT_Hash_String() override
72  {
73  free(myData);
74  }
75 
76  int compare( const UT_Hash & a) const override
77  {
78  return strcmp(myData, ((const UT_Hash_String&)a).myData);
79  }
80 
81  void copy(const UT_Hash &a) override
82  {
83  free(myData);
84  myData = strdup(static_cast<const UT_Hash_String &>(a).myData);
85  }
86 
87  unsigned hash() const override
88  {
89  return UT_String::hash(myData);
90  }
91 
92  UT_Hash* copy() const override
93  {
94  return new UT_Hash_String( myData );
95  }
96 
97  int64 getMemoryUsage(bool inclusive) const override
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  ~UT_Hash_Int() override
122  {
123  }
124 
125  int compare( const UT_Hash & a) const override
126  {
127  return !(myData==((const UT_Hash_Int&)a).myData);
128  }
129 
130  void copy(const UT_Hash &a) override
131  {
132  myData = static_cast<const UT_Hash_Int &>(a).myData;
133  myKey = static_cast<const UT_Hash_Int &>(a).myKey;
134  }
135 
136  unsigned hash() const override
137  {
138  return myKey;
139  }
140 
141  UT_Hash* copy() const override
142  {
143  return new UT_Hash_Int( myData );
144  }
145 
146  int64 getMemoryUsage(bool inclusive) const override
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  ~UT_Hash_Int64() override
166  {
167  }
168 
169  int compare( const UT_Hash & a) const override
170  {
171  return !(myData==((const UT_Hash_Int64&)a).myData);
172  }
173 
174  void copy(const UT_Hash &a) override
175  {
176  myData = static_cast<const UT_Hash_Int64 &>(a).myData;
177  myKey = static_cast<const UT_Hash_Int64 &>(a).myKey;
178  }
179 
180  unsigned hash() const override
181  {
182  return myKey;
183  }
184 
185  UT_Hash* copy() const override
186  {
187  return new UT_Hash_Int64( myData );
188  }
189 
190  int64 getMemoryUsage(bool inclusive) const override
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  ~UT_Hash_Ptr() override
208  {
209  }
210 
211  int compare(const UT_Hash &a) const override
212  {
213  return !(myData==((const UT_Hash_Ptr&)a).myData);
214  }
215 
216  void copy(const UT_Hash &a) override
217  {
218  myData = static_cast<const UT_Hash_Ptr &>(a).myData;
219  }
220 
221  unsigned hash() const override
222  {
223  unsigned key;
224  key = SYSpointerHash(myData);
225  key = SYSwang_inthash(key);
226  return key;
227  }
228 
229  UT_Hash* copy() const override
230  {
231  return new UT_Hash_Ptr(myData);
232  }
233 
234  int64 getMemoryUsage(bool inclusive) const override
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  ~UT_Hash_Const_Ptr() override
264  {
265  }
266 
267  int compare(const UT_Hash &a) const override
268  {
269  return !(myData==((const UT_Hash_Const_Ptr&)a).myData);
270  }
271 
272  void copy(const UT_Hash &a) override
273  {
274  myData = static_cast<const UT_Hash_Const_Ptr &>(a).myData;
275  }
276 
277  unsigned hash() const override
278  {
279  unsigned key;
280  key = SYSpointerHash(myData);
281  key = SYSwang_inthash(key);
282  return key;
283  }
284 
285  UT_Hash* copy() const override
286  {
287  return new UT_Hash_Const_Ptr(myData);
288  }
289 
290  int64 getMemoryUsage(bool inclusive) const override
291  {
292  return inclusive ? sizeof(*this) : 0;
293  }
294 };
295 
296 SYS_DEPRECATED_POP_DISABLE()
297 
298 #endif
299 
UT_Hash * copy() const override
Definition: UT_Hash.h:185
#define UT_SMALLOBJECT_PAGESIZE_DEFAULT
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:623
void copy(const UT_Hash &a) override
Definition: UT_Hash.h:81
void
Definition: png.h:1083
~UT_Hash_Int64() override
Definition: UT_Hash.h:165
unsigned hash() const override
Definition: UT_Guid.h:124
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
#define SYS_DEPRECATED_PUSH_DISABLE()
UT_Hash_String(const char *s)
Definition: UT_Hash.h:66
void copy(const UT_Hash &a) override
Definition: UT_Hash.h:174
const char * getString() const
Definition: UT_Hash.h:103
#define UT_API
Definition: UT_API.h:14
int64 getMemoryUsage(bool inclusive) const override
Definition: UT_Hash.h:190
unsigned long long uint64
Definition: SYS_Types.h:117
UT_Hash * copy() const override
Definition: UT_Hash.h:92
unsigned hash() const override
Definition: UT_Hash.h:136
int compare(const UT_Hash &a) const override
Definition: UT_Hash.h:169
#define SYS_DEPRECATED_REPLACE(__V__, __R__)
UT_Hash_Int64(int64 a)
Definition: UT_Hash.h:159
CompareResults OIIO_API compare(const ImageBuf &A, const ImageBuf &B, float failthresh, float warnthresh, ROI roi={}, int nthreads=0)
SYS_FORCE_INLINE uint32 hash() const
Definition: UT_String.h:882
#define UT_SMALLOBJECT_THREADSAFE_ON
UT_Hash * copy() const override
Definition: UT_Hash.h:141
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
unsigned hash() const override
Definition: UT_Hash.h:87
int getData() const
Definition: UT_Hash.h:151
long long int64
Definition: SYS_Types.h:116
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
int64 getData() const
Definition: UT_Hash.h:195
~UT_Hash_Int() override
Definition: UT_Hash.h:121
int compare(const UT_Hash &a) const override
Definition: UT_Hash.h:76
int64 getMemoryUsage(bool inclusive) const override
Definition: UT_Guid.h:132
int64 getMemoryUsage(bool inclusive) const override
Definition: UT_Hash.h:146
unsigned hash() const override
Definition: UT_Hash.h:180
void copy(const UT_Hash &a) override
Definition: UT_Hash.h:130
#define UT_SMALLOBJECT_CLEANPAGES_DEFAULT
virtual ~UT_Hash()
Definition: UT_Hash.h:35
~UT_Hash_String() override
Definition: UT_Hash.h:71
int compare(const UT_Hash &a) const override
Definition: UT_Hash.h:125
GLdouble s
Definition: glew.h:1395
int64 getMemoryUsage(bool inclusive) const override
Definition: UT_Hash.h:97
UT_Hash_Int(int a)
Definition: UT_Hash.h:115