[Bf-blender-cvs] [c31f74d] master: Cleanup: use BLI_listbase_count_ex to avoid redundant looping

Campbell Barton noreply at git.blender.org
Sun Nov 16 14:31:06 CET 2014


Commit: c31f74de6bb7938ce0e36f75caeedfa16ac90b53
Author: Campbell Barton
Date:   Sun Nov 16 14:23:37 2014 +0100
Branches: master
https://developer.blender.org/rBc31f74de6bb7938ce0e36f75caeedfa16ac90b53

Cleanup: use BLI_listbase_count_ex to avoid redundant looping

===================================================================

M	source/blender/blenkernel/intern/object.c
M	source/blender/blenkernel/intern/particle.c
M	source/blender/blenkernel/intern/softbody.c
M	source/blender/blenlib/intern/listbase.c
M	source/blender/bmesh/tools/bmesh_region_match.c
M	source/blender/collada/ArmatureExporter.cpp
M	source/blender/editors/armature/editarmature_retarget.c
M	source/blender/editors/armature/reeb.c
M	source/blender/editors/mesh/editmesh_knife.c
M	source/blender/editors/space_buttons/buttons_texture.c
M	source/blender/editors/space_nla/nla_edit.c
M	source/gameengine/Converter/BL_BlenderDataConversion.cpp
M	source/gameengine/Converter/BL_SkinDeformer.cpp

===================================================================

diff --git a/source/blender/blenkernel/intern/object.c b/source/blender/blenkernel/intern/object.c
index 95dfc8a..725ed8d 100644
--- a/source/blender/blenkernel/intern/object.c
+++ b/source/blender/blenkernel/intern/object.c
@@ -1098,7 +1098,7 @@ bool BKE_object_lod_remove(Object *ob, int level)
 	MEM_freeN(rem);
 
 	/* If there are no user defined lods, remove the base lod as well */
-	if (BLI_listbase_count(&ob->lodlevels) == 1) {
+	if (BLI_listbase_is_single(&ob->lodlevels)) {
 		LodLevel *base = ob->lodlevels.first;
 		BLI_remlink(&ob->lodlevels, base);
 		MEM_freeN(base);
diff --git a/source/blender/blenkernel/intern/particle.c b/source/blender/blenkernel/intern/particle.c
index a17d0ca..bfb7802 100644
--- a/source/blender/blenkernel/intern/particle.c
+++ b/source/blender/blenkernel/intern/particle.c
@@ -3513,7 +3513,7 @@ ModifierData *object_add_particle_system(Scene *scene, Object *ob, const char *n
 
 	psys->part = psys_new_settings(DATA_("ParticleSettings"), NULL);
 
-	if (BLI_listbase_count(&ob->particlesystem) > 1)
+	if (BLI_listbase_count_ex(&ob->particlesystem, 2) > 1)
 		BLI_snprintf(psys->name, sizeof(psys->name), DATA_("ParticleSystem %i"), BLI_listbase_count(&ob->particlesystem));
 	else
 		BLI_strncpy(psys->name, DATA_("ParticleSystem"), sizeof(psys->name));
diff --git a/source/blender/blenkernel/intern/softbody.c b/source/blender/blenkernel/intern/softbody.c
index de95653..941344c 100644
--- a/source/blender/blenkernel/intern/softbody.c
+++ b/source/blender/blenkernel/intern/softbody.c
@@ -3561,7 +3561,7 @@ static void curve_surf_to_softbody(Scene *scene, Object *ob)
 
 	if (ob->softflag & OB_SB_EDGES) {
 		if (ob->type==OB_CURVE) {
-			totspring= totvert - BLI_listbase_count(&cu->nurb);
+			totspring = totvert - BLI_listbase_count(&cu->nurb);
 		}
 	}
 
diff --git a/source/blender/blenlib/intern/listbase.c b/source/blender/blenlib/intern/listbase.c
index ea33b9b..bd3e1e0 100644
--- a/source/blender/blenlib/intern/listbase.c
+++ b/source/blender/blenlib/intern/listbase.c
@@ -372,6 +372,11 @@ void BLI_freelistN(ListBase *listbase)
 	BLI_listbase_clear(listbase);
 }
 
+/**
+ * Returns the number of elements in \a listbase, up until (and including count_max)
+ *
+ * \note Use to avoid redundant looping.
+ */
 int BLI_listbase_count_ex(const ListBase *listbase, const int count_max)
 {
 	Link *link;
diff --git a/source/blender/bmesh/tools/bmesh_region_match.c b/source/blender/bmesh/tools/bmesh_region_match.c
index 562f123..bb7000e 100644
--- a/source/blender/bmesh/tools/bmesh_region_match.c
+++ b/source/blender/bmesh/tools/bmesh_region_match.c
@@ -656,7 +656,7 @@ static bool bm_uuidwalk_facestep_begin(
 	bool ok = false;
 
 	BLI_assert(BLI_ghash_size(uuidwalk->cache.faces_from_uuid) == 0);
-	BLI_assert(BLI_listbase_count(&fstep->items) == 0);
+	BLI_assert(BLI_listbase_is_empty(&fstep->items));
 
 	f_link_prev_p = &fstep->faces;
 	for (f_link = fstep->faces; f_link; f_link = f_link_next) {
diff --git a/source/blender/collada/ArmatureExporter.cpp b/source/blender/collada/ArmatureExporter.cpp
index e50cd6f..5ce6287 100644
--- a/source/blender/collada/ArmatureExporter.cpp
+++ b/source/blender/collada/ArmatureExporter.cpp
@@ -168,7 +168,7 @@ void ArmatureExporter::add_bone_node(Bone *bone, Object *ob_arm, Scene *sce,
 		node.setNodeSid(node_sid);
 
 #if 0 
-		if (BLI_listbase_is_empty(&bone->childbase) || BLI_listbase_count(&(bone->childbase)) >= 2) {
+		if (BLI_listbase_is_empty(&bone->childbase) || BLI_listbase_count_ex(&bone->childbase, 2) == 2) {
 			add_blender_leaf_bone( bone, ob_arm, node);
 		}
 		else {
diff --git a/source/blender/editors/armature/editarmature_retarget.c b/source/blender/editors/armature/editarmature_retarget.c
index d1cab01..aace8c5 100644
--- a/source/blender/editors/armature/editarmature_retarget.c
+++ b/source/blender/editors/armature/editarmature_retarget.c
@@ -1205,7 +1205,7 @@ static void RIG_arcFromBoneChain(RigGraph *rg, ListBase *list, EditBone *root_bo
 static void RIG_findHead(RigGraph *rg)
 {
 	if (rg->head == NULL) {
-		if (BLI_listbase_count(&rg->arcs) == 1) {
+		if (BLI_listbase_is_single(&rg->arcs)) {
 			RigArc *arc = rg->arcs.first;
 			
 			rg->head = (RigNode *)arc->head;
@@ -2152,7 +2152,7 @@ void exec_retargetArctoArc(TaskPool *UNUSED(pool), void *taskdata, int UNUSED(th
 	RigNode *inode_start = p->inode_start;
 	ReebArc *earc = iarc->link_mesh;
 	
-	if (BLI_listbase_count(&iarc->edges) == 1) {
+	if (BLI_listbase_is_single(&iarc->edges)) {
 		RigEdge *edge = iarc->edges.first;
 
 		if (testFlipArc(iarc, inode_start)) {
diff --git a/source/blender/editors/armature/reeb.c b/source/blender/editors/armature/reeb.c
index dd22127..f29d15f 100644
--- a/source/blender/editors/armature/reeb.c
+++ b/source/blender/editors/armature/reeb.c
@@ -2460,7 +2460,7 @@ void renormalizeWeight(EditMesh *em, float newmax)
 	EditVert *eve;
 	float minimum, maximum, range;
 	
-	if (em == NULL || BLI_listbase_count(&em->verts) == 0)
+	if (em == NULL || BLI_listbase_is_empty(&em->verts))
 		return;
 
 	/* First pass, determine maximum and minimum */
@@ -2486,7 +2486,7 @@ int weightFromLoc(EditMesh *em, int axis)
 {
 	EditVert *eve;
 	
-	if (em == NULL || BLI_listbase_count(&em->verts) == 0 || axis < 0 || axis > 2)
+	if (em == NULL || BLI_listbase_is_empty(&em->verts) || axis < 0 || axis > 2)
 		return 0;
 
 	/* Copy coordinate in weight */
diff --git a/source/blender/editors/mesh/editmesh_knife.c b/source/blender/editors/mesh/editmesh_knife.c
index dccb1f1..6246abd 100644
--- a/source/blender/editors/mesh/editmesh_knife.c
+++ b/source/blender/editors/mesh/editmesh_knife.c
@@ -749,12 +749,8 @@ static void knife_cut_face(KnifeTool_OpData *kcd, BMFace *f, ListBase *hits)
 {
 	Ref *r;
 	KnifeLineHit *lh, *prevlh;
-	int n;
 
-	(void) kcd;
-
-	n = BLI_listbase_count(hits);
-	if (n < 2)
+	if (BLI_listbase_count_ex(hits, 2) != 2)
 		return;
 
 	prevlh = NULL;
@@ -2108,7 +2104,7 @@ static ListBase *find_chain(KnifeTool_OpData *kcd, ListBase *fedges)
 			break;
 	}
 	if (ans) {
-		BLI_assert(BLI_listbase_count(ans) > 0);
+		BLI_assert(!BLI_listbase_is_empty(ans));
 		for (r = ans->first; r; r = r->next) {
 			ref = find_ref(fedges, r->ref);
 			BLI_assert(ref != NULL);
diff --git a/source/blender/editors/space_buttons/buttons_texture.c b/source/blender/editors/space_buttons/buttons_texture.c
index c8cbf0f..3b93970 100644
--- a/source/blender/editors/space_buttons/buttons_texture.c
+++ b/source/blender/editors/space_buttons/buttons_texture.c
@@ -468,7 +468,7 @@ void buttons_texture_context_compute(const bContext *C, SpaceButs *sbuts)
 	}
 	else {
 		/* set one user as active based on active index */
-		if (ct->index >= BLI_listbase_count(&ct->users))
+		if (ct->index == BLI_listbase_count_ex(&ct->users, ct->index))
 			ct->index = 0;
 
 		ct->user = BLI_findlink(&ct->users, ct->index);
diff --git a/source/blender/editors/space_nla/nla_edit.c b/source/blender/editors/space_nla/nla_edit.c
index 37efe1a..f43982f 100644
--- a/source/blender/editors/space_nla/nla_edit.c
+++ b/source/blender/editors/space_nla/nla_edit.c
@@ -1436,7 +1436,9 @@ static int nlaedit_swap_exec(bContext *C, wmOperator *op)
 		if (BLI_listbase_is_empty(&nlt->strips) == false) {
 			NlaStrip *mstrip = (NlaStrip *)nlt->strips.first;
 			
-			if ((mstrip->flag & NLASTRIP_FLAG_TEMP_META) && (BLI_listbase_count(&mstrip->strips) == 2)) {
+			if ((mstrip->flag & NLASTRIP_FLAG_TEMP_META) &&
+			    (BLI_listbase_count_ex(&mstrip->strips, 3) == 2))
+			{
 				/* remove this temp meta, so that we can see the strips inside */
 				BKE_nlastrips_clear_metas(&nlt->strips, 0, 1);
 			}
diff --git a/source/gameengine/Converter/BL_BlenderDataConversion.cpp b/source/gameengine/Converter/BL_BlenderDataConversion.cpp
index 246d823..c614074 100644
--- a/source/gameengine/Converter/BL_BlenderDataConversion.cpp
+++ b/source/gameengine/Converter/BL_BlenderDataConversion.cpp
@@ -1568,7 +1568,7 @@ static KX_GameObject *gameobject_from_blenderobject(
 		gameobj->AddMesh(meshobj);
 
 		// gather levels of detail
-		if (BLI_listbase_count(&ob->lodlevels) > 1) {
+		if (BLI_listbase_count_ex(&ob->lodlevels, 2) > 1) {
 			LodLevel *lod = ((LodLevel*)ob->lodlevels.first)->next;
 			Mesh* lodmesh = mesh;
 			Object* lodmatob = ob;
diff --git a/source/gameengine/Converter/BL_SkinDeformer.cpp b/source/gameengine/Converter/BL_SkinDeformer.cpp
index bf5e386..950c1dc 100644
--- a/source/gameengine/Converter/BL_SkinDeformer.cpp
+++ b/source/gameengine/Converter/BL_SkinDeformer.cpp
@@ -200,12 +200,14 @@ void BL_SkinDeformer::BGEDeformVerts()
 	Object *par_arma = m_armobj->GetArmatureObject();
 	MDeformVert *dverts = m_bmesh->dvert;
 	bDeformGroup *dg;
-	int defbase_tot = BLI_listbase_count(&m_objMesh->defbase);
+	int defbase_tot;
 	Eigen::Matrix4f pre_mat, post_mat, chan_mat, norm_chan_mat;
 
 	if (!dverts)
 		return;
 
+	defbase_tot = BLI_listbase_count(&m_objMesh->defbase);
+
 	if (m_dfnrToPC == NULL)
 	{
 		m_dfnrToPC = new bPoseChannel*[defbase_tot];




More information about the Bf-blender-cvs mailing list