72
63
struct Entry *curEntry;
75
GHash* BLI_ghash_new (GHashHashFP hashfp, GHashCmpFP cmpfp, const char *info);
76
void BLI_ghash_free (GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp);
78
//BM_INLINE void BLI_ghash_insert (GHash *gh, void *key, void *val);
79
//BM_INLINE int BLI_ghash_remove (GHash *gh, void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp);
80
//BM_INLINE void* BLI_ghash_lookup (GHash *gh, void *key);
81
//BM_INLINE int BLI_ghash_haskey (GHash *gh, void *key);
83
int BLI_ghash_size (GHash *gh);
68
GHash* BLI_ghash_new (GHashHashFP hashfp, GHashCmpFP cmpfp, const char *info);
69
void BLI_ghash_free (GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp);
70
void BLI_ghash_insert(GHash *gh, void *key, void *val);
71
void * BLI_ghash_lookup(GHash *gh, const void *key);
72
int BLI_ghash_remove(GHash *gh, void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp);
73
int BLI_ghash_haskey(GHash *gh, void *key);
74
int BLI_ghash_size (GHash *gh);
98
89
* be mutated while the iterator is in use, and the iterator will
99
90
* step exactly BLI_ghash_size(gh) times before becoming done.
101
* @param ghi The GHashIterator to initialize.
102
* @param gh The GHash to iterate over.
92
* \param ghi The GHashIterator to initialize.
93
* \param gh The GHash to iterate over.
104
95
void BLI_ghashIterator_init(GHashIterator *ghi, GHash *gh);
106
97
* Free a GHashIterator.
108
* @param ghi The iterator to free.
99
* \param ghi The iterator to free.
110
101
void BLI_ghashIterator_free (GHashIterator *ghi);
113
104
* Retrieve the key from an iterator.
115
* @param ghi The iterator.
116
* @return The key at the current index, or NULL if the
106
* \param ghi The iterator.
107
* \return The key at the current index, or NULL if the
117
108
* iterator is done.
119
110
void* BLI_ghashIterator_getKey (GHashIterator *ghi);
121
112
* Retrieve the value from an iterator.
123
* @param ghi The iterator.
124
* @return The value at the current index, or NULL if the
114
* \param ghi The iterator.
115
* \return The value at the current index, or NULL if the
125
116
* iterator is done.
127
118
void* BLI_ghashIterator_getValue (GHashIterator *ghi);
129
120
* Steps the iterator to the next index.
131
* @param ghi The iterator.
122
* \param ghi The iterator.
133
124
void BLI_ghashIterator_step (GHashIterator *ghi);
135
126
* Determine if an iterator is done (has reached the end of
136
127
* the hash table).
138
* @param ghi The iterator.
139
* @return True if done, False otherwise.
129
* \param ghi The iterator.
130
* \return True if done, False otherwise.
141
132
int BLI_ghashIterator_isDone (GHashIterator *ghi);
145
unsigned int BLI_ghashutil_ptrhash (void *key);
146
int BLI_ghashutil_ptrcmp (void *a, void *b);
148
unsigned int BLI_ghashutil_strhash (void *key);
149
int BLI_ghashutil_strcmp (void *a, void *b);
151
unsigned int BLI_ghashutil_inthash (void *ptr);
152
int BLI_ghashutil_intcmp(void *a, void *b);
154
/*begin of macro-inlined functions*/
155
extern unsigned int hashsizes[];
158
#define BLI_ghash_insert(gh, _k, _v){\
159
unsigned int _hash= (gh)->hashfp(_k)%gh->nbuckets;\
160
Entry *_e= BLI_mempool_alloc((gh)->entrypool);\
163
_e->next= (gh)->buckets[_hash];\
164
(gh)->buckets[_hash]= _e;\
165
if (++(gh)->nentries>(gh)->nbuckets*3) {\
166
Entry *_e, **_old= (gh)->buckets;\
167
int _i, _nold= (gh)->nbuckets;\
168
(gh)->nbuckets= hashsizes[++(gh)->cursize];\
169
(gh)->buckets= malloc((gh)->nbuckets*sizeof(*(gh)->buckets));\
170
memset((gh)->buckets, 0, (gh)->nbuckets*sizeof(*(gh)->buckets));\
171
for (_i=0; _i<_nold; _i++) {\
172
for (_e= _old[_i]; _e;) {\
173
Entry *_n= _e->next;\
174
_hash= (gh)->hashfp(_e->key)%(gh)->nbuckets;\
175
_e->next= (gh)->buckets[_hash];\
176
(gh)->buckets[_hash]= _e;\
183
/*---------inlined functions---------*/
184
BM_INLINE void BLI_ghash_insert(GHash *gh, void *key, void *val) {
185
unsigned int hash= gh->hashfp(key)%gh->nbuckets;
186
Entry *e= (Entry*) BLI_mempool_alloc(gh->entrypool);
190
e->next= gh->buckets[hash];
191
gh->buckets[hash]= e;
193
if (++gh->nentries>(float)gh->nbuckets/2) {
194
Entry *e, **old= gh->buckets;
195
int i, nold= gh->nbuckets;
197
gh->nbuckets= hashsizes[++gh->cursize];
198
gh->buckets= (Entry**)MEM_mallocN(gh->nbuckets*sizeof(*gh->buckets), "buckets");
199
memset(gh->buckets, 0, gh->nbuckets*sizeof(*gh->buckets));
201
for (i=0; i<nold; i++) {
202
for (e= old[i]; e;) {
205
hash= gh->hashfp(e->key)%gh->nbuckets;
206
e->next= gh->buckets[hash];
207
gh->buckets[hash]= e;
217
BM_INLINE void* BLI_ghash_lookup(GHash *gh, void *key)
220
unsigned int hash= gh->hashfp(key)%gh->nbuckets;
223
for (e= gh->buckets[hash]; e; e= e->next)
224
if (gh->cmpfp(key, e->key)==0)
230
BM_INLINE int BLI_ghash_remove (GHash *gh, void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
232
unsigned int hash= gh->hashfp(key)%gh->nbuckets;
236
for (e= gh->buckets[hash]; e; e= e->next) {
237
if (gh->cmpfp(key, e->key)==0) {
240
if (keyfreefp) keyfreefp(e->key);
241
if (valfreefp) valfreefp(e->val);
242
BLI_mempool_free(gh->entrypool, e);
249
gh->buckets[hash] = n;
260
BM_INLINE int BLI_ghash_haskey(GHash *gh, void *key) {
261
unsigned int hash= gh->hashfp(key)%gh->nbuckets;
264
for (e= gh->buckets[hash]; e; e= e->next)
265
if (gh->cmpfp(key, e->key)==0)
136
unsigned int BLI_ghashutil_ptrhash (const void *key);
137
int BLI_ghashutil_ptrcmp (const void *a, const void *b);
139
unsigned int BLI_ghashutil_strhash (const void *key);
140
int BLI_ghashutil_strcmp (const void *a, const void *b);
142
unsigned int BLI_ghashutil_inthash (const void *ptr);
143
int BLI_ghashutil_intcmp (const void *a, const void *b);
145
typedef struct GHashPair {
150
GHashPair* BLI_ghashutil_pairalloc (const void *first, const void *second);
151
unsigned int BLI_ghashutil_pairhash (const void *ptr);
152
int BLI_ghashutil_paircmp (const void *a, const void *b);
153
void BLI_ghashutil_pairfree (void *ptr);
271
155
#ifdef __cplusplus
159
#endif /* __BLI_GHASH_H__ */