CFx SDK Documentation  2022 SP0
OdFNVHash.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2017, Open Design Alliance (the "Alliance").
3 // All rights reserved.
4 //
5 // This software and its documentation and related materials are owned by
6 // the Alliance. The software may only be incorporated into application
7 // programs owned by members of the Alliance, subject to a signed
8 // Membership Agreement and Supplemental Software License Agreement with the
9 // Alliance. The structure and organization of this software are the valuable
10 // trade secrets of the Alliance and its suppliers. The software is also
11 // protected by copyright law and international treaty provisions. Application
12 // programs incorporating this software must include the following statement
13 // with their copyright notices:
14 //
15 // This application incorporates Teigha(R) software pursuant to a license
16 // agreement with Open Design Alliance.
17 // Teigha(R) Copyright (C) 2002-2017 by Open Design Alliance.
18 // All rights reserved.
19 //
20 // By use of this software, its documentation or related materials, you
21 // acknowledge and accept the above terms.
23 
24 /*
25  * Fowler/Noll/Vo hash
26  *
27  * The basis of this hash algorithm was taken from an idea sent
28  * as reviewer comments to the IEEE POSIX P1003.2 committee by:
29  *
30  * Phong Vo (http://www.research.att.com/info/kpv/)
31  * Glenn Fowler (http://www.research.att.com/~gsf/)
32  *
33  * In a subsequent ballot round:
34  *
35  * Landon Curt Noll (http://www.isthe.com/chongo/)
36  *
37  * improved on their algorithm. Some people tried this hash
38  * and found that it worked rather well. In an EMail message
39  * to Landon, they named it the ``Fowler/Noll/Vo'' or FNV hash.
40  *
41  * FNV hashes are designed to be fast while maintaining a low
42  * collision rate. The FNV speed allows one to quickly hash lots
43  * of data while maintaining a reasonable collision rate. See:
44  *
45  * http://www.isthe.com/chongo/tech/comp/fnv/index.html
46  *
47  * for more details as well as other forms of the FNV hash.
48  *
49  * Please do not copyright this code. This code is in the public domain.
50  *
51  * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
52  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
53  * EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
54  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
55  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
56  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
57  * PERFORMANCE OF THIS SOFTWARE.
58  *
59  * By:
60  * chongo <Landon Curt Noll> /\oo/\
61  * http://www.isthe.com/chongo/
62  *
63  * Share and Enjoy! :-)
64  */
65 
66 #ifndef _OD_FNV1HASH_H_
67 #define _OD_FNV1HASH_H_
68 
69 #include "TD_PackPush.h"
70 
85 inline OdUInt32 odFNV32HashBuf(const void *buf, size_t len, OdUInt32 hval = 0x811c9dc5)
86 {
87  const OdUInt8 *bp = (const OdUInt8*)buf; // Start of buffer
88  const OdUInt8 *be = bp + len; // End of buffer
89  while (bp < be) // FNV-1 hash each octet in the buffer
90  {
91  // Multiply by the 32 bit FNV magic prime mod 2^32
92 #if defined(NO_FNV_GCC_OPTIMIZATION)
93  hval *= 0x01000193;
94 #else
95  hval += (hval << 1) + (hval << 4) + (hval << 7) +
96  (hval << 8) + (hval << 24);
97 #endif
98  // Xor the bottom with the current octet
99  hval ^= (OdUInt32)*bp++;
100  }
101  // Return our new hash value
102  return hval;
103 }
104 
118 inline OdUInt32 odFNV32HashStr(const char *str, OdUInt32 hval = 0x811c9dc5)
119 {
120  const OdUInt8 *s = (const OdUInt8*)str; // Unsigned string
121  while (*s) // FNV-1 hash each octet in the buffer
122  {
123  // Multiply by the 32 bit FNV magic prime mod 2^32
124 #if defined(NO_FNV_GCC_OPTIMIZATION)
125  hval *= 0x01000193;
126 #else
127  hval += (hval << 1) + (hval << 4) + (hval << 7) +
128  (hval << 8) + (hval << 24);
129 #endif
130  // Xor the bottom with the current octet
131  hval ^= (OdUInt32)*s++;
132  }
133  // Return our new hash value
134  return hval;
135 }
136 
150 inline OdUInt32 odFNV32aHashBuf(const void *buf, size_t len, OdUInt32 hval = 0x811c9dc5)
151 {
152  const OdUInt8 *bp = (const OdUInt8*)buf; // Start of buffer
153  const OdUInt8 *be = bp + len; // End of buffer
154  while (bp < be) // FNV-1a hash each octet in the buffer
155  {
156  // Xor the bottom with the current octet
157  hval ^= (OdUInt32)*bp++;
158  // Multiply by the 32 bit FNV magic prime mod 2^32
159 #if defined(NO_FNV_GCC_OPTIMIZATION)
160  hval *= 0x01000193;
161 #else
162  hval += (hval << 1) + (hval << 4) + (hval << 7) +
163  (hval << 8) + (hval << 24);
164 #endif
165  }
166  // Return our new hash value
167  return hval;
168 }
169 
182 inline OdUInt32 odFNV32aHashStr(const char *str, OdUInt32 hval = 0x811c9dc5)
183 {
184  const OdUInt8 *s = (const OdUInt8*)str; // Unsigned string
185  while (*s) // FNV-1a hash each octet in the buffer
186  {
187  // Xor the bottom with the current octet
188  hval ^= (OdUInt32)*s++;
189  // Multiply by the 32 bit FNV magic prime mod 2^32
190 #if defined(NO_FNV_GCC_OPTIMIZATION)
191  hval *= 0x01000193;
192 #else
193  hval += (hval << 1) + (hval << 4) + (hval << 7) +
194  (hval << 8) + (hval << 24);
195 #endif
196  }
197  // Return our new hash value
198  return hval;
199 }
200 
215 inline OdUInt64 odFNV64HashBuf(const void *buf, size_t len, OdUInt64 hval = 0xcbf29ce484222325ULL)
216 {
217  const OdUInt8 *bp = (const OdUInt8*)buf; // Start of buffer
218  const OdUInt8 *be = bp + len; // Beyond end of buffer
219  while (bp < be) // FNV-1 hash each octet in the buffer
220  {
221  // Multiply by the 64 bit FNV magic prime mod 2^32
222 #if defined(NO_FNV_GCC_OPTIMIZATION)
223  hval *= 0x100000001b3;
224 #else
225  hval += (hval << 1) + (hval << 4) + (hval << 5) +
226  (hval << 7) + (hval << 8) + (hval << 40);
227 #endif
228  // Xor the bottom with the current octet
229  hval ^= (OdUInt64)*bp++;
230  }
231  // Return our new hash value
232  return hval;
233 }
234 
248 inline OdUInt64 odFNV64HashStr(const char *str, OdUInt64 hval = 0xcbf29ce484222325ULL)
249 {
250  const OdUInt8 *s = (const OdUInt8*)str; // Unsigned string
251  while (*s) // FNV-1 hash each octet in the buffer
252  {
253  // Multiply by the 64 bit FNV magic prime mod 2^32
254 #if defined(NO_FNV_GCC_OPTIMIZATION)
255  hval *= 0x100000001b3;
256 #else
257  hval += (hval << 1) + (hval << 4) + (hval << 5) +
258  (hval << 7) + (hval << 8) + (hval << 40);
259 #endif
260  // Xor the bottom with the current octet
261  hval ^= (OdUInt64)*s++;
262  }
263  // Return our new hash value
264  return hval;
265 }
266 
280 inline OdUInt64 odFNV64aHashBuf(const void *buf, size_t len, OdUInt64 hval = 0xcbf29ce484222325ULL)
281 {
282  const OdUInt8 *bp = (const OdUInt8*)buf; // Start of buffer
283  const OdUInt8 *be = bp + len; // Beyond end of buffer
284  while (bp < be) // FNV-1a hash each octet in the buffer
285  {
286  // Xor the bottom with the current octet
287  hval ^= (OdUInt64)*bp++;
288  // Multiply by the 64 bit FNV magic prime mod 2^32
289 #if defined(NO_FNV_GCC_OPTIMIZATION)
290  hval *= 0x100000001b3;
291 #else
292  hval += (hval << 1) + (hval << 4) + (hval << 5) +
293  (hval << 7) + (hval << 8) + (hval << 40);
294 #endif
295  }
296  // Return our new hash value
297  return hval;
298 }
299 
312 inline OdUInt64 odFNV64aHashStr(const char *str, OdUInt64 hval = 0xcbf29ce484222325ULL)
313 {
314  const OdUInt8 *s = (const OdUInt8*)str; // Unsigned string
315  while (*s) // FNV-1a hash each octet in the buffer
316  {
317  // Xor the bottom with the current octet
318  hval ^= (OdUInt64)*s++;
319  // Multiply by the 64 bit FNV magic prime mod 2^32
320 #if defined(NO_FNV_GCC_OPTIMIZATION)
321  hval *= 0x100000001b3;
322 #else
323  hval += (hval << 1) + (hval << 4) + (hval << 5) +
324  (hval << 7) + (hval << 8) + (hval << 40);
325 #endif
326  }
327  // Return our new hash value
328  return hval;
329 }
330 
331 // C++ wrappers for hash functions
332 
333 template <typename Type, size_t szf = sizeof(Type)>
334 class OdFNVHashWrap { };
335 template <typename Type>
337 {
338  public:
339  // For buffer
340  static inline Type buf(const void *buf, size_t len) { return (Type)::odFNV32HashBuf(buf, len); };
341  static inline Type buf(const void *buf, size_t len, Type hval) { return (Type)::odFNV32HashBuf(buf, len, (OdUInt32)hval); };
342  // For strings
343  static inline Type str(const char *str) { return (Type)::odFNV32HashStr(str); };
344  static inline Type str(const char *str, Type hval) { return (Type)::odFNV32HashStr(str, (OdUInt32)hval); };
345  static inline Type str(const OdChar *str) { return buf(str, odStrLen(str) * sizeof(OdChar)); };
346  static inline Type str(const OdChar *str, Type hval) { return buf(str, odStrLen(str) * sizeof(OdChar), hval); };
347  static inline Type str(const OdString &str) { return buf(str.c_str(), str.getLength() * sizeof(OdChar)); };
348  static inline Type str(const OdString &str, Type hval) { return buf(str.c_str(), str.getLength() * sizeof(OdChar), hval); };
349  // For other types
350  template <typename DataType>
351  static inline Type type(DataType data) { return buf(&data, sizeof(DataType)); }
352  template <typename DataType>
353  static inline Type type(DataType data, Type hval) { return buf(&data, sizeof(DataType), hval); }
354  // For typed arrays
355  template <typename DataType>
356  static inline Type tArray(const DataType *pData, size_t nVals) { return buf(pData, sizeof(DataType) * nVals); }
357  template <typename DataType>
358  static inline Type tArray(const DataType *pData, size_t nVals, Type hval) { return buf(pData, sizeof(DataType) * nVals, hval); }
359  // For ODA arrays and vectors
360  template <typename ArrayType>
361  static inline Type odArray(const ArrayType &arry) { return tArray(arry.getPtr(), arry.size()); }
362  template <typename ArrayType>
363  static inline Type odArray(const ArrayType &arry, Type hval) { return tArray(arry.getPtr(), arry.size(), hval); }
364 };
365 template <typename Type>
367 {
368  public:
369  // For buffer
370  static inline Type buf(const void *buf, size_t len) { return (Type)::odFNV64HashBuf(buf, len); };
371  static inline Type buf(const void *buf, size_t len, Type hval) { return (Type)::odFNV64HashBuf(buf, len, (OdUInt64)hval); };
372  // For strings
373  static inline Type str(const char *str) { return (Type)::odFNV64HashStr(str); };
374  static inline Type str(const char *str, Type hval) { return (Type)::odFNV64HashStr(str, (OdUInt32)hval); };
375  static inline Type str(const OdChar *str) { return buf(str, odStrLen(str) * sizeof(OdChar)); };
376  static inline Type str(const OdChar *str, Type hval) { return buf(str, odStrLen(str) * sizeof(OdChar), hval); };
377  static inline Type str(const OdString &str) { return buf(str.c_str(), str.getLength() * sizeof(OdChar)); };
378  static inline Type str(const OdString &str, Type hval) { return buf(str.c_str(), str.getLength() * sizeof(OdChar), hval); };
379  // For other types
380  template <typename DataType>
381  static inline Type type(DataType data) { return buf(&data, sizeof(DataType)); }
382  template <typename DataType>
383  static inline Type type(DataType data, Type hval) { return buf(&data, sizeof(DataType), hval); }
384  // For typed arrays
385  template <typename DataType>
386  static inline Type tArray(const DataType *pData, size_t nVals) { return buf(pData, sizeof(DataType) * nVals); }
387  template <typename DataType>
388  static inline Type tArray(const DataType *pData, size_t nVals, Type hval) { return buf(pData, sizeof(DataType) * nVals, hval); }
389  // For ODA arrays and vectors
390  template <typename ArrayType>
391  static inline Type odArray(const ArrayType &arry) { return tArray(arry.getPtr(), arry.size()); }
392  template <typename ArrayType>
393  static inline Type odArray(const ArrayType &arry, Type hval) { return tArray(arry.getPtr(), arry.size(), hval); }
394 };
395 
396 template <typename Type, size_t szf = sizeof(Type)>
397 class OdFNVaHashWrap { };
398 template <typename Type>
400 {
401  public:
402  // For buffer
403  static inline Type buf(const void *buf, size_t len) { return (Type)::odFNV32aHashBuf(buf, len); };
404  static inline Type buf(const void *buf, size_t len, Type hval) { return (Type)::odFNV32aHashBuf(buf, len, (OdUInt32)hval); };
405  // For strings
406  static inline Type str(const char *str) { return (Type)::odFNV32aHashStr(str); };
407  static inline Type str(const char *str, Type hval) { return (Type)::odFNV32aHashStr(str, (OdUInt32)hval); };
408  static inline Type str(const OdChar *str) { return buf(str, odStrLen(str) * sizeof(OdChar)); };
409  static inline Type str(const OdChar *str, Type hval) { return buf(str, odStrLen(str) * sizeof(OdChar), hval); };
410  static inline Type str(const OdString &str) { return buf(str.c_str(), str.getLength() * sizeof(OdChar)); };
411  static inline Type str(const OdString &str, Type hval) { return buf(str.c_str(), str.getLength() * sizeof(OdChar), hval); };
412  // For other types
413  template <typename DataType>
414  static inline Type type(DataType data) { return buf(&data, sizeof(DataType)); }
415  template <typename DataType>
416  static inline Type type(DataType data, Type hval) { return buf(&data, sizeof(DataType), hval); }
417  // For typed arrays
418  template <typename DataType>
419  static inline Type tArray(const DataType *pData, size_t nVals) { return buf(pData, sizeof(DataType) * nVals); }
420  template <typename DataType>
421  static inline Type tArray(const DataType *pData, size_t nVals, Type hval) { return buf(pData, sizeof(DataType) * nVals, hval); }
422  // For ODA arrays and vectors
423  template <typename ArrayType>
424  static inline Type odArray(const ArrayType &arry) { return tArray(arry.getPtr(), arry.size()); }
425  template <typename ArrayType>
426  static inline Type odArray(const ArrayType &arry, Type hval) { return tArray(arry.getPtr(), arry.size(), hval); }
427 };
428 template <typename Type>
430 {
431  public:
432  // For buffer
433  static inline Type buf(const void *buf, size_t len) { return (Type)::odFNV64aHashBuf(buf, len); };
434  static inline Type buf(const void *buf, size_t len, Type hval) { return (Type)::odFNV64aHashBuf(buf, len, (OdUInt64)hval); };
435  // For strings
436  static inline Type str(const char *str) { return (Type)::odFNV64aHashStr(str); };
437  static inline Type str(const char *str, Type hval) { return (Type)::odFNV64aHashStr(str, (OdUInt32)hval); };
438  static inline Type str(const OdChar *str) { return buf(str, odStrLen(str) * sizeof(OdChar)); };
439  static inline Type str(const OdChar *str, Type hval) { return buf(str, odStrLen(str) * sizeof(OdChar), hval); };
440  static inline Type str(const OdString &str) { return buf(str.c_str(), str.getLength() * sizeof(OdChar)); };
441  static inline Type str(const OdString &str, Type hval) { return buf(str.c_str(), str.getLength() * sizeof(OdChar), hval); };
442  // For other types
443  template <typename DataType>
444  static inline Type type(DataType data) { return buf(&data, sizeof(DataType)); }
445  template <typename DataType>
446  static inline Type type(DataType data, Type hval) { return buf(&data, sizeof(DataType), hval); }
447  // For typed arrays
448  template <typename DataType>
449  static inline Type tArray(const DataType *pData, size_t nVals) { return buf(pData, sizeof(DataType) * nVals); }
450  template <typename DataType>
451  static inline Type tArray(const DataType *pData, size_t nVals, Type hval) { return buf(pData, sizeof(DataType) * nVals, hval); }
452  // For ODA arrays and vectors
453  template <typename ArrayType>
454  static inline Type odArray(const ArrayType &arry) { return tArray(arry.getPtr(), arry.size()); }
455  template <typename ArrayType>
456  static inline Type odArray(const ArrayType &arry, Type hval) { return tArray(arry.getPtr(), arry.size(), hval); }
457 };
458 
459 #include "TD_PackPop.h"
460 
461 #endif //_OD_FNV1HASH_H_
OdUInt64 odFNV64aHashStr(const char *str, OdUInt64 hval=0xcbf29ce484222325ULL)
Definition: OdFNVHash.h:312
OdUInt32 odFNV32aHashBuf(const void *buf, size_t len, OdUInt32 hval=0x811c9dc5)
Definition: OdFNVHash.h:150
OdUInt32 odFNV32aHashStr(const char *str, OdUInt32 hval=0x811c9dc5)
Definition: OdFNVHash.h:182
OdUInt64 odFNV64HashBuf(const void *buf, size_t len, OdUInt64 hval=0xcbf29ce484222325ULL)
Definition: OdFNVHash.h:215
OdUInt64 odFNV64HashStr(const char *str, OdUInt64 hval=0xcbf29ce484222325ULL)
Definition: OdFNVHash.h:248
OdUInt64 odFNV64aHashBuf(const void *buf, size_t len, OdUInt64 hval=0xcbf29ce484222325ULL)
Definition: OdFNVHash.h:280
OdUInt32 odFNV32HashBuf(const void *buf, size_t len, OdUInt32 hval=0x811c9dc5)
Definition: OdFNVHash.h:85
OdUInt32 odFNV32HashStr(const char *str, OdUInt32 hval=0x811c9dc5)
Definition: OdFNVHash.h:118
#define odStrLen(str)
Definition: OdPlatform.h:240
unsigned int OdUInt32
unsigned char OdUInt8
wchar_t OdChar
static Type buf(const void *buf, size_t len)
Definition: OdFNVHash.h:340
static Type str(const char *str)
Definition: OdFNVHash.h:343
static Type tArray(const DataType *pData, size_t nVals)
Definition: OdFNVHash.h:356
static Type str(const OdChar *str)
Definition: OdFNVHash.h:345
static Type str(const OdString &str, Type hval)
Definition: OdFNVHash.h:348
static Type odArray(const ArrayType &arry)
Definition: OdFNVHash.h:361
static Type str(const OdString &str)
Definition: OdFNVHash.h:347
static Type str(const OdChar *str, Type hval)
Definition: OdFNVHash.h:346
static Type buf(const void *buf, size_t len, Type hval)
Definition: OdFNVHash.h:341
static Type tArray(const DataType *pData, size_t nVals, Type hval)
Definition: OdFNVHash.h:358
static Type str(const char *str, Type hval)
Definition: OdFNVHash.h:344
static Type type(DataType data)
Definition: OdFNVHash.h:351
static Type type(DataType data, Type hval)
Definition: OdFNVHash.h:353
static Type odArray(const ArrayType &arry, Type hval)
Definition: OdFNVHash.h:363
static Type tArray(const DataType *pData, size_t nVals, Type hval)
Definition: OdFNVHash.h:388
static Type str(const OdString &str, Type hval)
Definition: OdFNVHash.h:378
static Type str(const OdChar *str, Type hval)
Definition: OdFNVHash.h:376
static Type odArray(const ArrayType &arry, Type hval)
Definition: OdFNVHash.h:393
static Type str(const OdString &str)
Definition: OdFNVHash.h:377
static Type tArray(const DataType *pData, size_t nVals)
Definition: OdFNVHash.h:386
static Type str(const char *str, Type hval)
Definition: OdFNVHash.h:374
static Type str(const char *str)
Definition: OdFNVHash.h:373
static Type buf(const void *buf, size_t len, Type hval)
Definition: OdFNVHash.h:371
static Type odArray(const ArrayType &arry)
Definition: OdFNVHash.h:391
static Type buf(const void *buf, size_t len)
Definition: OdFNVHash.h:370
static Type str(const OdChar *str)
Definition: OdFNVHash.h:375
static Type type(DataType data)
Definition: OdFNVHash.h:381
static Type type(DataType data, Type hval)
Definition: OdFNVHash.h:383
static Type buf(const void *buf, size_t len)
Definition: OdFNVHash.h:403
static Type buf(const void *buf, size_t len, Type hval)
Definition: OdFNVHash.h:404
static Type type(DataType data, Type hval)
Definition: OdFNVHash.h:416
static Type type(DataType data)
Definition: OdFNVHash.h:414
static Type str(const char *str, Type hval)
Definition: OdFNVHash.h:407
static Type str(const OdString &str, Type hval)
Definition: OdFNVHash.h:411
static Type odArray(const ArrayType &arry)
Definition: OdFNVHash.h:424
static Type odArray(const ArrayType &arry, Type hval)
Definition: OdFNVHash.h:426
static Type str(const char *str)
Definition: OdFNVHash.h:406
static Type str(const OdChar *str, Type hval)
Definition: OdFNVHash.h:409
static Type str(const OdChar *str)
Definition: OdFNVHash.h:408
static Type tArray(const DataType *pData, size_t nVals, Type hval)
Definition: OdFNVHash.h:421
static Type tArray(const DataType *pData, size_t nVals)
Definition: OdFNVHash.h:419
static Type str(const OdString &str)
Definition: OdFNVHash.h:410
static Type str(const OdChar *str)
Definition: OdFNVHash.h:438
static Type str(const OdString &str)
Definition: OdFNVHash.h:440
static Type str(const OdString &str, Type hval)
Definition: OdFNVHash.h:441
static Type str(const char *str, Type hval)
Definition: OdFNVHash.h:437
static Type tArray(const DataType *pData, size_t nVals, Type hval)
Definition: OdFNVHash.h:451
static Type buf(const void *buf, size_t len, Type hval)
Definition: OdFNVHash.h:434
static Type buf(const void *buf, size_t len)
Definition: OdFNVHash.h:433
static Type tArray(const DataType *pData, size_t nVals)
Definition: OdFNVHash.h:449
static Type str(const OdChar *str, Type hval)
Definition: OdFNVHash.h:439
static Type str(const char *str)
Definition: OdFNVHash.h:436
static Type odArray(const ArrayType &arry)
Definition: OdFNVHash.h:454
static Type type(DataType data)
Definition: OdFNVHash.h:444
static Type odArray(const ArrayType &arry, Type hval)
Definition: OdFNVHash.h:456
static Type type(DataType data, Type hval)
Definition: OdFNVHash.h:446
const OdChar * c_str() const
Definition: OdString.h:200
int getLength() const
Definition: OdString.h:130
GLint GLenum GLsizei GLsizei GLint GLsizei const void * data
Definition: gles2_ext.h:110