Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slice.unique returns incorrect slice #3276

Closed
kavalee opened this issue Mar 14, 2024 · 0 comments
Closed

slice.unique returns incorrect slice #3276

kavalee opened this issue Mar 14, 2024 · 0 comments

Comments

@kavalee
Copy link

kavalee commented Mar 14, 2024

Context

odin version dev-2024-03
Odin: dev-2024-03
OS: Arch Linux, Linux 6.7.8-arch1-1
CPU: Intel(R) Core(TM) i5-4590 CPU @ 3.30GHz
RAM: 15943 MiB

Expected Behavior

The slice returned from unique should be of length 1 for a slice of consecutive values.

Current Behavior

import "core:fmt"
import "core:slice"
main :: proc() {
	s := []int{2,2,2}
	res := slice.unique(s)
	fmt.println(res) // output: [2, 2, 2]
}

Suspicious code

// '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 where intrinsics.type_is_comparable(T) #no_bounds_check {
	if len(s) < 2 {
		return s
	}
	i := 1
	for j in 1..<len(s) {
		if s[j] != s[j-1] && i != j {
			s[i] = s[j]
		}
		i += 1
	}

	return s[:i]
}

i is always going to be len(s) since i is incremented every iteration of the loop, and the loop always runs len(s)-1 times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants