Skip to content

Commit 8824a5c

Browse files
Yu-zhbobzhang
authored andcommitted
feat(arrayview): take arrayview from fixedarray
1 parent 181772d commit 8824a5c

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

builtin/arrayview.mbt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,59 @@ pub fn[T] ArrayView::sub(
250250
ArrayView::make(self.buf(), self.start() + start, end - start)
251251
}
252252
253+
///|
254+
fn[T] unsafe_cast_fixedarray_to_uninitializedarray(
255+
arr : FixedArray[T],
256+
) -> UninitializedArray[T] = "%identity"
257+
258+
///|
259+
/// Creates a new `ArrayView` from a `FixedArray`.
260+
///
261+
/// Parameters:
262+
///
263+
/// * `self` : The fixed array to create a new view from.
264+
/// * `start` : The starting index in the array (inclusive). Defaults to 0.
265+
/// * `end` : The ending index in the array (exclusive). Defaults to the
266+
/// length of the array.
267+
///
268+
/// Returns a new `ArrayView` that provides a window into the specified portion
269+
/// of the original fixed array.
270+
///
271+
/// Throws a panic if:
272+
///
273+
/// * `start` is negative
274+
/// * `end` is greater than the length of the array
275+
/// * `start` is greater than `end`
276+
///
277+
/// Example:
278+
///
279+
/// ```moonbit
280+
/// let arr: FixedArray[Int] = [1, 2, 3, 4, 5]
281+
/// let view = arr[1:4] // view = [2, 3, 4]
282+
/// inspect(view[0], content="2")
283+
/// ```
284+
#alias(op_as_view)
285+
pub fn[T] FixedArray::sub(
286+
self : FixedArray[T],
287+
start? : Int = 0,
288+
end? : Int,
289+
) -> ArrayView[T] {
290+
let len = self.length()
291+
let end = match end {
292+
None => len
293+
Some(end) => if end < 0 { len + end } else { end }
294+
}
295+
let start = if start < 0 { len + start } else { start }
296+
guard start >= 0 && start <= end && end <= len else {
297+
abort("View index out of bounds")
298+
}
299+
ArrayView::make(
300+
unsafe_cast_fixedarray_to_uninitializedarray(self),
301+
start,
302+
end - start,
303+
)
304+
}
305+
253306
///|
254307
#deprecated("ArrayView will be immutable, use array if you need mutation")
255308
pub fn[T] ArrayView::blit_to(self : ArrayView[T], dst : ArrayView[T]) -> Unit {

builtin/arrayview_test.mbt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,51 @@ test "panic negative index8" {
145145
let _ = arr[-1:-3]
146146

147147
}
148+
149+
///|
150+
test "fixedarray_sub" {
151+
// scalar case
152+
let arr = FixedArray::makei(10, i => i)
153+
inspect(arr[:], content="[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]")
154+
inspect(arr[1:], content="[1, 2, 3, 4, 5, 6, 7, 8, 9]")
155+
inspect(arr[:-2], content="[0, 1, 2, 3, 4, 5, 6, 7]")
156+
inspect(arr[3:-4], content="[3, 4, 5]")
157+
inspect(arr[-5:], content="[5, 6, 7, 8, 9]")
158+
arr[5] = 100
159+
inspect(arr[:], content="[0, 1, 2, 3, 4, 100, 6, 7, 8, 9]")
160+
inspect(arr[1:], content="[1, 2, 3, 4, 100, 6, 7, 8, 9]")
161+
inspect(arr[:-2], content="[0, 1, 2, 3, 4, 100, 6, 7]")
162+
inspect(arr[3:-4], content="[3, 4, 100]")
163+
inspect(arr[-5:], content="[100, 6, 7, 8, 9]")
164+
// reference case
165+
let arr = FixedArray::makei(10, i => (i, i))
166+
inspect(
167+
arr[:],
168+
content="[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9)]",
169+
)
170+
inspect(
171+
arr[1:],
172+
content="[(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9)]",
173+
)
174+
inspect(
175+
arr[:-2],
176+
content="[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7)]",
177+
)
178+
inspect(arr[3:-4], content="[(3, 3), (4, 4), (5, 5)]")
179+
inspect(arr[-5:], content="[(5, 5), (6, 6), (7, 7), (8, 8), (9, 9)]")
180+
arr[5] = (100, 100)
181+
inspect(
182+
arr[:],
183+
content="[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (100, 100), (6, 6), (7, 7), (8, 8), (9, 9)]",
184+
)
185+
inspect(
186+
arr[1:],
187+
content="[(1, 1), (2, 2), (3, 3), (4, 4), (100, 100), (6, 6), (7, 7), (8, 8), (9, 9)]",
188+
)
189+
inspect(
190+
arr[:-2],
191+
content="[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (100, 100), (6, 6), (7, 7)]",
192+
)
193+
inspect(arr[3:-4], content="[(3, 3), (4, 4), (100, 100)]")
194+
inspect(arr[-5:], content="[(100, 100), (6, 6), (7, 7), (8, 8), (9, 9)]")
195+
}

0 commit comments

Comments
 (0)