Actual source code: hashset.h

petsc-3.11.3 2019-06-26
Report Typos and Errors
  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:   Concepts: hash table, set

 23: .keywords: hash table, set
 24: .seealso: PetscHSetT, PetscHSetTCreate()
 25: M*/

 27: /*S
 28:   PetscHSetT - Hash table set

 30:   Synopsis:
 31:   typedef khash_t(HSetT) *PetscHSetT;

 33:   Level: developer

 35:   Concepts: hash table, set

 37: .keywords: hash table, set
 38: .seealso:  PETSC_HASH_SET(), PetscHSetTCreate()
 39: S*/

 41: /*MC
 42:   PetscHSetTCreate - Create a hash table

 44:   Synopsis:
 45:   #include <petsc/private/hashset.h>
 46:   PetscErrorCode PetscHSetTCreate(PetscHSetT *ht)

 48:   Output Parameter:
 49: . ht - The hash table

 51:   Level: developer

 53:   Concepts: hash table, set

 55: .keywords: hash table, set, create
 56: .seealso: PetscHSetTDestroy()
 57: M*/

 59: /*MC
 60:   PetscHSetTDestroy - Destroy a hash table

 62:   Synopsis:
 63:   #include <petsc/private/hashset.h>
 64:   PetscErrorCode PetscHSetTDestroy(PetscHSetT *ht)

 66:   Input Parameter:
 67: . ht - The hash table

 69:   Level: developer

 71:   Concepts: hash table, set

 73: .keywords: hash table, set, destroy
 74: .seealso: PetscHSetTCreate()
 75: M*/

 77: /*MC
 78:   PetscHSetTReset - Reset a hash table

 80:   Synopsis:
 81:   #include <petsc/private/hashset.h>
 82:   PetscErrorCode PetscHSetTReset(PetscHSetT ht)

 84:   Input Parameter:
 85: . ht - The hash table

 87:   Level: developer

 89:   Concepts: hash table, set

 91: .keywords: hash table, set, reset
 92: .seealso: PetscHSetTClear()
 93: M*/

 95: /*MC
 96:   PetscHSetTDuplicate - Duplicate a hash table

 98:   Synopsis:
 99:   #include <petsc/private/hashset.h>
100:   PetscErrorCode PetscHSetTDuplicate(PetscHSetT ht,PetscHSetT *hd)

102:   Input Parameter:
103: . ht - The source hash table

105:   Output Parameter:
106: . ht - The duplicated hash table

108:   Level: developer

110:   Concepts: hash table, set

112: .keywords: hash table, set, duplicate
113: .seealso: PetscHSetTCreate()
114: M*/

116: /*MC
117:   PetscHSetTClear - Clear a hash table

119:   Synopsis:
120:   #include <petsc/private/hashset.h>
121:   PetscErrorCode PetscHSetTClear(PetscHSetT ht)

123:   Input Parameter:
124: . ht - The hash table

126:   Level: developer

128:   Concepts: hash table, set

130: .keywords: hash table, set, reset
131: .seealso: PetscHSetTReset()
132: M*/

134: /*MC
135:   PetscHSetTResize - Set the number of buckets in a hash table

137:   Synopsis:
138:   #include <petsc/private/hashset.h>
139:   PetscErrorCode PetscHSetTResize(PetscHSetT ht,PetscInt nb)

141:   Input Parameters:
142: + ht - The hash table
143: - nb - The number of buckets

145:   Level: developer

147:   Concepts: hash table, set

149: .seealso: PetscHSetTCreate()
150: M*/

152: /*MC
153:   PetscHSetTGetSize - Get the number of entries in a hash table

155:   Synopsis:
156:   #include <petsc/private/hashset.h>
157:   PetscErrorCode PetscHSetTGetSize(PetscHSetT ht,PetscInt *n)

159:   Input Parameter:
160: . ht - The hash table

162:   Output Parameter:
163: . n - The number of entries

165:   Level: developer

167:   Concepts: hash table, set

169: .keywords: hash table, set, resize
170: .seealso: PetscHSetTResize()
171: M*/

173: /*MC
174:   PetscHSetTHas - Query for an entry in the hash table

176:   Synopsis:
177:   #include <petsc/private/hashset.h>
178:   PetscErrorCode PetscHSetTHas(PetscHSetT ht,KeyType key,PetscBool *has)

180:   Input Parameters:
181: + ht  - The hash table
182: - key - The entry

184:   Output Parameter:
185: . has - Boolean indicating whether the entry is in the hash table

187:   Level: developer

189:   Concepts: hash table, set

191: .keywords: hash table, set, query
192: .seealso:  PetscHSetTAdd(), PetscHSetTDel()
193: M*/

195: /*MC
196:   PetscHSetTAdd - Set an entry in the hash table

198:   Synopsis:
199:   #include <petsc/private/hashset.h>
200:   PetscErrorCode PetscHSetTAdd(PetscHSetT ht,KeyType key)

202:   Input Parameters:
203: + ht  - The hash table
204: - key - The entry

206:   Level: developer

208:   Concepts: hash table, set

210: .keywords: hash table, set, add
211: .seealso: PetscHSetTDel(), PetscHSetTHas()
212: M*/

214: /*MC
215:   PetscHSetTDel - Remove an entry from the hash table

217:   Synopsis:
218:   #include <petsc/private/hashset.h>
219:   PetscErrorCode PetscHSetTDel(PetscHSetT ht,KeyType key)

221:   Input Parameters:
222: + ht  - The hash table
223: - key - The entry

225:   Level: developer

227:   Concepts: hash table, set

229: .keywords: hash table, set, del
230: .seealso: PetscHSetTAdd(), PetscHSetTHas()
231: M*/

233: /*MC
234:   PetscHSetTQueryAdd - Query and add an entry in the hash table

236:   Synopsis:
237:   #include <petsc/private/hashset.h>
238:   PetscErrorCode PetscHSetTQueryAdd(PetscHSetT ht,KeyType key,PetscBool *missing)

240:   Input Parameters:
241: + ht  - The hash table
242: - key - The entry

244:   Output Parameter:
245: . missing - Boolean indicating whether the entry was missing

247:   Level: developer

249:   Concepts: hash table, set

251: .keywords: hash table, set, query, set
252: .seealso: PetscHSetTQueryDel(), PetscHSetTAdd()
253: M*/

255: /*MC
256:   PetscHSetTQueryDel - Query and remove an entry from the hash table

258:   Synopsis:
259:   #include <petsc/private/hashset.h>
260:   PetscErrorCode PetscHSetTQueryDel(PetscHSetT ht,KeyType key,PetscBool *present)

262:   Input Parameters:
263: + ht  - The hash table
264: - key - The entry

266:   Output Parameter:
267: . present - Boolean indicating whether the entry was present

269:   Level: developer

271:   Concepts: hash table, set

273: .keywords: hash table, set, query, del
274: .seealso: PetscHSetTQueryAdd(), PetscHSetTDel()
275: M*/

277: /*MC
278:   PetscHSetTGetElems - Get all entries from a hash table

280:   Synopsis:
281:   #include <petsc/private/hashset.h>
282:   PetscErrorCode PetscHSetTGetElems(PetscHSetT ht,PetscInt *off,KeyType array[])

284:   Input Parameters:
285: + ht    - The hash table
286: . off   - Input offset in array (usually zero)
287: - array - Array where to put hash table entries into

289:   Output Parameter:
290: + off   - Output offset in array (output offset = input offset + hash table size)
291: - array - Array filled with the hash table entries

293:   Level: developer

295:   Concepts: hash table, set

297: .keywords: hash table, set, array
298: .seealso: PetscHSetTGetSize()
299: M*/

301: #define PETSC_HASH_SET(HashT, KeyType, HashFunc, EqualFunc)                                          \
302:                                                                                                      \
303: KHASH_INIT(HashT, KeyType, char, 0, HashFunc, EqualFunc)                                             \
304:                                                                                                      \
305: typedef khash_t(HashT) *Petsc##HashT;                                                                \
306:                                                                                                      \
307: PETSC_STATIC_INLINE PETSC_UNUSED                                                                     \
308: PetscErrorCode Petsc##HashT##Create(Petsc##HashT *ht)                                                \
309: {                                                                                                    \
312:   *ht = kh_init(HashT);                                                                              \
313:   return(0);                                                                            \
314: }                                                                                                    \
315:                                                                                                      \
316: PETSC_STATIC_INLINE PETSC_UNUSED                                                                     \
317: PetscErrorCode Petsc##HashT##Destroy(Petsc##HashT *ht)                                               \
318: {                                                                                                    \
321:   if (!*ht) return(0);                                                                  \
322:   kh_destroy(HashT,*ht); *ht = NULL;                                                                 \
323:   return(0);                                                                            \
324: }                                                                                                    \
325:                                                                                                      \
326: PETSC_STATIC_INLINE PETSC_UNUSED                                                                     \
327: PetscErrorCode Petsc##HashT##Reset(Petsc##HashT ht)                                                  \
328: {                                                                                                    \
331:   kh_reset(HashT,ht);                                                                                \
332:   return(0);                                                                            \
333: }                                                                                                    \
334:                                                                                                      \
335: PETSC_STATIC_INLINE PETSC_UNUSED                                                                     \
336: PetscErrorCode Petsc##HashT##Duplicate(Petsc##HashT ht,Petsc##HashT *hd)                             \
337: {                                                                                                    \
338:   int     ret;                                                                                       \
339:   KeyType key;                                                                                       \
343:   *hd = kh_init(HashT);                                                                              \
344:   ret = kh_resize(HashT,*hd,kh_size(ht));                                                            \
345:   PetscHashAssert(ret==0);                                                                           \
346:   kh_foreach_key(ht,key,{                                                                            \
347:       kh_put(HashT,*hd,key,&ret);                                                                    \
348:       PetscHashAssert(ret>=0);})                                                                     \
349:   return(0);                                                                            \
350: }                                                                                                    \
351:                                                                                                      \
352: PETSC_STATIC_INLINE PETSC_UNUSED                                                                     \
353: PetscErrorCode Petsc##HashT##Clear(Petsc##HashT ht)                                                  \
354: {                                                                                                    \
357:   kh_clear(HashT,ht);                                                                                \
358:   return(0);                                                                            \
359: }                                                                                                    \
360:                                                                                                      \
361: PETSC_STATIC_INLINE PETSC_UNUSED                                                                     \
362: PetscErrorCode Petsc##HashT##Resize(Petsc##HashT ht,PetscInt nb)                                     \
363: {                                                                                                    \
364:   int ret;                                                                                           \
367:   ret = kh_resize(HashT,ht,(khint_t)nb);                                                             \
368:   PetscHashAssert(ret==0);                                                                           \
369:   return(0);                                                                            \
370: }                                                                                                    \
371:                                                                                                      \
372: PETSC_STATIC_INLINE PETSC_UNUSED                                                                     \
373: PetscErrorCode Petsc##HashT##GetSize(Petsc##HashT ht,PetscInt *n)                                    \
374: {                                                                                                    \
378:   *n = (PetscInt)kh_size(ht);                                                                        \
379:   return(0);                                                                            \
380: }                                                                                                    \
381:                                                                                                      \
382: PETSC_STATIC_INLINE PETSC_UNUSED                                                                     \
383: PetscErrorCode Petsc##HashT##Has(Petsc##HashT ht,KeyType key,PetscBool *has)                         \
384: {                                                                                                    \
385:   khiter_t iter;                                                                                     \
389:   iter = kh_get(HashT,ht,key);                                                                       \
390:   *has = (iter != kh_end(ht)) ? PETSC_TRUE : PETSC_FALSE;                                            \
391:   return(0);                                                                            \
392: }                                                                                                    \
393:                                                                                                      \
394: PETSC_STATIC_INLINE PETSC_UNUSED                                                                     \
395: PetscErrorCode Petsc##HashT##Add(Petsc##HashT ht,KeyType key)                                        \
396: {                                                                                                    \
397:   int      ret;                                                                                      \
398:   khiter_t iter;                                                                                     \
401:   iter = kh_put(HashT,ht,key,&ret); (void)iter;                                                      \
402:   PetscHashAssert(ret>=0);                                                                           \
403:   return(0);                                                                            \
404: }                                                                                                    \
405:                                                                                                      \
406: PETSC_STATIC_INLINE PETSC_UNUSED                                                                     \
407: PetscErrorCode Petsc##HashT##Del(Petsc##HashT ht,KeyType key)                                        \
408: {                                                                                                    \
409:   khiter_t iter;                                                                                     \
412:   iter = kh_get(HashT,ht,key);                                                                       \
413:   kh_del(HashT,ht,iter);                                                                             \
414:   return(0);                                                                            \
415: }                                                                                                    \
416:                                                                                                      \
417: PETSC_STATIC_INLINE PETSC_UNUSED                                                                     \
418: PetscErrorCode Petsc##HashT##QueryAdd(Petsc##HashT ht,KeyType key,PetscBool *missing)                \
419: {                                                                                                    \
420:   int      ret;                                                                                      \
421:   khiter_t iter;                                                                                     \
425:   iter = kh_put(HashT,ht,key,&ret); (void)iter;                                                      \
426:   PetscHashAssert(ret>=0);                                                                           \
427:   *missing = ret ? PETSC_TRUE : PETSC_FALSE;                                                         \
428:   return(0);                                                                            \
429: }                                                                                                    \
430:                                                                                                      \
431: PETSC_STATIC_INLINE PETSC_UNUSED                                                                     \
432: PetscErrorCode Petsc##HashT##QueryDel(Petsc##HashT ht,KeyType key,PetscBool *present)                \
433: {                                                                                                    \
434:   khiter_t iter;                                                                                     \
438:   iter = kh_get(HashT,ht,key);                                                                       \
439:   if (iter != kh_end(ht)) {                                                                          \
440:     kh_del(HashT,ht,iter);                                                                           \
441:     *present = PETSC_TRUE;                                                                           \
442:   } else {                                                                                           \
443:     *present = PETSC_FALSE;                                                                          \
444:   }                                                                                                  \
445:   return(0);                                                                            \
446: }                                                                                                    \
447:                                                                                                      \
448: PETSC_STATIC_INLINE PETSC_UNUSED                                                                     \
449: PetscErrorCode Petsc##HashT##GetElems(Petsc##HashT ht,PetscInt *off,KeyType array[])                 \
450: {                                                                                                    \
451:   KeyType  key;                                                                                      \
452:   PetscInt pos;                                                                                      \
456:   pos = *off;                                                                                        \
457:   kh_foreach_key(ht,key,array[pos++] = key);                                                         \
458:   *off = pos;                                                                                        \
459:   return(0);                                                                            \
460: }                                                                                                    \

462: #endif /* _PETSC_HASHSET_H */