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  UT_Hash() = default;
36  virtual ~UT_Hash() = default;
37 
38  UT_Hash(const UT_Hash &) = delete;
39  UT_Hash &operator=(const UT_Hash &) = delete;
40 
41  // return 0 on equality
42  //
43  // Often, the comparison function for a UT_Hash is more expensive
44  // than computing the unsigned integer hash code. However, it's
45  // possible for the compare() function to be called even when
46  // there is no collision on the hash codes, therefore, if you
47  // have the hash codes cached, its often efficient to do
48  // a quick comparison of the hash codes before computing the
49  // expensive compare function.
50  virtual int compare(const UT_Hash &a) const = 0;
51 
52  // Copies data from another UT_Hash into this one.
53  virtual void copy(const UT_Hash &a) = 0;
54 
55  // return hash value
56  // should never change i.e. should rely on const data
57  virtual unsigned hash() const = 0;
58 
59  virtual UT_Hash* copy() const = 0;
60 
61  virtual int64 getMemoryUsage(bool inclusive) const = 0;
62 };
63 
64 class SYS_DEPRECATED_REPLACE(16.0, "SYShash<string>") UT_API UT_Hash_String :
65  public UT_Hash
66 {
67  private:
68  char *myData;
69  public:
70  UT_Hash_String( const char *s )
71  {
72  myData = strdup( s );
73  }
74 
75  ~UT_Hash_String() override
76  {
77  free(myData);
78  }
79 
80  int compare( const UT_Hash & a) const override
81  {
82  return strcmp(myData, ((const UT_Hash_String&)a).myData);
83  }
84 
85  void copy(const UT_Hash &a) override
86  {
87  free(myData);
88  myData = strdup(static_cast<const UT_Hash_String &>(a).myData);
89  }
90 
91  unsigned hash() const override
92  {
93  return UT_String::hash(myData);
94  }
95 
96  UT_Hash* copy() const override
97  {
98  return new UT_Hash_String( myData );
99  }
100 
101  int64 getMemoryUsage(bool inclusive) const override
102  {
103  return (inclusive ? sizeof(*this) : 0)
104  + (::strlen(myData) + 1) * sizeof(char);
105  }
106 
107  const char* getString() const
108  {
109  return myData;
110  }
111 };
112 
113 class SYS_DEPRECATED_REPLACE(16.0, "SYShash<int>") UT_API UT_Hash_Int :
114  public UT_Hash {
115  private:
116  int myData;
117  unsigned myKey;
118  public:
119  UT_Hash_Int( int a ) : myData( a )
120  {
121  myKey = (unsigned) a;
122  myKey = SYSwang_inthash(myKey);
123  }
124 
125  ~UT_Hash_Int() override
126  {
127  }
128 
129  int compare( const UT_Hash & a) const override
130  {
131  return !(myData==((const UT_Hash_Int&)a).myData);
132  }
133 
134  void copy(const UT_Hash &a) override
135  {
136  myData = static_cast<const UT_Hash_Int &>(a).myData;
137  myKey = static_cast<const UT_Hash_Int &>(a).myKey;
138  }
139 
140  unsigned hash() const override
141  {
142  return myKey;
143  }
144 
145  UT_Hash* copy() const override
146  {
147  return new UT_Hash_Int( myData );
148  }
149 
150  int64 getMemoryUsage(bool inclusive) const override
151  {
152  return inclusive ? sizeof(*this) : 0;
153  }
154 
155  int getData() const { return myData; }
156 };
157 
158 class SYS_DEPRECATED_REPLACE(16.0, "SYShash<int64>") UT_API UT_Hash_Int64 : public UT_Hash {
159  private:
160  int64 myData;
161  uint64 myKey;
162  public:
163  UT_Hash_Int64( int64 a ) : myData( a )
164  {
165  myKey = (uint64) a;
166  myKey = SYSwang_inthash64(myKey);
167  }
168 
169  ~UT_Hash_Int64() override
170  {
171  }
172 
173  int compare( const UT_Hash & a) const override
174  {
175  return !(myData==((const UT_Hash_Int64&)a).myData);
176  }
177 
178  void copy(const UT_Hash &a) override
179  {
180  myData = static_cast<const UT_Hash_Int64 &>(a).myData;
181  myKey = static_cast<const UT_Hash_Int64 &>(a).myKey;
182  }
183 
184  unsigned hash() const override
185  {
186  return myKey;
187  }
188 
189  UT_Hash* copy() const override
190  {
191  return new UT_Hash_Int64( myData );
192  }
193 
194  int64 getMemoryUsage(bool inclusive) const override
195  {
196  return inclusive ? sizeof(*this) : 0;
197  }
198 
199  int64 getData() const { return myData; }
200 };
201 
202 class SYS_DEPRECATED_REPLACE(16.0, "SYShash<void *>") UT_API UT_Hash_Ptr : public UT_Hash {
203  private:
204  const void *myData;
205  public:
206  UT_Hash_Ptr( const void *p )
207  : myData(p)
208  {
209  }
210 
211  ~UT_Hash_Ptr() override
212  {
213  }
214 
215  int compare(const UT_Hash &a) const override
216  {
217  return !(myData==((const UT_Hash_Ptr&)a).myData);
218  }
219 
220  void copy(const UT_Hash &a) override
221  {
222  myData = static_cast<const UT_Hash_Ptr &>(a).myData;
223  }
224 
225  unsigned hash() const override
226  {
227  unsigned key;
228  key = SYSpointerHash(myData);
229  key = SYSwang_inthash(key);
230  return key;
231  }
232 
233  UT_Hash* copy() const override
234  {
235  return new UT_Hash_Ptr(myData);
236  }
237 
238  int64 getMemoryUsage(bool inclusive) const override
239  {
240  return inclusive ? sizeof(*this) : 0;
241  }
242 
243  /// Get the pointer as a given type
244  template <typename T>
245  const T *asPointer() const { return (const T *)(myData); }
246 
247  /// Casting operators
248  operator const void *() const
249  {
250  return myData;
251  }
252  operator void *() const
253  {
254  return const_cast<void *>(myData);
255  }
256 };
257 
258 class SYS_DEPRECATED_REPLACE(16.0, "SYShash<const void *>") UT_API
259  UT_Hash_Const_Ptr : public UT_Hash {
260  private:
261  const void *myData;
262  public:
263  UT_Hash_Const_Ptr( const void *p ) : myData(p)
264  {
265  }
266 
267  ~UT_Hash_Const_Ptr() override
268  {
269  }
270 
271  int compare(const UT_Hash &a) const override
272  {
273  return !(myData==((const UT_Hash_Const_Ptr&)a).myData);
274  }
275 
276  void copy(const UT_Hash &a) override
277  {
278  myData = static_cast<const UT_Hash_Const_Ptr &>(a).myData;
279  }
280 
281  unsigned hash() const override
282  {
283  unsigned key;
284  key = SYSpointerHash(myData);
285  key = SYSwang_inthash(key);
286  return key;
287  }
288 
289  UT_Hash* copy() const override
290  {
291  return new UT_Hash_Const_Ptr(myData);
292  }
293 
294  int64 getMemoryUsage(bool inclusive) const override
295  {
296  return inclusive ? sizeof(*this) : 0;
297  }
298 };
299 
300 SYS_DEPRECATED_POP_DISABLE()
301 
302 #endif
303 
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
UT_Hash * copy() const override
Definition: UT_Hash.h:189
#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:85
void
Definition: png.h:1083
~UT_Hash_Int64() override
Definition: UT_Hash.h:169
unsigned hash() const override
Definition: UT_Guid.h:122
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
#define SYS_DEPRECATED_PUSH_DISABLE()
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
UT_Hash_String(const char *s)
Definition: UT_Hash.h:70
void copy(const UT_Hash &a) override
Definition: UT_Hash.h:178
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLdouble s
Definition: glad.h:3009
const char * getString() const
Definition: UT_Hash.h:107
#define UT_API
Definition: UT_API.h:14
int64 getMemoryUsage(bool inclusive) const override
Definition: UT_Hash.h:194
unsigned long long uint64
Definition: SYS_Types.h:117
UT_Hash * copy() const override
Definition: UT_Hash.h:96
unsigned hash() const override
Definition: UT_Hash.h:140
int compare(const UT_Hash &a) const override
Definition: UT_Hash.h:173
#define SYS_DEPRECATED_REPLACE(__V__, __R__)
UT_Hash_Int64(int64 a)
Definition: UT_Hash.h:163
UT_Hash()=default
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:892
#define UT_SMALLOBJECT_THREADSAFE_ON
UT_Hash * copy() const override
Definition: UT_Hash.h:145
UT_Hash & operator=(const UT_Hash &)=delete
unsigned hash() const override
Definition: UT_Hash.h:91
int getData() const
Definition: UT_Hash.h:155
long long int64
Definition: SYS_Types.h:116
int64 getData() const
Definition: UT_Hash.h:199
~UT_Hash_Int() override
Definition: UT_Hash.h:125
int compare(const UT_Hash &a) const override
Definition: UT_Hash.h:80
int64 getMemoryUsage(bool inclusive) const override
Definition: UT_Guid.h:130
int64 getMemoryUsage(bool inclusive) const override
Definition: UT_Hash.h:150
unsigned hash() const override
Definition: UT_Hash.h:184
void copy(const UT_Hash &a) override
Definition: UT_Hash.h:134
#define UT_SMALLOBJECT_CLEANPAGES_DEFAULT
virtual ~UT_Hash()=default
~UT_Hash_String() override
Definition: UT_Hash.h:75
int compare(const UT_Hash &a) const override
Definition: UT_Hash.h:129
int64 getMemoryUsage(bool inclusive) const override
Definition: UT_Hash.h:101
UT_Hash_Int(int a)
Definition: UT_Hash.h:119