8000 Add truncate to array and join/drain/flatten to deque by ethereal-dream · Pull Request #2191 · moonbitlang/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add truncate to array and join/drain/flatten to deque #2191

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

Merged
merged 51 commits into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
8f1539d
Add truncate to array and join/drain/flatten to deque
ethereal-dream May 31, 2025
c66b690
add string package for deque
ethereal-dream May 31, 2025
dbddc3e
Merge branch 'main' into main
ethereal-dream May 31, 2025
c8132b4
format code
ethereal-dream May 31, 2025
b5cd80c
Merge branch 'main' of https://github.com/ethereal-dream/core
ethereal-dream May 31, 2025
847e61b
format code
ethereal-dream May 31, 2025
c01e2a1
Add truncate to array and join/drain/flatten to deque
ethereal-dream May 31, 2025
40f9691
add string package for deque
ethereal-dream May 31, 2025
bc585ad
format code
ethereal-dream May 31, 2025
231b5c4
format code
ethereal-dream May 31, 2025
88e38dc
Change implementation of Array::truncate
ethereal-dream Jun 1, 2025
73e1a24
Merge branch 'main' of https://github.com/ethereal-dream/core
ethereal-dream Jun 1, 2025
bef5f72
correct variable typo from 'length' to 'len'"
ethereal-dream Jun 1, 2025
be30174
remove redundant import of string module
ethereal-dream Jun 1, 2025
ed0af90
Merge branch 'main' into main
ethereal-dream Jun 1, 2025
0f66422
correct parameter type
ethereal-dream Jun 1, 2025
17face4
Merge branch 'main' of https://github.com/ethereal-dream/core
ethereal-dream Jun 1, 2025
4d32f2a
Merge branch 'main' into main
ethereal-dream Jun 1, 2025
163e5eb
Merge branch 'main' into main
ethereal-dream Jun 3, 2025
b99b5ab
improve code consistency
ethereal-dream Jun 5, 2025
c946aa9
Merge branch 'main' of https://github.com/ethereal-dream/core
ethereal-dream Jun 5, 2025
2342a20
Merge branch 'main' into main
ethereal-dream Jun 5, 2025
351d838
Merge branch 'main' into main
ethereal-dream Jun 11, 2025
031f41c
Merge branch 'main' into main
ethereal-dream Jun 12, 2025
ea85b10
Merge branch 'main' into main
ethereal-dream Jun 13, 2025
80f3aeb
Replaced `copy + clear` with direct capacity
ethereal-dream Jun 16, 2025
54e3cf4
Move and improve implementation of Array::truncate
ethereal-dream Jun 16, 2025
430c0e0
Merge branch 'main' of https://github.com/ethereal-dream/core
ethereal-dream Jun 16, 2025
0e08f15
Change the separator from string to string.view
ethereal-dream Jun 17, 2025
e201873
add UninitializedArray::blit and
ethereal-dream Jun 18, 2025
6614687
Change the implementation of T::flatten
ethereal-dream Jun 18, 2025
b6bdb4e
Merge branch 'main' into main
ethereal-dream Jun 18, 2025
79851e7
format code
ethereal-dream Jun 18, 2025
f55a208
Merge branch 'main' of https://github.com/ethereal-dream/core
ethereal-dream Jun 18, 2025
cfe6885
format code
ethereal-dream Jun 18, 2025
b7f467b
Move `Array::truncate` from arraycore_nonjs.mbt to array.mbt
ethereal-dream Jun 19, 2025
c48a266
format code
ethereal-dream Jun 19, 2025
25a1540
Merge branch 'main' into main
ethereal-dream Jun 19, 2025
0cfca9a
Move Array::truncate
ethereal-dream Jun 19, 2025
314d2c0
Merge branch 'main' of https://github.com/ethereal-dream/core
ethereal-dream Jun 19, 2025
bf2a093
Merge branch 'main' into main
peter-jerry-ye Jun 19, 2025
e728839
chore: fmt & fix warning
peter-jerry-ye Jun 19, 2025
96d2f1d
Merge branch 'main' into main
peter-jerry-ye Jun 20, 2025
bb0815b
Update deque/deque.mbt
ethereal-dream Jun 20, 2025
b4313a5
Change the implementation of T::drain
ethereal-dream Jun 24, 2025
08d9778
Format code
ethereal-dream Jun 24, 2025
851c896
Format code
ethereal-dream Jun 24, 2025
eb8e9fa
Merge branch 'main' into main
ethereal-dream Jun 24, 2025
a798caf
Format code
ethereal-dream Jun 24, 2025
56eb3fb
Merge branch 'main' of https://github.com/ethereal-dream/core
ethereal-dream Jun 24, 2025
6151184
Merge branch 'main' into main
peter-jerry-ye Jun 26, 2025
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
32 changes: 32 additions & 0 deletions builtin/array.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -1759,3 +1759,35 @@ pub impl[T] Default for Array[T] with default() {
pub fn[A] Array::unsafe_pop_back(self : Array[A]) -> Unit {
self.unsafe_pop() |> ignore
}

///|
/// Truncates the array in-place to the specified length.
///
/// If `len` is greater than or equal to the current array length,
/// the function does nothing. If `len` is 0, the array is cleared.
/// Otherwise, removes elements from the end until the array reaches the given length.
///
/// Parameters:
///
/// * `self` : The target array (modified in-place).
/// * `len` : The new desired length (must be non-negative).
///
/// Important:
/// - If `len` is negative, the function does nothing.
/// - If `len` exceeds current length, the array remains unchanged.
///
/// Example:
///
/// ```moonbit
/// let arr = [1, 2, 3, 4, 5]
/// arr.truncate(3)
/// inspect(arr, content="[1, 2, 3]")
/// ```
pub fn[A] Array::truncate(self : Array[A], len : Int) -> Unit {
guard len >= 0 && len < self.length() else { return }
if len == 0 {
self.clear()
return
}
self.unsafe_truncate_to_length(len)
}
2 changes: 2 additions & 0 deletions builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ fn[T : Eq] Array::starts_with(Self[T], Self[T]) -> Bool
fn[T : Eq] Array::strip_prefix(Self[T], Self[T]) -> Self[T]?
fn[T : Eq] Array::strip_suffix(Self[T], Self[T]) -> Self[T]?
fn[T] Array::swap(Self[T], Int, Int) -> Unit
fn[A] Array::truncate(Self[A], Int) -> Unit
fn[A] Array::unsafe_blit(Self[A], Int, Self[A], Int, Int) -> Unit
fn[A] Array::unsafe_blit_fixed(Self[A], Int, FixedArray[A], Int, Int) -> Unit
fn[T] Array::unsafe_get(Self[T], Int) -> T
Expand Down Expand Up @@ -302,6 +303,7 @@ impl Logger for StringBuilder
impl Show for StringBuilder

type UninitializedArray[T]
fn[A] UninitializedArray::blit(Self[A], Self[A], len~ : Int, src_offset~ : Int = .., dst_offset~ : Int = ..) -> Self[A]
fn[A] UninitializedArray::length(Self[A]) -> Int
fn[T] UninitializedArray::make(Int) -> Self[T]
fn[T] UninitializedArray::op_as_view(Self[T], start~ : Int = .., end? : Int) -> ArrayView[T]
Expand Down
47 changes: 47 additions & 0 deletions builtin/uninitialized_array.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,53 @@ fn[T] UninitializedArray::unsafe_blit(
FixedArray::unsafe_blit(dst.inner(), dst_offset, src.inner(), src_offset, len)
}

///|
/// Copies a range of elements from one `UninitializedArray` to another, ensuring the destination array has enough space.
///
/// If the destination array is not large enough to hold the copied elements at the specified offset, a new larger array is created and returned.
///
/// Parameters:
///
/// * `self` : The source array (not modified).
/// * `dst` : The destination array. May be modified in-place or replaced by a new array.
/// * `len` : Number of elements to copy.
/// * `src_offset` : Start index in the source array (default: 0).
/// * `dst_offset` : Start index in the destination array (default: 0).
///
/// Returns:
/// A new array if reallocation was necessary, otherwise the original `dst`.
///
/// Important:
/// - Requires: `len >= 0`
/// - Requires: `src_offset >= 0`
/// - Requires: `dst_offset >= 0`
/// - Requires: `src_offset + len <= self.length()`
/// - Ensures: `dst_offset + len <= dst.length()` after call (either by expanding `dst` or using a new array)
/// - Does not initialize or drop values; assumes caller handles memory safety.
///
pub fn[A] UninitializedArray::blit(
self : UninitializedArray[A],
dst : UninitializedArray[A],
len~ : Int,
src_offset~ : Int = 0,
dst_offset~ : Int = 0
) -> UninitializedArray[A] {
guard len >= 0 &&
dst_offset >= 0 &&
src_offset >= 0 &&
dst_offset <= dst.length() &&
src_offset + len <= self.length()
if dst_offset + len > dst.length() {
let new_dst = UninitializedArray::make(dst_offset + len)
UninitializedArray::unsafe_blit(new_dst, 0, dst, 0, dst.length())
UninitializedArray::unsafe_blit(new_dst, dst_offset, self, src_offset, len)
new_dst
} else {
UninitializedArray::unsafe_blit(dst, dst_offset, self, src_offset, len)
dst
}
}

///|
test "op_as_view with valid_range" {
let arr : UninitializedArray[Int] = UninitializedArray::make(5)
Expand Down