[vlc-devel] [PATCH 5/7] randomizer: fix history cursor on removal

Romain Vimont rom1v at videolabs.io
Thu May 16 17:50:44 CEST 2019


randomizer_RemoveAt() did not manage the 'history' cursor correctly: it
explicitly tested "r->history == 0", which meant "no history" in
an earlier implementation, but not anymore.
---
 src/playlist/randomizer.c | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)

diff --git a/src/playlist/randomizer.c b/src/playlist/randomizer.c
index 2da77b84fd..388cc02139 100644
--- a/src/playlist/randomizer.c
+++ b/src/playlist/randomizer.c
@@ -483,7 +483,7 @@ randomizer_RemoveAt(struct randomizer *r, size_t index)
      *    ordered            order irrelevant               ordered
      */
 
-    /* update next before may be updated */
+    /* update next before index may be updated */
     if (index < r->next)
         r->next--;
 
@@ -497,26 +497,20 @@ randomizer_RemoveAt(struct randomizer *r, size_t index)
         index = r->head; /* the new index to remove */
     }
 
-    if (!r->history || index < r->history)
+    if (index < r->history)
     {
-        size_t swap = (r->history + r->items.size - 1) % r->items.size;
-        r->items.data[index] = r->items.data[swap];
-        index = swap;
+        /* this part is unordered, no need to shift all items */
+        r->items.data[index] = r->items.data[r->history - 1];
+        index = r->history - 1;
+        r->history--;
     }
 
-    if (r->history)
+    if (index < r->items.size - 1)
     {
+        /* shift the ordered history part by one */
         memmove(&r->items.data[index],
                 &r->items.data[index + 1],
                 (r->items.size - index - 1) * sizeof(*r->items.data));
-
-        if (index < r->history)
-            r->history--;
-        else if (r->history == r->items.size)
-            r->history = 0;
-
-        if (r->next == r->items.size)
-            r->next = 0;
     }
 
     r->items.size--;
-- 
2.20.1



More information about the vlc-devel mailing list