GHash/Edgehash: make simple iterator checking functions inline.

also remove NULL check, only a few areas made use of this.
This commit is contained in:
Campbell Barton 2014-04-08 15:50:38 +10:00
parent ebaf3781fa
commit 5580afb5df
Notes: blender-bot 2023-02-14 10:47:15 +01:00
Referenced by issue #39793, Cycles shading failure; major blowout when using an input in a certain setup on recent builds
6 changed files with 113 additions and 70 deletions

View File

@ -32,9 +32,13 @@
#include "BLI_compiler_attrs.h"
struct EdgeHash;
struct EdgeHashIterator;
typedef struct EdgeHash EdgeHash;
typedef struct EdgeHashIterator EdgeHashIterator;
typedef struct EdgeHashIterator {
EdgeHash *eh;
struct EdgeEntry *curEntry;
unsigned int curBucket;
} EdgeHashIterator;
typedef void (*EdgeHashFreeFP)(void *key);
@ -59,13 +63,29 @@ void BLI_edgehash_flag_set(EdgeHash *eh, unsigned int flag);
void BLI_edgehash_flag_clear(EdgeHash *eh, unsigned int flag);
EdgeHashIterator *BLI_edgehashIterator_new(EdgeHash *eh) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT;
void BLI_edgehashIterator_init(EdgeHashIterator *ehi, EdgeHash *eh);
void BLI_edgehashIterator_free(EdgeHashIterator *ehi);
void BLI_edgehashIterator_getKey(EdgeHashIterator *ehi, unsigned int *r_v0, unsigned int *r_v1);
void *BLI_edgehashIterator_getValue(EdgeHashIterator *ehi) ATTR_WARN_UNUSED_RESULT;
void **BLI_edgehashIterator_getValue_p(EdgeHashIterator *ehi) ATTR_WARN_UNUSED_RESULT;
void BLI_edgehashIterator_setValue(EdgeHashIterator *ehi, void *val);
void BLI_edgehashIterator_step(EdgeHashIterator *ehi);
bool BLI_edgehashIterator_isDone(EdgeHashIterator *ehi) ATTR_WARN_UNUSED_RESULT;
BLI_INLINE bool BLI_edgehashIterator_isDone(EdgeHashIterator *ehi) ATTR_WARN_UNUSED_RESULT;
BLI_INLINE void BLI_edgehashIterator_getKey(EdgeHashIterator *ehi, unsigned int *r_v0, unsigned int *r_v1);
BLI_INLINE void *BLI_edgehashIterator_getValue(EdgeHashIterator *ehi) ATTR_WARN_UNUSED_RESULT;
BLI_INLINE void **BLI_edgehashIterator_getValue_p(EdgeHashIterator *ehi) ATTR_WARN_UNUSED_RESULT;
BLI_INLINE void BLI_edgehashIterator_setValue(EdgeHashIterator *ehi, void *val);
struct _eh_Entry { void *next; unsigned int v0, v1; void *val; };
BLI_INLINE void BLI_edgehashIterator_getKey(EdgeHashIterator *ehi, unsigned int *r_v0, unsigned int *r_v1)
{ *r_v0 = ((struct _eh_Entry *)ehi->curEntry)->v0; *r_v1 = ((struct _eh_Entry *)ehi->curEntry)->v1; }
BLI_INLINE void *BLI_edgehashIterator_getValue(EdgeHashIterator *ehi) { return ((struct _eh_Entry *)ehi->curEntry)->val; }
BLI_INLINE void **BLI_edgehashIterator_getValue_p(EdgeHashIterator *ehi) { return &((struct _eh_Entry *)ehi->curEntry)->val; }
BLI_INLINE void BLI_edgehashIterator_setValue(EdgeHashIterator *ehi, void *val) { ((struct _eh_Entry *)ehi->curEntry)->val = val; }
BLI_INLINE bool BLI_edgehashIterator_isDone(EdgeHashIterator *ehi) { return (((struct _eh_Entry *)ehi->curEntry) == NULL); }
/* disallow further access */
#ifdef __GNUC__
# pragma GCC poison _eh_Entry
#else
# define _eh_Entry void
#endif
#define BLI_EDGEHASH_SIZE_GUESS_FROM_LOOPS(totloop) ((totloop) / 2)
#define BLI_EDGEHASH_SIZE_GUESS_FROM_POLYS(totpoly) ((totpoly) * 2)

View File

@ -83,13 +83,24 @@ GHashIterator *BLI_ghashIterator_new(GHash *gh) ATTR_MALLOC ATTR_WARN_UNUSED_RES
void BLI_ghashIterator_init(GHashIterator *ghi, GHash *gh);
void BLI_ghashIterator_free(GHashIterator *ghi);
void *BLI_ghashIterator_getKey(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT;
void *BLI_ghashIterator_getValue(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT;
void **BLI_ghashIterator_getValue_p(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT;
void BLI_ghashIterator_step(GHashIterator *ghi);
bool BLI_ghashIterator_done(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT;
BLI_INLINE void *BLI_ghashIterator_getKey(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT;
BLI_INLINE void *BLI_ghashIterator_getValue(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT;
BLI_INLINE void **BLI_ghashIterator_getValue_p(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT;
BLI_INLINE bool BLI_ghashIterator_done(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT;
struct _gh_Entry { void *next, *key, *val; };
BLI_INLINE void *BLI_ghashIterator_getKey(GHashIterator *ghi) { return ((struct _gh_Entry *)ghi->curEntry)->key; }
BLI_INLINE void *BLI_ghashIterator_getValue(GHashIterator *ghi) { return ((struct _gh_Entry *)ghi->curEntry)->val; }
BLI_INLINE void **BLI_ghashIterator_getValue_p(GHashIterator *ghi) { return &((struct _gh_Entry *)ghi->curEntry)->val; }
BLI_INLINE bool BLI_ghashIterator_done(GHashIterator *ghi) { return !ghi->curEntry; }
/* disallow further access */
#ifdef __GNUC__
# pragma GCC poison _gh_Entry
#else
# define _gh_Entry void
#endif
#define GHASH_ITER(gh_iter_, ghash_) \
for (BLI_ghashIterator_init(&gh_iter_, ghash_); \

View File

@ -577,6 +577,8 @@ void BLI_ghashIterator_free(GHashIterator *ghi)
MEM_freeN(ghi);
}
/* inline functions now */
#if 0
/**
* Retrieve the key from an iterator.
*
@ -586,7 +588,7 @@ void BLI_ghashIterator_free(GHashIterator *ghi)
*/
void *BLI_ghashIterator_getKey(GHashIterator *ghi)
{
return ghi->curEntry ? ghi->curEntry->key : NULL;
return ghi->curEntry->key;
}
/**
@ -598,7 +600,7 @@ void *BLI_ghashIterator_getKey(GHashIterator *ghi)
*/
void *BLI_ghashIterator_getValue(GHashIterator *ghi)
{
return ghi->curEntry ? ghi->curEntry->val : NULL;
return ghi->curEntry->val;
}
/**
@ -610,9 +612,22 @@ void *BLI_ghashIterator_getValue(GHashIterator *ghi)
*/
void **BLI_ghashIterator_getValue_p(GHashIterator *ghi)
{
return ghi->curEntry ? &ghi->curEntry->val : NULL;
return &ghi->curEntry->val;
}
/**
* Determine if an iterator is done (has reached the end of
* the hash table).
*
* \param ghi The iterator.
* \return True if done, False otherwise.
*/
bool BLI_ghashIterator_done(GHashIterator *ghi)
{
return ghi->curEntry == NULL;
}
#endif
/**
* Steps the iterator to the next index.
*
@ -631,18 +646,6 @@ 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.
*/
bool BLI_ghashIterator_done(GHashIterator *ghi)
{
return ghi->curEntry == NULL;
}
/** \} */

View File

@ -427,13 +427,6 @@ void BLI_edgehash_flag_clear(EdgeHash *eh, unsigned int flag)
/** \name Iterator API
* \{ */
struct EdgeHashIterator {
EdgeHash *eh;
unsigned int curBucket;
EdgeEntry *curEntry;
};
/**
* Create a new EdgeHashIterator. The hash table must not be mutated
* while the iterator is in use, and the iterator will step exactly
@ -442,6 +435,20 @@ struct EdgeHashIterator {
EdgeHashIterator *BLI_edgehashIterator_new(EdgeHash *eh)
{
EdgeHashIterator *ehi = MEM_mallocN(sizeof(*ehi), "eh iter");
BLI_edgehashIterator_init(ehi, eh);
return ehi;
}
/**
* Init an already allocated EdgeHashIterator. The hash table must not
* be mutated while the iterator is in use, and the iterator will
* step exactly BLI_edgehash_size(eh) times before becoming done.
*
* \param ehi The EdgeHashIterator to initialize.
* \param eh The EdgeHash to iterate over.
*/
void BLI_edgehashIterator_init(EdgeHashIterator *ehi, EdgeHash *eh)
{
ehi->eh = eh;
ehi->curEntry = NULL;
ehi->curBucket = UINT_MAX; /* wraps to zero */
@ -451,7 +458,6 @@ EdgeHashIterator *BLI_edgehashIterator_new(EdgeHash *eh)
break;
ehi->curEntry = ehi->eh->buckets[ehi->curBucket];
}
return ehi;
}
/**
@ -462,15 +468,15 @@ void BLI_edgehashIterator_free(EdgeHashIterator *ehi)
MEM_freeN(ehi);
}
/* inline functions now */
#if 0
/**
* Retrieve the key from an iterator.
*/
void BLI_edgehashIterator_getKey(EdgeHashIterator *ehi, unsigned int *r_v0, unsigned int *r_v1)
{
if (ehi->curEntry) {
*r_v0 = ehi->curEntry->v0;
*r_v1 = ehi->curEntry->v1;
}
*r_v0 = ehi->curEntry->v0;
*r_v1 = ehi->curEntry->v1;
}
/**
@ -478,7 +484,7 @@ void BLI_edgehashIterator_getKey(EdgeHashIterator *ehi, unsigned int *r_v0, unsi
*/
void *BLI_edgehashIterator_getValue(EdgeHashIterator *ehi)
{
return ehi->curEntry ? ehi->curEntry->val : NULL;
return ehi->curEntry->val;
}
/**
@ -486,7 +492,7 @@ void *BLI_edgehashIterator_getValue(EdgeHashIterator *ehi)
*/
void **BLI_edgehashIterator_getValue_p(EdgeHashIterator *ehi)
{
return ehi->curEntry ? &ehi->curEntry->val : NULL;
return &ehi->curEntry->val;
}
/**
@ -494,11 +500,18 @@ void **BLI_edgehashIterator_getValue_p(EdgeHashIterator *ehi)
*/
void BLI_edgehashIterator_setValue(EdgeHashIterator *ehi, void *val)
{
if (ehi->curEntry) {
ehi->curEntry->val = val;
}
ehi->curEntry->val = val;
}
/**
* Determine if an iterator is done.
*/
bool BLI_edgehashIterator_isDone(EdgeHashIterator *ehi)
{
return (ehi->curEntry == NULL);
}
#endif
/**
* Steps the iterator to the next index.
*/
@ -517,14 +530,6 @@ void BLI_edgehashIterator_step(EdgeHashIterator *ehi)
}
}
/**
* Determine if an iterator is done.
*/
bool BLI_edgehashIterator_isDone(EdgeHashIterator *ehi)
{
return (ehi->curEntry == NULL);
}
/** \} */
/* -------------------------------------------------------------------- */

View File

@ -347,13 +347,10 @@ void _bmo_slot_copy(BMOpSlot slot_args_src[BMO_OP_MAX_SLOTS], const char *slot_n
}
}
else if (slot_dst->slot_type == BMO_OP_SLOT_MAPPING) {
GHashIterator it;
for (BLI_ghashIterator_init(&it, slot_src->data.ghash);
BLI_ghashIterator_done(&it) == false;
BLI_ghashIterator_step(&it))
{
void *key = BLI_ghashIterator_getKey(&it);
void *val = BLI_ghashIterator_getValue(&it);
GHashIterator gh_iter;
GHASH_ITER (gh_iter, slot_src->data.ghash) {
void *key = BLI_ghashIterator_getKey(&gh_iter);
void *val = BLI_ghashIterator_getValue(&gh_iter);
BLI_ghash_insert(slot_dst->data.ghash, key, val);
}
}
@ -722,17 +719,15 @@ void *bmo_slot_buffer_grow(BMesh *bm, BMOperator *op, int slot_code, int totadd)
void BMO_slot_map_to_flag(BMesh *bm, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name,
const char htype, const short oflag)
{
GHashIterator it;
GHashIterator gh_iter;
BMOpSlot *slot = BMO_slot_get(slot_args, slot_name);
BMElemF *ele_f;
BLI_assert(slot->slot_type == BMO_OP_SLOT_MAPPING);
for (BLI_ghashIterator_init(&it, slot->data.ghash);
(ele_f = BLI_ghashIterator_getKey(&it));
BLI_ghashIterator_step(&it))
{
GHASH_ITER (gh_iter, slot->data.ghash) {
ele_f = BLI_ghashIterator_getKey(&gh_iter);
if (ele_f->head.htype & htype) {
BMO_elem_flag_enable(bm, ele_f, oflag);
}
@ -1424,10 +1419,19 @@ void *BMO_iter_step(BMOIter *iter)
return ele;
}
else if (slot->slot_type == BMO_OP_SLOT_MAPPING) {
void *ret = BLI_ghashIterator_getKey(&iter->giter);
iter->val = BLI_ghashIterator_getValue_p(&iter->giter);
void *ret;
BLI_ghashIterator_step(&iter->giter);
if (BLI_ghashIterator_done(&iter->giter) == false) {
ret = BLI_ghashIterator_getKey(&iter->giter);
iter->val = BLI_ghashIterator_getValue_p(&iter->giter);
BLI_ghashIterator_step(&iter->giter);
}
else {
ret = NULL;
iter->val = NULL;
}
return ret;
}

View File

@ -217,11 +217,11 @@ static HullFinalEdges *hull_final_edges(GSet *hull_triangles)
final_edges->link_pool = BLI_mempool_create(sizeof(LinkData), 0, 128, BLI_MEMPOOL_NOP);
GSET_ITER (iter, hull_triangles) {
HullTriangle *t = BLI_gsetIterator_getKey(&iter);
LinkData *link;
int i;
for (i = 0; i < 3; i++) {
HullTriangle *t = BLI_gsetIterator_getKey(&iter);
BMVert *v1 = t->v[i];
BMVert *v2 = t->v[(i + 1) % 3];
ListBase *adj;