made array.unique work on all array types
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 13 Dec 2021 14:27:06 +0000 (08:27 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 13 Dec 2021 14:27:06 +0000 (08:27 -0600)
core/container/array.onyx
tests/aoc-2021/day13.onyx

index 160a7f3a763b9f94d2ca48f0966effc34144d373..7b2928f380a45553c99286218958781e92e5acac 100644 (file)
@@ -144,26 +144,6 @@ concat :: (arr: ^[..] $T, other: [] T) {
     for ^o: other do push(arr, *o);
 }
 
-// This assumes that the elements are sorted in some fashion,
-// such that equal elements would be next to each other.
-unique :: (arr: ^[..] $T) {
-    idx := 0;
-    while i := 0; i < arr.count - 1 {
-        defer i += 1;
-
-        if idx != i {
-            arr.data[idx] = arr.data[i];
-        }
-
-        if !(arr.data[i] == arr.data[i + 1]) {
-            idx += 1;
-        }
-    }
-
-    arr.data[idx] = arr.data[arr.count - 1];
-    arr.count = idx + 1;
-}
-
 
 fold_idx_elem :: (arr: [] $T, cmp: (T, T) -> bool) -> (i32, T) {
     idx  := 0;
@@ -371,6 +351,27 @@ quicksort :: #match {
     }
 }
 
+// This assumes that the elements are sorted in some fashion,
+// such that equal elements would be next to each other.
+unique :: (arr: ^[] $T) {
+    idx := 0;
+    while i := 0; i < arr.count - 1 {
+        defer i += 1;
+
+        if idx != i {
+            arr.data[idx] = arr.data[i];
+        }
+
+        if !(arr.data[i] == arr.data[i + 1]) {
+            idx += 1;
+        }
+    }
+
+    arr.data[idx] = arr.data[arr.count - 1];
+    arr.count = idx + 1;
+}
+
+
 fold :: (arr: [] $T, init: $R, f: (T, R) -> R) -> R {
     val := init;
     for it: arr do val = f(it, val);
index d50ade6dc48fdc81eb0858290c940dde252f7508..163339ea454119a3989295c0f813abcd5a5a5be9 100644 (file)
@@ -12,7 +12,7 @@ cmp_point :: (p1, p2: Point) => {
     return p1.y - p2.y;
 }
 
-apply_fold :: (dots: ^[..] Point, axis_name: str, axis_value: i32) {
+apply_fold :: (dots: ^[] Point, axis_name: str, axis_value: i32) {
     for^ *dots do switch axis_name {
         case "x" do if it.x > axis_value do it.x = 2 * axis_value - it.x;
         case "y" do if it.y > axis_value do it.y = 2 * axis_value - it.y;
@@ -53,7 +53,7 @@ main :: (args) => {
             string.advance(^line, 1);
             axis_value := cast(i32) conv.str_to_i64(line);
 
-            apply_fold(^dots, axis_name, axis_value);
+            apply_fold(~~ ^dots, axis_name, axis_value);
             if part_1_answer < 0 {
                 part_1_answer = dots.count;
             }