forked from bartvdbraak/blender
- added BLI_ghash_size(), number of entries in table
- added GHashIterator ADT, for iterating over GHash-tables
This commit is contained in:
parent
8e9e9e6e35
commit
7f609ec2aa
@ -37,6 +37,7 @@
|
|||||||
|
|
||||||
struct GHash;
|
struct GHash;
|
||||||
typedef struct GHash GHash;
|
typedef struct GHash GHash;
|
||||||
|
typedef struct GHashIterator GHashIterator;
|
||||||
|
|
||||||
typedef unsigned int (*GHashHashFP) (void *key);
|
typedef unsigned int (*GHashHashFP) (void *key);
|
||||||
typedef int (*GHashCmpFP) (void *a, void *b);
|
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);
|
void* BLI_ghash_lookup (GHash *gh, void *key);
|
||||||
int BLI_ghash_haskey (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);
|
unsigned int BLI_ghashutil_ptrhash (void *key);
|
||||||
int BLI_ghashutil_ptrcmp (void *a, void *b);
|
int BLI_ghashutil_ptrcmp (void *a, void *b);
|
||||||
|
|
||||||
|
@ -139,6 +139,10 @@ int BLI_ghash_haskey(GHash *gh, void *key) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int BLI_ghash_size(GHash *gh) {
|
||||||
|
return gh->nentries;
|
||||||
|
}
|
||||||
|
|
||||||
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp) {
|
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp) {
|
||||||
int i;
|
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) {
|
unsigned int BLI_ghashutil_ptrhash(void *key) {
|
||||||
return (unsigned int) key;
|
return (unsigned int) key;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user