From f8cb2bcad21fc1faffcec90a9435fd85327b090f Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 13 Dec 2023 01:50:26 +0000 Subject: [PATCH] Add `slice.unique` and `slice.unique_proc` --- core/slice/slice.odin | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/core/slice/slice.odin b/core/slice/slice.odin index 5b25c930e..d85756859 100644 --- a/core/slice/slice.odin +++ b/core/slice/slice.odin @@ -486,6 +486,42 @@ repeat :: proc(s: $S/[]$U, count: int, allocator := context.allocator) -> (b: S, return } +// 'unique' replaces consecutive runs of equal elements with a single copy. +// The procedures modifies the slice in-place and returns the modified slice. +@(require_results) +unique :: proc(s: $S/[]$T) -> S #optional_allocator_error where intrinsics.type_is_comparable(T) #no_bounds_check { + if len(s) < 2 { + return s + } + i := 1 + for j in 1.. bool) -> S #optional_allocator_error #no_bounds_check { + if len(s) < 2 { + return s + } + i := 1 + for j in 1.. (res: T, ok: bool) where intrinsics.type_is_ordered(T) #optional_ok {