-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathfixedarray.mbt
101 lines (93 loc) · 2.37 KB
/
fixedarray.mbt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2025 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
///|
/// @intrinsic %iter.from_array
pub fn FixedArray::iter[T](self : FixedArray[T]) -> Iter[T] {
Iter::new(fn(yield_) {
for v in self {
guard let IterContinue = yield_(v) else { x => break x }
} else {
IterContinue
}
})
}
///|
pub fn FixedArray::iter2[T](self : FixedArray[T]) -> Iter2[Int, T] {
Iter2::new(fn(yield_) {
for i, v in self {
guard let IterContinue = yield_(i, v) else { x => break x }
} else {
IterContinue
}
})
}
///|
pub fn FixedArray::default[X]() -> FixedArray[X] {
[]
}
///|
/// Fill the array with a given value.
///
/// # Example
/// ```
/// let fa : FixedArray[Int] = [0, 0, 0, 0, 0]
/// fa.fill(3)
/// assert_eq!(fa[0], 3)
/// ```
pub fn FixedArray::fill[T](self : FixedArray[T], value : T) -> Unit {
for i in 0..<self.length() {
self[i] = value
}
}
///|
pub fn compare[T : Compare](self : FixedArray[T], other : FixedArray[T]) -> 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.unsafe_get(i).compare(other.unsafe_get(i))
if cmp != 0 {
break cmp
}
} else {
0
}
}
}
///|
/// Tests whether the FixedArray contains no elements.
///
/// Parameters:
///
/// * `FixedArray` : The FixedArray to check.
///
/// Returns `true` if the FixedArray has no elements, `false` otherwise.
///
/// Example:
///
/// ```moonbit
/// test "FixedArray::is_empty" {
/// let empty : FixedArray[Int] = []
/// inspect!(empty.is_empty(), content="true")
/// let non_empty = [1, 2, 3]
/// inspect!(non_empty.is_empty(), content="false")
/// }
/// ```
pub fn FixedArray::is_empty[T](self : FixedArray[T]) -> Bool {
self.length() == 0
}