From 7f609ec2aa5f3c4ec3635bf9ab3ac636089ec891 Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Wed, 7 Jan 2004 06:13:43 +0000 Subject: [PATCH] - added BLI_ghash_size(), number of entries in table - added GHashIterator ADT, for iterating over GHash-tables --- source/blender/blenlib/BLI_ghash.h | 54 +++++++++++++++++++++++ source/blender/blenlib/intern/BLI_ghash.c | 51 +++++++++++++++++++++ 2 files changed, 105 insertions(+) diff --git a/source/blender/blenlib/BLI_ghash.h b/source/blender/blenlib/BLI_ghash.h index 11dee5df57d..d2f2bcc95e4 100644 --- a/source/blender/blenlib/BLI_ghash.h +++ b/source/blender/blenlib/BLI_ghash.h @@ -37,6 +37,7 @@ struct GHash; typedef struct GHash GHash; +typedef struct GHashIterator GHashIterator; typedef unsigned int (*GHashHashFP) (void *key); typedef int (*GHashCmpFP) (void *a, void *b); @@ -50,6 +51,59 @@ void BLI_ghash_insert (GHash *gh, void *key, void *val); void* BLI_ghash_lookup (GHash *gh, void *key); int BLI_ghash_haskey (GHash *gh, void *key); +int BLI_ghash_size (GHash *gh); + +/* *** */ + + /** + * Create a new GHashIterator. The hash table must not be mutated + * while the iterator is in use, and the iterator will step exactly + * BLI_ghash_size(gh) times before becoming done. + * + * @param gh The GHash to iterate over. + * @return Pointer to a new DynStr. + */ +GHashIterator* BLI_ghashIterator_new (GHash *gh); + /** + * Free a GHashIterator. + * + * @param ghi The iterator to free. + */ +void BLI_ghashIterator_free (GHashIterator *ghi); + + /** + * Retrieve the key from an iterator. + * + * @param ghi The iterator. + * @return The key at the current index, or NULL if the + * iterator is done. + */ +void* BLI_ghashIterator_getKey (GHashIterator *ghi); + /** + * Retrieve the value from an iterator. + * + * @param ghi The iterator. + * @return The value at the current index, or NULL if the + * iterator is done. + */ +void* BLI_ghashIterator_getValue (GHashIterator *ghi); + /** + * Steps the iterator to the next index. + * + * @param ghi The iterator. + */ +void BLI_ghashIterator_step (GHashIterator *ghi); + /** + * Determine if an iterator is done (has reached the end of + * the hash table). + * + * @param ghi The iterator. + * @return True if done, False otherwise. + */ +int BLI_ghashIterator_isDone (GHashIterator *ghi); + +/* *** */ + unsigned int BLI_ghashutil_ptrhash (void *key); int BLI_ghashutil_ptrcmp (void *a, void *b); diff --git a/source/blender/blenlib/intern/BLI_ghash.c b/source/blender/blenlib/intern/BLI_ghash.c index bb7ef40ffb4..e523cdcdea9 100644 --- a/source/blender/blenlib/intern/BLI_ghash.c +++ b/source/blender/blenlib/intern/BLI_ghash.c @@ -139,6 +139,10 @@ int BLI_ghash_haskey(GHash *gh, void *key) { return 0; } +int BLI_ghash_size(GHash *gh) { + return gh->nentries; +} + void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp) { int i; @@ -162,6 +166,53 @@ void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreef /***/ +struct GHashIterator { + GHash *gh; + int curBucket; + Entry *curEntry; +}; + +GHashIterator *BLI_ghashIterator_new(GHash *gh) { + GHashIterator *ghi= malloc(sizeof(*ghi)); + ghi->gh= gh; + ghi->curEntry= NULL; + ghi->curBucket= -1; + while (!ghi->curEntry) { + ghi->curBucket++; + if (ghi->curBucket==ghi->gh->nbuckets) + break; + ghi->curEntry= ghi->gh->buckets[ghi->curBucket]; + } + return ghi; +} +void BLI_ghashIterator_free(GHashIterator *ghi) { + free(ghi); +} + +void *BLI_ghashIterator_getKey(GHashIterator *ghi) { + return ghi->curEntry?ghi->curEntry->key:NULL; +} +void *BLI_ghashIterator_getValue(GHashIterator *ghi) { + return ghi->curEntry?ghi->curEntry->val:NULL; +} + +void BLI_ghashIterator_step(GHashIterator *ghi) { + if (ghi->curEntry) { + ghi->curEntry= ghi->curEntry->next; + while (!ghi->curEntry) { + ghi->curBucket++; + if (ghi->curBucket==ghi->gh->nbuckets) + break; + ghi->curEntry= ghi->gh->buckets[ghi->curBucket]; + } + } +} +int BLI_ghashIterator_isDone(GHashIterator *ghi) { + return !ghi->curEntry; +} + +/***/ + unsigned int BLI_ghashutil_ptrhash(void *key) { return (unsigned int) key; }