Actual source code: hashset.h
1: #if !defined(PETSC_HASHSET_H)
2: #define PETSC_HASHSET_H
4: #include <petsc/private/hashtable.h>
6: /*MC
7: PETSC_HASH_SET - Instantiate a PETSc hash table set type
9: Synopsis:
10: #include <petsc/private/hashset.h>
11: PETSC_HASH_SET(HSetT, KeyType, HashFunc, EqualFunc)
13: Input Parameters:
14: + HSetT - The hash table set type name suffix
15: . KeyType - The type of entries
16: . HashFunc - Routine or function-like macro computing hash values from entries
17: - EqualFunc - Routine or function-like macro computing whether two values are equal
19: Level: developer
21: .seealso: PetscHSetT, PetscHSetTCreate()
22: M*/
24: /*S
25: PetscHSetT - Hash table set
27: Synopsis:
28: typedef khash_t(HSetT) *PetscHSetT;
30: Level: developer
32: .seealso: PETSC_HASH_SET(), PetscHSetTCreate()
33: S*/
35: /*MC
36: PetscHSetTCreate - Create a hash table
38: Synopsis:
39: #include <petsc/private/hashset.h>
40: PetscErrorCode PetscHSetTCreate(PetscHSetT *ht)
42: Output Parameter:
43: . ht - The hash table
45: Level: developer
47: .seealso: PetscHSetTDestroy()
48: M*/
50: /*MC
51: PetscHSetTDestroy - Destroy a hash table
53: Synopsis:
54: #include <petsc/private/hashset.h>
55: PetscErrorCode PetscHSetTDestroy(PetscHSetT *ht)
57: Input Parameter:
58: . ht - The hash table
60: Level: developer
62: .seealso: PetscHSetTCreate()
63: M*/
65: /*MC
66: PetscHSetTReset - Reset a hash table
68: Synopsis:
69: #include <petsc/private/hashset.h>
70: PetscErrorCode PetscHSetTReset(PetscHSetT ht)
72: Input Parameter:
73: . ht - The hash table
75: Level: developer
77: .seealso: PetscHSetTClear()
78: M*/
80: /*MC
81: PetscHSetTDuplicate - Duplicate a hash table
83: Synopsis:
84: #include <petsc/private/hashset.h>
85: PetscErrorCode PetscHSetTDuplicate(PetscHSetT ht,PetscHSetT *hd)
87: Input Parameter:
88: . ht - The source hash table
90: Output Parameter:
91: . ht - The duplicated hash table
93: Level: developer
95: .seealso: PetscHSetTCreate()
96: M*/
98: /*MC
99: PetscHSetTUpdate - Add entries from a hash table to another
101: Synopsis:
102: #include <petsc/private/hashset.h>
103: PetscErrorCode PetscHSetTUpdate(PetscHSetT ht,PetscHSetT hda)
105: Input Parameters:
106: + ht - The hash table to which elements are added
107: - hta - The hash table from which the elements are retrieved
109: Output Parameter:
110: . ht - The hash table filled with the elements from the other hash table
112: Level: developer
114: .seealso: PetscHSetTCreate(), PetscHSetTDuplicate()
115: M*/
117: /*MC
118: PetscHSetTClear - Clear a hash table
120: Synopsis:
121: #include <petsc/private/hashset.h>
122: PetscErrorCode PetscHSetTClear(PetscHSetT ht)
124: Input Parameter:
125: . ht - The hash table
127: Level: developer
129: .seealso: PetscHSetTReset()
130: M*/
132: /*MC
133: PetscHSetTResize - Set the number of buckets in a hash table
135: Synopsis:
136: #include <petsc/private/hashset.h>
137: PetscErrorCode PetscHSetTResize(PetscHSetT ht,PetscInt nb)
139: Input Parameters:
140: + ht - The hash table
141: - nb - The number of buckets
143: Level: developer
145: .seealso: PetscHSetTCreate()
146: M*/
148: /*MC
149: PetscHSetTGetSize - Get the number of entries in a hash table
151: Synopsis:
152: #include <petsc/private/hashset.h>
153: PetscErrorCode PetscHSetTGetSize(PetscHSetT ht,PetscInt *n)
155: Input Parameter:
156: . ht - The hash table
158: Output Parameter:
159: . n - The number of entries
161: Level: developer
163: .seealso: PetscHSetTResize()
164: M*/
166: /*MC
167: PetscHSetTGetCapacity - Get the current size of the array in the hash table
169: Synopsis:
170: #include <petsc/private/hashset.h>
171: PetscErrorCode PetscHSetTGetCapacity(PetscHSetT ht,PetscInt *n)
173: Input Parameter:
174: . ht - The hash table
176: Output Parameter:
177: . n - The capacity
179: Level: developer
181: .seealso: PetscHSetTResize(), PetscHSetTGetSize()
182: M*/
184: /*MC
185: PetscHSetTHas - Query for an entry in the hash table
187: Synopsis:
188: #include <petsc/private/hashset.h>
189: PetscErrorCode PetscHSetTHas(PetscHSetT ht,KeyType key,PetscBool *has)
191: Input Parameters:
192: + ht - The hash table
193: - key - The entry
195: Output Parameter:
196: . has - Boolean indicating whether the entry is in the hash table
198: Level: developer
200: .seealso: PetscHSetTAdd(), PetscHSetTDel()
201: M*/
203: /*MC
204: PetscHSetTAdd - Set an entry in the hash table
206: Synopsis:
207: #include <petsc/private/hashset.h>
208: PetscErrorCode PetscHSetTAdd(PetscHSetT ht,KeyType key)
210: Input Parameters:
211: + ht - The hash table
212: - key - The entry
214: Level: developer
216: .seealso: PetscHSetTDel(), PetscHSetTHas()
217: M*/
219: /*MC
220: PetscHSetTDel - Remove an entry from the hash table
222: Synopsis:
223: #include <petsc/private/hashset.h>
224: PetscErrorCode PetscHSetTDel(PetscHSetT ht,KeyType key)
226: Input Parameters:
227: + ht - The hash table
228: - key - The entry
230: Level: developer
232: .seealso: PetscHSetTAdd(), PetscHSetTHas()
233: M*/
235: /*MC
236: PetscHSetTQueryAdd - Query and add an entry in the hash table
238: Synopsis:
239: #include <petsc/private/hashset.h>
240: PetscErrorCode PetscHSetTQueryAdd(PetscHSetT ht,KeyType key,PetscBool *missing)
242: Input Parameters:
243: + ht - The hash table
244: - key - The entry
246: Output Parameter:
247: . missing - Boolean indicating whether the entry was missing
249: Level: developer
251: .seealso: PetscHSetTQueryDel(), PetscHSetTAdd()
252: M*/
254: /*MC
255: PetscHSetTQueryDel - Query and remove an entry from the hash table
257: Synopsis:
258: #include <petsc/private/hashset.h>
259: PetscErrorCode PetscHSetTQueryDel(PetscHSetT ht,KeyType key,PetscBool *present)
261: Input Parameters:
262: + ht - The hash table
263: - key - The entry
265: Output Parameter:
266: . present - Boolean indicating whether the entry was present
268: Level: developer
270: .seealso: PetscHSetTQueryAdd(), PetscHSetTDel()
271: M*/
273: /*MC
274: PetscHSetTGetElems - Get all entries from a hash table
276: Synopsis:
277: #include <petsc/private/hashset.h>
278: PetscErrorCode PetscHSetTGetElems(PetscHSetT ht,PetscInt *off,KeyType array[])
280: Input Parameters:
281: + ht - The hash table
282: . off - Input offset in array (usually zero)
283: - array - Array where to put hash table entries into
285: Output Parameters:
286: + off - Output offset in array (output offset = input offset + hash table size)
287: - array - Array filled with the hash table entries
289: Level: developer
291: .seealso: PetscHSetTGetSize()
292: M*/
294: #define PETSC_HASH_SET(HashT, KeyType, HashFunc, EqualFunc) \
295: \
296: KHASH_INIT(HashT, KeyType, char, 0, HashFunc, EqualFunc) \
297: \
298: typedef khash_t(HashT) *Petsc##HashT; \
299: \
300: static inline PETSC_UNUSED \
301: PetscErrorCode Petsc##HashT##Create(Petsc##HashT *ht) \
302: { \
304: *ht = kh_init(HashT); \
305: return 0; \
306: } \
307: \
308: static inline PETSC_UNUSED \
309: PetscErrorCode Petsc##HashT##Destroy(Petsc##HashT *ht) \
310: { \
312: if (!*ht) return 0; \
313: kh_destroy(HashT,*ht); *ht = NULL; \
314: return 0; \
315: } \
316: \
317: static inline PETSC_UNUSED \
318: PetscErrorCode Petsc##HashT##Reset(Petsc##HashT ht) \
319: { \
321: kh_reset(HashT,ht); \
322: return 0; \
323: } \
324: \
325: static inline PETSC_UNUSED \
326: PetscErrorCode Petsc##HashT##Duplicate(Petsc##HashT ht,Petsc##HashT *hd) \
327: { \
328: int ret; \
329: KeyType key; \
332: *hd = kh_init(HashT); \
333: ret = kh_resize(HashT,*hd,kh_size(ht)); \
334: PetscHashAssert(ret==0); \
335: kh_foreach_key(ht,key,{ \
336: kh_put(HashT,*hd,key,&ret); \
337: PetscHashAssert(ret>=0);}) \
338: return 0; \
339: } \
340: \
341: static inline PETSC_UNUSED \
342: PetscErrorCode Petsc##HashT##Update(Petsc##HashT ht,Petsc##HashT hta) \
343: { \
344: int ret; \
345: KeyType key; \
348: kh_foreach_key(hta,key,{ \
349: kh_put(HashT,ht,key,&ret); \
350: PetscHashAssert(ret>=0);}) \
351: return 0; \
352: } \
353: \
354: static inline PETSC_UNUSED \
355: PetscErrorCode Petsc##HashT##Clear(Petsc##HashT ht) \
356: { \
358: kh_clear(HashT,ht); \
359: return 0; \
360: } \
361: \
362: static inline PETSC_UNUSED \
363: PetscErrorCode Petsc##HashT##Resize(Petsc##HashT ht,PetscInt nb) \
364: { \
365: int ret; \
367: ret = kh_resize(HashT,ht,(khint_t)nb); \
368: PetscHashAssert(ret==0); \
369: return 0; \
370: } \
371: \
372: static inline PETSC_UNUSED \
373: PetscErrorCode Petsc##HashT##GetSize(Petsc##HashT ht,PetscInt *n) \
374: { \
377: *n = (PetscInt)kh_size(ht); \
378: return 0; \
379: } \
380: \
381: static inline PETSC_UNUSED \
382: PetscErrorCode Petsc##HashT##GetCapacity(Petsc##HashT ht,PetscInt *n) \
383: { \
386: *n = (PetscInt)kh_n_buckets(ht); \
387: return 0; \
388: } \
389: \
390: static inline PETSC_UNUSED \
391: PetscErrorCode Petsc##HashT##Has(Petsc##HashT ht,KeyType key,PetscBool *has) \
392: { \
393: khiter_t iter; \
397: iter = kh_get(HashT,ht,key); \
398: *has = (iter != kh_end(ht)) ? PETSC_TRUE : PETSC_FALSE; \
399: return 0; \
400: } \
401: \
402: static inline PETSC_UNUSED \
403: PetscErrorCode Petsc##HashT##Add(Petsc##HashT ht,KeyType key) \
404: { \
405: int ret; \
406: khiter_t iter; \
409: iter = kh_put(HashT,ht,key,&ret); (void)iter; \
410: PetscHashAssert(ret>=0); \
411: return 0; \
412: } \
413: \
414: static inline PETSC_UNUSED \
415: PetscErrorCode Petsc##HashT##Del(Petsc##HashT ht,KeyType key) \
416: { \
417: khiter_t iter; \
420: iter = kh_get(HashT,ht,key); \
421: kh_del(HashT,ht,iter); \
422: return 0; \
423: } \
424: \
425: static inline PETSC_UNUSED \
426: PetscErrorCode Petsc##HashT##QueryAdd(Petsc##HashT ht,KeyType key,PetscBool *missing) \
427: { \
428: int ret; \
429: khiter_t iter; \
433: iter = kh_put(HashT,ht,key,&ret); (void)iter; \
434: PetscHashAssert(ret>=0); \
435: *missing = ret ? PETSC_TRUE : PETSC_FALSE; \
436: return 0; \
437: } \
438: \
439: static inline PETSC_UNUSED \
440: PetscErrorCode Petsc##HashT##QueryDel(Petsc##HashT ht,KeyType key,PetscBool *present) \
441: { \
442: khiter_t iter; \
446: iter = kh_get(HashT,ht,key); \
447: if (iter != kh_end(ht)) { \
448: kh_del(HashT,ht,iter); \
449: *present = PETSC_TRUE; \
450: } else { \
451: *present = PETSC_FALSE; \
452: } \
453: return 0; \
454: } \
455: \
456: static inline PETSC_UNUSED \
457: PetscErrorCode Petsc##HashT##GetElems(Petsc##HashT ht,PetscInt *off,KeyType array[]) \
458: { \
459: KeyType key; \
460: PetscInt pos; \
463: pos = *off; \
464: kh_foreach_key(ht,key,array[pos++] = key); \
465: *off = pos; \
466: return 0; \
467: } \
469: #endif /* PETSC_HASHSET_H */