Actual source code: hashtable.h
1: #ifndef PETSC_HASHTABLE_H
2: #define PETSC_HASHTABLE_H
4: #include <petsc/private/petscimpl.h>
6: #define kh_inline inline
7: #define klib_unused PETSC_UNUSED
8: #include <petsc/private/khash/khash.h>
10: /* Required for khash <= 0.2.5 */
11: #if !defined(kcalloc)
12: #define kcalloc(N, Z) calloc(N, Z)
13: #endif
14: #if !defined(kmalloc)
15: #define kmalloc(Z) malloc(Z)
16: #endif
17: #if !defined(krealloc)
18: #define krealloc(P, Z) realloc(P, Z)
19: #endif
20: #if !defined(kfree)
21: #define kfree(P) free(P)
22: #endif
24: /* --- Useful extensions to khash --- */
26: #if !defined(kh_reset)
27: /*! @function
28: @abstract Reset a hash table to initial state.
29: @param name Name of the hash table [symbol]
30: @param h Pointer to the hash table [khash_t(name)*]
31: */
32: #define kh_reset(name, h) \
33: { \
34: if (h) { \
35: kfree((h)->keys); \
36: kfree((h)->flags); \
37: kfree((h)->vals); \
38: memset((h), 0x00, sizeof(*(h))); \
39: } \
40: }
41: #endif /*kh_reset*/
43: #if !defined(kh_foreach)
44: /*! @function
45: @abstract Iterate over the entries in the hash table
46: @param h Pointer to the hash table [khash_t(name)*]
47: @param kvar Variable to which key will be assigned
48: @param vvar Variable to which value will be assigned
49: @param code Block of code to execute
50: */
51: #define kh_foreach(h, kvar, vvar, code) \
52: { \
53: khint_t __i; \
54: for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \
55: if (!kh_exist(h, __i)) continue; \
56: (kvar) = kh_key(h, __i); \
57: (vvar) = kh_val(h, __i); \
58: code; \
59: } \
60: }
61: #endif /*kh_foreach*/
63: #if !defined(kh_foreach_key)
64: /*! @function
65: @abstract Iterate over the keys in the hash table
66: @param h Pointer to the hash table [khash_t(name)*]
67: @param kvar Variable to which key will be assigned
68: @param code Block of code to execute
69: */
70: #define kh_foreach_key(h, kvar, code) \
71: { \
72: khint_t __i; \
73: for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \
74: if (!kh_exist(h, __i)) continue; \
75: (kvar) = kh_key(h, __i); \
76: code; \
77: } \
78: }
79: #endif /*kh_foreach_key*/
81: #if !defined(kh_foreach_value)
82: /*! @function
83: @abstract Iterate over the values in the hash table
84: @param h Pointer to the hash table [khash_t(name)*]
85: @param vvar Variable to which value will be assigned
86: @param code Block of code to execute
87: */
88: #define kh_foreach_value(h, vvar, code) \
89: { \
90: khint_t __i; \
91: for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \
92: if (!kh_exist(h, __i)) continue; \
93: (vvar) = kh_val(h, __i); \
94: code; \
95: } \
96: }
97: #endif /*kh_foreach_value*/
99: /* --- Helper macro for error checking --- */
101: #if defined(PETSC_USE_DEBUG)
103: #else
104: #define PetscHashAssert(expr) ((void)(expr))
105: #endif
107: /* --- Low level iterator API --- */
109: typedef khiter_t PetscHashIter;
111: #define PetscHashIterBegin(ht, i) \
112: do { \
113: (i) = kh_begin((ht)); \
114: if ((i) != kh_end((ht)) && !kh_exist((ht), (i))) PetscHashIterNext((ht), (i)); \
115: } while (0)
117: #define PetscHashIterNext(ht, i) \
118: do { \
119: ++(i); \
120: } while ((i) != kh_end((ht)) && !kh_exist((ht), (i)))
122: #define PetscHashIterAtEnd(ht, i) ((i) == kh_end((ht)))
124: #define PetscHashIterGetKey(ht, i, k) ((k) = kh_key((ht), (i)))
126: #define PetscHashIterGetVal(ht, i, v) ((v) = kh_val((ht), (i)))
128: #define PetscHashIterSetVal(ht, i, v) (kh_val((ht), (i)) = (v))
130: /* --- Thomas Wang integer hash functions --- */
132: typedef khint32_t PetscHash32_t;
133: typedef khint64_t PetscHash64_t;
134: typedef khint_t PetscHash_t;
136: /* Thomas Wang's first version for 32bit integers */
137: static inline PetscHash_t PetscHash_UInt32_v0(PetscHash32_t key)
138: {
139: key += ~(key << 15);
140: key ^= (key >> 10);
141: key += (key << 3);
142: key ^= (key >> 6);
143: key += ~(key << 11);
144: key ^= (key >> 16);
145: return key;
146: }
148: /* Thomas Wang's second version for 32bit integers */
149: static inline PetscHash_t PetscHash_UInt32_v1(PetscHash32_t key)
150: {
151: key = ~key + (key << 15); /* key = (key << 15) - key - 1; */
152: key = key ^ (key >> 12);
153: key = key + (key << 2);
154: key = key ^ (key >> 4);
155: key = key * 2057; /* key = (key + (key << 3)) + (key << 11); */
156: key = key ^ (key >> 16);
157: return key;
158: }
160: static inline PetscHash_t PetscHash_UInt32(PetscHash32_t key)
161: {
162: return PetscHash_UInt32_v1(key);
163: }
165: /* Thomas Wang's version for 64bit integer -> 32bit hash */
166: static inline PetscHash32_t PetscHash_UInt64_32(PetscHash64_t key)
167: {
168: key = ~key + (key << 18); /* key = (key << 18) - key - 1; */
169: key = key ^ (key >> 31);
170: key = key * 21; /* key = (key + (key << 2)) + (key << 4); */
171: key = key ^ (key >> 11);
172: key = key + (key << 6);
173: key = key ^ (key >> 22);
174: return (PetscHash32_t)key;
175: }
177: /* Thomas Wang's version for 64bit integer -> 64bit hash */
178: static inline PetscHash64_t PetscHash_UInt64_64(PetscHash64_t key)
179: {
180: key = ~key + (key << 21); /* key = (key << 21) - key - 1; */
181: key = key ^ (key >> 24);
182: key = key * 265; /* key = (key + (key << 3)) + (key << 8); */
183: key = key ^ (key >> 14);
184: key = key * 21; /* key = (key + (key << 2)) + (key << 4); */
185: key = key ^ (key >> 28);
186: key = key + (key << 31);
187: return key;
188: }
190: static inline PetscHash_t PetscHash_UInt64(PetscHash64_t key)
191: {
192: return sizeof(PetscHash_t) < sizeof(PetscHash64_t) ? (PetscHash_t)PetscHash_UInt64_32(key) : (PetscHash_t)PetscHash_UInt64_64(key);
193: }
195: static inline PetscHash_t PetscHashInt(PetscInt key)
196: {
197: #if defined(PETSC_USE_64BIT_INDICES)
198: return PetscHash_UInt64((PetscHash64_t)key);
199: #else
200: return PetscHash_UInt32((PetscHash32_t)key);
201: #endif
202: }
204: static inline PetscHash_t PetscHashPointer(void *key)
205: {
206: #if PETSC_SIZEOF_VOID_P == 8
207: return PetscHash_UInt64((PetscHash64_t)key);
208: #else
209: return PetscHash_UInt32((PetscHash32_t)key);
210: #endif
211: }
213: static inline PetscHash_t PetscHashCombine(PetscHash_t seed, PetscHash_t hash)
214: {
215: /* https://doi.org/10.1002/asi.10170 */
216: /* https://dl.acm.org/citation.cfm?id=759509 */
217: return seed ^ (hash + (seed << 6) + (seed >> 2));
218: }
220: #define PetscHashEqual(a, b) ((a) == (b))
222: #endif /* PETSC_HASHTABLE_H */