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

impl Eq/Compare traits for ArrayView #1337

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions array/view_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,21 @@ test "contains" {
assert_true!(v.contains(4))
assert_false!(v.contains(6))
}

test "op_equal" {
let v1 = [1, 2, 3][:]
let v2 = [1, 2, 3][:]
assert_true!(v1 == v2)
let v3 = [1, 2, 4, 6, 10][1:3]
let v4 = [0, 1, 2, 4, 6, 10, 12][2:4]
assert_true!(v3 == v4)
assert_false!(v1 == v3)
}

test "compare" {
assert_eq!([1, 2, 4][:].compare([1, 2, 3][:]), 1)
assert_eq!([-1, 2, 4][:].compare([1, 2, 4][:]), -1)
assert_eq!([1, 2, 3][:].compare([1, 2, 3][:]), 0)
assert_eq!([1, 2, 0][:].compare([1, 2][:]), 1)
assert_eq!([1, 2][:].compare([1, 2, 0][:]), -1)
}
34 changes: 34 additions & 0 deletions builtin/arrayview.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,37 @@ pub fn filter[T](self : ArrayView[T], f : (T) -> Bool) -> Array[T] {
}
arr
}

///|
pub impl[T : Eq] Eq for ArrayView[T] with op_equal(self, other) -> Bool {
if self.length() != other.length() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use guard?

return false
}
for i in 0..<self.length() {
if not(self[i] == other[i]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self[i] != other[i]?

return false
}
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid else of for as we are using imperative here. See return in loop.

true
}
}

///|
pub impl[T : Compare] Compare for ArrayView[T] with compare(self, other) -> Int {
let len_self = self.length()
let len_other = other.length()
if len_self < len_other {
-1
} else if len_self > len_other {
1
} else {
for i in 0..<len_self {
let cmp = self[i].compare(other[i])
if cmp != 0 {
break cmp
}
} else {
0
}
}
}
2 changes: 2 additions & 0 deletions builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ impl ArrayView {
to_json[X : ToJson](Self[X]) -> Json
to_string[X : Show](Self[X]) -> String
}
impl[T : Compare] Compare for ArrayView[T]
impl[T : Eq] Eq for ArrayView[T]
impl[X : Show] Show for ArrayView[X]

type BigInt
Expand Down
Loading