From e70573badd22fc08b72c7c56f39293bc70d52c71 Mon Sep 17 00:00:00 2001 From: Martin Poirier Date: Tue, 27 May 2008 13:22:17 +0000 Subject: gHash iterator initializer To be able to have iterators on the stack/reuse iterators --- source/blender/blenlib/BLI_ghash.h | 16 +++++++++++++++- source/blender/blenlib/intern/BLI_ghash.c | 17 +++++++++++------ 2 files changed, 26 insertions(+), 7 deletions(-) (limited to 'source') diff --git a/source/blender/blenlib/BLI_ghash.h b/source/blender/blenlib/BLI_ghash.h index aec77f5f385..c77e82f0a2b 100644 --- a/source/blender/blenlib/BLI_ghash.h +++ b/source/blender/blenlib/BLI_ghash.h @@ -34,7 +34,12 @@ struct GHash; typedef struct GHash GHash; -typedef struct GHashIterator GHashIterator; + +typedef struct GHashIterator { + GHash *gh; + int curBucket; + struct Entry *curEntry; +} GHashIterator; typedef unsigned int (*GHashHashFP) (void *key); typedef int (*GHashCmpFP) (void *a, void *b); @@ -62,6 +67,15 @@ int BLI_ghash_size (GHash *gh); * @return Pointer to a new DynStr. */ GHashIterator* BLI_ghashIterator_new (GHash *gh); + /** + * Init an already allocated 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 ghi The GHashIterator to initialize. + * @param gh The GHash to iterate over. + */ +void BLI_ghashIterator_init(GHashIterator *ghi, GHash *gh); /** * Free a GHashIterator. * diff --git a/source/blender/blenlib/intern/BLI_ghash.c b/source/blender/blenlib/intern/BLI_ghash.c index 227cb8f5e9a..bae6ad428ea 100644 --- a/source/blender/blenlib/intern/BLI_ghash.c +++ b/source/blender/blenlib/intern/BLI_ghash.c @@ -198,12 +198,6 @@ 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; @@ -217,6 +211,17 @@ GHashIterator *BLI_ghashIterator_new(GHash *gh) { } return ghi; } +void BLI_ghashIterator_init(GHashIterator *ghi, GHash *gh) { + 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]; + } +} void BLI_ghashIterator_free(GHashIterator *ghi) { free(ghi); } -- cgit v1.2.3