Cleanup: sync edgehash w/ ghash remove logic

This commit is contained in:
Campbell Barton
2015-11-29 17:49:56 +11:00
parent 09c2bff32f
commit 3e499c1a31
2 changed files with 42 additions and 25 deletions

View File

@@ -378,13 +378,12 @@ BLI_INLINE Entry *ghash_lookup_entry_ex(
* Useful when modifying buckets somehow (like removing an entry...). * Useful when modifying buckets somehow (like removing an entry...).
*/ */
BLI_INLINE Entry *ghash_lookup_entry_prev_ex( BLI_INLINE Entry *ghash_lookup_entry_prev_ex(
GHash *gh, const void *key, Entry **r_e_prev, const unsigned int bucket_index) GHash *gh, const void *key,
Entry **r_e_prev, const unsigned int bucket_index)
{ {
Entry *e, *e_prev = NULL;
/* If we do not store GHash, not worth computing it for each entry here! /* If we do not store GHash, not worth computing it for each entry here!
* Typically, comparison function will be quicker, and since it's needed in the end anyway... */ * Typically, comparison function will be quicker, and since it's needed in the end anyway... */
for (e = gh->buckets[bucket_index]; e; e_prev = e, e = e->next) { for (Entry *e_prev = NULL, *e = gh->buckets[bucket_index]; e; e_prev = e, e = e->next) {
if (UNLIKELY(gh->cmpfp(key, e->key) == false)) { if (UNLIKELY(gh->cmpfp(key, e->key) == false)) {
*r_e_prev = e_prev; *r_e_prev = e_prev;
return e; return e;

View File

@@ -161,6 +161,27 @@ BLI_INLINE EdgeEntry *edgehash_lookup_entry_ex(
return NULL; return NULL;
} }
/**
* Internal lookup function, returns previous entry of target one too.
* Takes bucket_index argument to avoid calling #edgehash_bucket_index multiple times.
* Useful when modifying buckets somehow (like removing an entry...).
*/
BLI_INLINE EdgeEntry *edgehash_lookup_entry_prev_ex(
EdgeHash *eh, unsigned int v0, unsigned int v1,
EdgeEntry **r_e_prev, const unsigned int bucket_index)
{
BLI_assert(v0 < v1);
for (EdgeEntry *e_prev = NULL, *e = eh->buckets[bucket_index]; e; e = e->next) {
if (UNLIKELY(v0 == e->v0 && v1 == e->v1)) {
*r_e_prev = e_prev;
return e;
}
}
*r_e_prev = NULL;
return NULL;
}
/** /**
* Internal lookup function. Only wraps #edgehash_lookup_entry_ex * Internal lookup function. Only wraps #edgehash_lookup_entry_ex
*/ */
@@ -282,38 +303,35 @@ BLI_INLINE void edgehash_insert(EdgeHash *eh, unsigned int v0, unsigned int v1,
/** /**
* Remove the entry and return it, caller must free from eh->epool. * Remove the entry and return it, caller must free from eh->epool.
*/ */
static EdgeEntry *edgehash_remove_ex( BLI_INLINE EdgeEntry *edgehash_remove_ex(
EdgeHash *eh, unsigned int v0, unsigned int v1, EdgeHash *eh, unsigned int v0, unsigned int v1,
EdgeHashFreeFP valfreefp, EdgeHashFreeFP valfreefp,
const unsigned int bucket_index) const unsigned int bucket_index)
{ {
EdgeEntry *e; EdgeEntry *e_prev;
EdgeEntry *e_prev = NULL; EdgeEntry *e = edgehash_lookup_entry_prev_ex(eh, v0, v1, &e_prev, bucket_index);
BLI_assert(v0 < v1); BLI_assert(v0 < v1);
for (e = eh->buckets[bucket_index]; e; e = e->next) { if (e) {
if (UNLIKELY(v0 == e->v0 && v1 == e->v1)) { EdgeEntry *e_next = e->next;
EdgeEntry *e_next = e->next;
if (valfreefp) { if (valfreefp) {
valfreefp(e->val); valfreefp(e->val);
}
if (e_prev) {
e_prev->next = e_next;
}
else {
eh->buckets[bucket_index] = e_next;
}
eh->nentries--;
return e;
} }
e_prev = e;
if (e_prev) {
e_prev->next = e_next;
}
else {
eh->buckets[bucket_index] = e_next;
}
eh->nentries--;
return e;
} }
return NULL; return e;
} }
/** /**