From 9bff19fbfd5292766671d2134a6442d5c3d7917a Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 22 Apr 2014 00:55:30 +1000 Subject: GHash, Edgehash: add lookup_default() funcs. Returns a fallback argument when the key isn't found. --- source/blender/blenlib/BLI_edgehash.h | 1 + source/blender/blenlib/BLI_ghash.h | 1 + source/blender/blenlib/intern/BLI_ghash.c | 10 ++++++++++ source/blender/blenlib/intern/edgehash.c | 10 ++++++++++ 4 files changed, 22 insertions(+) (limited to 'source/blender') diff --git a/source/blender/blenlib/BLI_edgehash.h b/source/blender/blenlib/BLI_edgehash.h index b4ca25bed4f..8e74ce3a9e3 100644 --- a/source/blender/blenlib/BLI_edgehash.h +++ b/source/blender/blenlib/BLI_edgehash.h @@ -53,6 +53,7 @@ void BLI_edgehash_free(EdgeHash *eh, EdgeHashFreeFP valfreefp); void BLI_edgehash_insert(EdgeHash *eh, unsigned int v0, unsigned int v1, void *val); bool BLI_edgehash_reinsert(EdgeHash *eh, unsigned int v0, unsigned int v1, void *val); void *BLI_edgehash_lookup(EdgeHash *eh, unsigned int v0, unsigned int v1) ATTR_WARN_UNUSED_RESULT; +void *BLI_edgehash_lookup_default(EdgeHash *eh, unsigned int v0, unsigned int v1, void *val_default) ATTR_WARN_UNUSED_RESULT; void **BLI_edgehash_lookup_p(EdgeHash *eh, unsigned int v0, unsigned int v1) ATTR_WARN_UNUSED_RESULT; bool BLI_edgehash_haskey(EdgeHash *eh, unsigned int v0, unsigned int v1) ATTR_WARN_UNUSED_RESULT; int BLI_edgehash_size(EdgeHash *eh) ATTR_WARN_UNUSED_RESULT; diff --git a/source/blender/blenlib/BLI_ghash.h b/source/blender/blenlib/BLI_ghash.h index d762876a55a..2aa79d6da8d 100644 --- a/source/blender/blenlib/BLI_ghash.h +++ b/source/blender/blenlib/BLI_ghash.h @@ -66,6 +66,7 @@ void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfre void BLI_ghash_insert(GHash *gh, void *key, void *val); bool BLI_ghash_reinsert(GHash *gh, void *key, void *val, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp); void *BLI_ghash_lookup(GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT; +void *BLI_ghash_lookup_default(GHash *gh, const void *key, void *val_default) ATTR_WARN_UNUSED_RESULT; void **BLI_ghash_lookup_p(GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT; bool BLI_ghash_remove(GHash *gh, void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp); void BLI_ghash_clear(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp); diff --git a/source/blender/blenlib/intern/BLI_ghash.c b/source/blender/blenlib/intern/BLI_ghash.c index 1df48885637..7b48744bcdf 100644 --- a/source/blender/blenlib/intern/BLI_ghash.c +++ b/source/blender/blenlib/intern/BLI_ghash.c @@ -383,6 +383,16 @@ void *BLI_ghash_lookup(GHash *gh, const void *key) return e ? e->val : NULL; } +/** + * A version of #BLI_ghash_lookup which accepts a fallback argument. + */ +void *BLI_ghash_lookup_default(GHash *gh, const void *key, void *val_default) +{ + Entry *e = ghash_lookup_entry(gh, key); + IS_GHASH_ASSERT(gh); + return e ? e->val : val_default; +} + /** * Lookup a pointer to the value of \a key in \a gh. * diff --git a/source/blender/blenlib/intern/edgehash.c b/source/blender/blenlib/intern/edgehash.c index 8558e5773c7..51a22cc46ab 100644 --- a/source/blender/blenlib/intern/edgehash.c +++ b/source/blender/blenlib/intern/edgehash.c @@ -355,6 +355,16 @@ void *BLI_edgehash_lookup(EdgeHash *eh, unsigned int v0, unsigned int v1) return e ? e->val : NULL; } +/** + * A version of #BLI_edgehash_lookup which accepts a fallback argument. + */ +void *BLI_edgehash_lookup_default(EdgeHash *eh, unsigned int v0, unsigned int v1, void *val_default) +{ + EdgeEntry *e = edgehash_lookup_entry(eh, v0, v1); + IS_EDGEHASH_ASSERT(eh); + return e ? e->val : val_default; +} + /** * Return boolean true/false if edge (v0,v1) in hash. */ -- cgit v1.2.3