[Bf-blender-cvs] [8f24ec2e26a] master: Cleanup: add BLI_linklist_find_last

Campbell Barton noreply at git.blender.org
Fri Jul 10 04:21:30 CEST 2020


Commit: 8f24ec2e26ac634789ed9027dcfe6d1cd14782a7
Author: Campbell Barton
Date:   Fri Jul 10 12:04:29 2020 +1000
Branches: master
https://developer.blender.org/rB8f24ec2e26ac634789ed9027dcfe6d1cd14782a7

Cleanup: add BLI_linklist_find_last

This makes adding to the end of a linked list simpler,
In most cases we avoid this in favor of BLI_linklist_append.

For one off operations it's OK.

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

M	source/blender/blenlib/BLI_linklist.h
M	source/blender/blenlib/intern/BLI_linklist.c
M	source/blender/editors/uvedit/uvedit_path.c

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

diff --git a/source/blender/blenlib/BLI_linklist.h b/source/blender/blenlib/BLI_linklist.h
index 06796d6592a..324da859af1 100644
--- a/source/blender/blenlib/BLI_linklist.h
+++ b/source/blender/blenlib/BLI_linklist.h
@@ -55,6 +55,7 @@ int BLI_linklist_count(const LinkNode *list) ATTR_WARN_UNUSED_RESULT;
 int BLI_linklist_index(const LinkNode *list, void *ptr) ATTR_WARN_UNUSED_RESULT;
 
 LinkNode *BLI_linklist_find(LinkNode *list, int index) ATTR_WARN_UNUSED_RESULT;
+LinkNode *BLI_linklist_find_last(LinkNode *list) ATTR_WARN_UNUSED_RESULT;
 
 void BLI_linklist_reverse(LinkNode **listp) ATTR_NONNULL(1);
 
diff --git a/source/blender/blenlib/intern/BLI_linklist.c b/source/blender/blenlib/intern/BLI_linklist.c
index 5020e06d0b3..dc5d20ece99 100644
--- a/source/blender/blenlib/intern/BLI_linklist.c
+++ b/source/blender/blenlib/intern/BLI_linklist.c
@@ -74,6 +74,16 @@ LinkNode *BLI_linklist_find(LinkNode *list, int index)
   return NULL;
 }
 
+LinkNode *BLI_linklist_find_last(LinkNode *list)
+{
+  if (list) {
+    while (list->next) {
+      list = list->next;
+    }
+  }
+  return list;
+}
+
 void BLI_linklist_reverse(LinkNode **listp)
 {
   LinkNode *rhead = NULL, *cur = *listp;
diff --git a/source/blender/editors/uvedit/uvedit_path.c b/source/blender/editors/uvedit/uvedit_path.c
index 3a7badc3e7b..8476592b587 100644
--- a/source/blender/editors/uvedit/uvedit_path.c
+++ b/source/blender/editors/uvedit/uvedit_path.c
@@ -265,10 +265,10 @@ static void mouse_mesh_uv_shortest_path_vert(Scene *scene,
 
   if (path) {
     if ((l_dst_add_to_path != NULL) && (BLI_linklist_index(path, l_dst_add_to_path) == -1)) {
-      /* Weak, we could find the last and append after that. */
-      BLI_linklist_reverse(&path);
-      BLI_linklist_prepend(&path, l_dst_add_to_path);
-      BLI_linklist_reverse(&path);
+      /* Append, this isn't optimal compared to #BLI_linklist_append, it's a one-off lookup. */
+      LinkNode *path_last = BLI_linklist_find_last(path);
+      BLI_linklist_insert_after(&path_last, l_dst_add_to_path);
+      BLI_assert(BLI_linklist_find_last(path)->link == l_dst_add_to_path);
     }
 
     /* toggle the flag */



More information about the Bf-blender-cvs mailing list