diff options
| author | Sandro Cavazzoni <sandro@ideaful.it> | 2024-06-16 11:36:20 +0200 |
|---|---|---|
| committer | Sandro Cavazzoni <sandro@ideaful.it> | 2024-06-16 11:36:20 +0200 |
| commit | e41878a64f3de4ba1eefe9f2364156f3065e4a40 (patch) | |
| tree | c165c7969164dc46e4b010103de95a09a6f4c8f7 /core/slice | |
| parent | a619ea3bcdbf170b8eebafde222a9df387284ee4 (diff) | |
Fix `slice.unique` wrong result
When you try to make this array unique `[]int{1, 2, 4, 4, 5}` you get
`[]int{1, 4, 5}` instead of `[]int{1, 2, 4, 5}`.
Our index `i` should be increased even with both indices `i` and `j`
have the same value
Diffstat (limited to 'core/slice')
| -rw-r--r-- | core/slice/slice.odin | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/core/slice/slice.odin b/core/slice/slice.odin index d8f4df88e..06f08fda2 100644 --- a/core/slice/slice.odin +++ b/core/slice/slice.odin @@ -495,8 +495,10 @@ unique :: proc(s: $S/[]$T) -> S where intrinsics.type_is_comparable(T) #no_bound } i := 1 for j in 1..<len(s) { - if s[j] != s[j-1] && i != j { - s[i] = s[j] + if s[j] != s[j-1] { + if i != j { + s[i] = s[j] + } i += 1 } } @@ -513,8 +515,10 @@ unique_proc :: proc(s: $S/[]$T, eq: proc(T, T) -> bool) -> S #no_bounds_check { } i := 1 for j in 1..<len(s) { - if !eq(s[j], s[j-1]) && i != j { - s[i] = s[j] + if !eq(s[j], s[j-1]) { + if i != j { + s[i] = s[j] + } i += 1 } } |