GHash/Edgehash: make simple iterator checking functions inline.
authorCampbell Barton <ideasman42@gmail.com>
Tue, 8 Apr 2014 05:50:38 +0000 (15:50 +1000)
committerCampbell Barton <ideasman42@gmail.com>
Tue, 8 Apr 2014 05:50:38 +0000 (15:50 +1000)
also remove NULL check, only a few areas made use of this.

source/blender/blenlib/BLI_edgehash.h
source/blender/blenlib/BLI_ghash.h
source/blender/blenlib/intern/BLI_ghash.c
source/blender/blenlib/intern/edgehash.c
source/blender/bmesh/intern/bmesh_operators.c
source/blender/bmesh/operators/bmo_hull.c

index fccbda4b4dbd359a1a1d8b2fb6dfac9f8e8cd0cf..b4ca25bed4f019fa5ba9c53a6f1c10df3efa2252 100644 (file)
 #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)
index e5a93691ad06ca44eb4c5ddf3d0d54311296e860..61a75ebb7e89ef7d3b9872f07c0f7727eb59cd12 100644 (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_);                           \
index 169b98da267bb67f2aa19ce507db0b16b39964b1..e30883611a8a1ce4661672b18e7209c4fcd1c370 100644 (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,8 +612,21 @@ 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;
-}
-
 /** \} */
 
 
index 1d0e62dfdf6a3d4c7102861259731126b635885e..40b484e1974d63f3f6e7661c54c40fd0a073aca0 100644 (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,10 +500,17 @@ 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);
-}
-
 /** \} */
 
 /* -------------------------------------------------------------------- */
index 086233ebe09eb170d919539be242eef1a49e113f..2a6b4d704190e8ee3ec2db8c9d55ce1c136676ea 100644 (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;
+
+
+               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);
+                       BLI_ghashIterator_step(&iter->giter);
+               }
+               else {
+                       ret = NULL;
+                       iter->val = NULL;
+               }
 
                return ret;
        }
index 1082955b466e07dffee3603166a09b75a0ba6bd7..26a4dbe1e1debe09841f759ed33acdc05b834e40 100644 (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;