fixed: implementation of `array.remove` and `array.filter`
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 6 Dec 2023 16:38:50 +0000 (10:38 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 6 Dec 2023 16:38:50 +0000 (10:38 -0600)
core/container/array.onyx

index 0282fba3364e9546d53b4ca089d6fac6ba3aa816..338ffb9f050ffc8d8dcc15891e7d49084b88c329 100644 (file)
@@ -221,7 +221,10 @@ remove :: (arr: &[..] $T, elem: T) {
     while i := 0; i < arr.count - move {
         defer i += 1;
 
-        if arr.data[i + move] == elem do move += 1;
+        while i + move < arr.count && arr.data[i + move] == elem {
+            move += 1;
+        }
+
         if move != 0 do arr.data[i] = arr.data[i + move];
     }
 
@@ -309,8 +312,12 @@ filter :: macro (arr: &[..] $T, body: Code) {
     while i := 0; i < arr.count - move {
         defer i += 1;
 
-        it := arr.data[i];
-        if !(#unquote body(it)) do move += 1;
+        while i + move < arr.count {
+            it := arr.data[i + move];
+            if #unquote body(it) do break;
+            move += 1;
+        }
+
         if move != 0 do arr.data[i] = arr.data[i + move];
     }