From 98756f559191256497319c4a9bf017a5f51007b4 Mon Sep 17 00:00:00 2001 From: Flamme Shadow Date: Tue, 17 Dec 2024 22:15:21 +0800 Subject: [PATCH] impl Eq/Compare for ArrayView --- array/view_test.mbt | 18 ++++++++++++++++++ builtin/arrayview.mbt | 34 ++++++++++++++++++++++++++++++++++ builtin/builtin.mbti | 2 ++ 3 files changed, 54 insertions(+) diff --git a/array/view_test.mbt b/array/view_test.mbt index e71ecabf3..d5715c756 100644 --- a/array/view_test.mbt +++ b/array/view_test.mbt @@ -57,3 +57,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) +} diff --git a/builtin/arrayview.mbt b/builtin/arrayview.mbt index 1da101357..a4df2409b 100644 --- a/builtin/arrayview.mbt +++ b/builtin/arrayview.mbt @@ -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() { + return false + } + for i in 0.. 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.. 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