Skip to content

Commit 9dea302

Browse files
committed
move Compare impl of FixedArray to its own package
1 parent 2efbcd3 commit 9dea302

File tree

4 files changed

+49
-49
lines changed

4 files changed

+49
-49
lines changed

array/array.mbti

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ impl FixedArray {
7373
swap[T](Self[T], Int, Int) -> Unit
7474
}
7575
impl[T] Add for FixedArray[T]
76+
impl[T : Compare] Compare for FixedArray[T]
7677
impl[T : Eq] Eq for FixedArray[T]
7778
impl[X : @quickcheck.Arbitrary] @quickcheck.Arbitrary for FixedArray[X]
7879

array/fixedarray.mbt

+48
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,54 @@ test "op_equal" {
10051005
)
10061006
}
10071007
1008+
///|
1009+
/// Compares two fixed arrays lexicographically based on their elements. First
1010+
/// compares the lengths of the arrays, then compares elements pairwise until a
1011+
/// difference is found or all elements have been compared.
1012+
///
1013+
/// Parameters:
1014+
///
1015+
/// * `self` : The first fixed array to compare.
1016+
/// * `other` : The second fixed array to compare.
1017+
///
1018+
/// Returns an integer that indicates the relative order:
1019+
///
1020+
/// * A negative value if `self` is less than `other`
1021+
/// * Zero if `self` equals `other`
1022+
/// * A positive value if `self` is greater than `other`
1023+
///
1024+
/// Example:
1025+
///
1026+
/// ```moonbit
1027+
/// test "FixedArray::compare" {
1028+
/// let arr1 = [1, 2, 3]
1029+
/// let arr2 = [1, 2, 4]
1030+
/// let arr3 = [1, 2]
1031+
/// inspect!(arr1.compare(arr2), content="-1") // arr1 < arr2
1032+
/// inspect!(arr2.compare(arr1), content="1") // arr2 > arr1
1033+
/// inspect!(arr1.compare(arr3), content="1") // arr1 > arr3 (longer)
1034+
/// inspect!(arr1.compare(arr1), content="0") // arr1 = arr1
1035+
/// }
1036+
/// ```
1037+
pub impl[T : Compare] Compare for FixedArray[T] with compare(self, other) {
1038+
let len_self = self.length()
1039+
let len_other = other.length()
1040+
if len_self < len_other {
1041+
-1
1042+
} else if len_self > len_other {
1043+
1
1044+
} else {
1045+
for i in 0..<len_self {
1046+
let cmp = self.unsafe_get(i).compare(other.unsafe_get(i))
1047+
if cmp != 0 {
1048+
break cmp
1049+
}
1050+
} else {
1051+
0
1052+
}
1053+
}
1054+
}
1055+
10081056
///|
10091057
/// Concatenates two arrays and returns a new array containing all elements from
10101058
/// both arrays in order.

builtin/builtin.mbti

-1
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,6 @@ impl Compare for UInt
706706
impl Compare for UInt64
707707
impl Compare for Float
708708
impl Compare for Double
709-
impl[T : Compare] Compare for FixedArray[T]
710709
impl Compare for Bytes
711710
impl[T0 : Compare, T1 : Compare] Compare for (T0, T1)
712711
impl[T0 : Compare, T1 : Compare, T2 : Compare] Compare for (T0, T1, T2)

builtin/fixedarray.mbt

-48
Original file line numberDiff line numberDiff line change
@@ -116,54 +116,6 @@ pub fn FixedArray::fill[T](self : FixedArray[T], value : T) -> Unit {
116116
}
117117
}
118118
119-
///|
120-
/// Compares two fixed arrays lexicographically based on their elements. First
121-
/// compares the lengths of the arrays, then compares elements pairwise until a
122-
/// difference is found or all elements have been compared.
123-
///
124-
/// Parameters:
125-
///
126-
/// * `self` : The first fixed array to compare.
127-
/// * `other` : The second fixed array to compare.
128-
///
129-
/// Returns an integer that indicates the relative order:
130-
///
131-
/// * A negative value if `self` is less than `other`
132-
/// * Zero if `self` equals `other`
133-
/// * A positive value if `self` is greater than `other`
134-
///
135-
/// Example:
136-
///
137-
/// ```moonbit
138-
/// test "FixedArray::compare" {
139-
/// let arr1 = [1, 2, 3]
140-
/// let arr2 = [1, 2, 4]
141-
/// let arr3 = [1, 2]
142-
/// inspect!(arr1.compare(arr2), content="-1") // arr1 < arr2
143-
/// inspect!(arr2.compare(arr1), content="1") // arr2 > arr1
144-
/// inspect!(arr1.compare(arr3), content="1") // arr1 > arr3 (longer)
145-
/// inspect!(arr1.compare(arr1), content="0") // arr1 = arr1
146-
/// }
147-
/// ```
148-
pub impl[T : Compare] Compare for FixedArray[T] with compare(self, other) {
149-
let len_self = self.length()
150-
let len_other = other.length()
151-
if len_self < len_other {
152-
-1
153-
} else if len_self > len_other {
154-
1
155-
} else {
156-
for i in 0..<len_self {
157-
let cmp = self.unsafe_get(i).compare(other.unsafe_get(i))
158-
if cmp != 0 {
159-
break cmp
160-
}
161-
} else {
162-
0
163-
}
164-
}
165-
}
166-
167119
///|
168120
/// Tests whether the FixedArray contains no elements.
169121
///

0 commit comments

Comments
 (0)