Skip to content

feat(deque): Add blit_to and append method #2397

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

Closed
wants to merge 60 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 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
3723883
Merge branch 'main' of https://github.com/ethereal-dream/core
ethereal-dream Jul 2, 2025
a2a3370
feat(deque): Add append method
ethereal-dream Jul 2, 2025
dacded2
Merge branch 'moonbitlang:main' into main
ethereal-dream Jul 2, 2025
53d8fdb
Delet UninitializedArray::blit
ethereal-dream Jul 2, 2025
21357ae
Merge branch 'main' of https://github.com/ethereal-dream/core
ethereal-dream Jul 2, 2025
48164ea
feat(deque): Add blit_to method
ethereal-dream Jul 2, 2025
b9bdc24
Merge branch 'main' of https://github.com/ethereal-dream/core
ethereal-dream Jul 2, 2025
944aad0
Format code
ethereal-dream Jul 5, 2025
af26bcc
Format code
ethereal-dream Jul 5, 2025
6b75f81
Format code
ethereal-dream Jul 6, 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
89 changes: 89 additions & 0 deletions deque/deque.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -1558,3 +1558,92 @@ pub fn[A] get(self : T[A], index : Int) -> A? {
pub fn[A : Compare] binary_search(self : T[A], value : A) -> Result[Int, Int] {
self.binary_search_by(x => x.compare(value))
}

///|
/// Appends all elements from one deque to the end of another deque.
///
/// Parameters:
///
/// * `self` : The deque to which elements will be appended.
/// * `other` : The deque whose elements will be appended to `self`.
///
/// Returns nothing (`Unit`). Modifies `self` in place by pushing
///
/// Example:
///
/// ```moonbit
/// let deque1 = @deque.of([1, 2, 3])
/// let deque2 = @deque.of([4, 5, 6])
/// deque1.append(deque2)
/// inspect(deque1, content="@deque.of([1, 2, 3, 4, 5, 6])")
/// ```
pub fn[A] T::append(self : T[A], other : T[A]) -> Unit {
let dst_len = self.length()
let src_len = other.length()
let new_dst = UninitializedArray::make(dst_len + src_len)
UninitializedArray::unsafe_blit(new_dst, 0, self.buf, 0, dst_len)
UninitializedArray::unsafe_blit(new_dst, dst_len, other.buf, 0, src_len)
self.buf = new_dst
self.len = dst_len + src_len
self.tail = self.tail + src_len
}

///|
/// Copies a segment of elements from the source deque into a destination deque.
///
/// Copies `len` elements starting from `src_offset` in the source deque (`self`)
/// into the destination deque (`dst`) starting at `dst_offset`. Modifies the destination deque in place.
///
/// * If `dst_offset + len` exceeds the destination deque's current length,
/// the destination is automatically **resized** to accommodate the new elements.
/// * If the destination has sufficient capacity, elements are overwritten in-place.
///
///
/// Parameters:
/// * `self` : Source deque (`T[A]`) to copy elements from.
/// * `dst` : Destination deque (`T[A]`) to copy elements into (modified in place).
/// * `len~` : Number of elements to copy (labeled argument).
/// * `src_offset~` : Starting index in source deque (default = 0).
/// * `dst_offset~` : Starting index in destination deque (default = 0).
///
/// ```moonbit
/// let src = @deque.of([10, 20, 30, 40])
/// let dst = @deque.of([1, 2, 3, 4])
/// src.blit_to(dst, len=3, src_offset=1, dst_offset=3)
/// inspect(dst, content="@deque.of([1, 2, 3, 20, 30, 40])")
/// ```
pub fn[A] T::blit_to(
self : T[A],
dst : T[A],
len~ : Int,
src_offset~ : Int = 0,
dst_offset~ : Int = 0
) -> Unit {
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.buf, 0, dst.length())
UninitializedArray::unsafe_blit(
new_dst,
dst_offset,
self.buf,
src_offset,
len,
)
dst.buf = new_dst
dst.len = dst_offset + len
dst.tail = dst.len - 1
} else {
UninitializedArray::unsafe_blit(
dst.buf,
dst_offset,
self.buf,
src_offset,
len,
)
}
}
2 changes: 2 additions & 0 deletions deque/deque.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ fn[A] of(FixedArray[A]) -> T[A]

// Types and methods
type T[A]
fn[A] T::append(Self[A], Self[A]) -> Unit
fn[A] T::as_views(Self[A]) -> (ArrayView[A], ArrayView[A])
fn[A] T::back(Self[A]) -> A?
fn[A : Compare] T::binary_search(Self[A], A) -> Result[Int, Int]
fn[A] T::binary_search_by(Self[A], (A) -> Int) -> Result[Int, Int]
fn[A] T::blit_to(Self[A], Self[A], len~ : Int, src_offset~ : Int = .., dst_offset~ : Int = ..) -> Unit
fn[A] T::capacity(Self[A]) -> Int
fn[A] T::clear(Self[A]) -> Unit
fn[A : Eq] T::contains(Self[A], A) -> Bool
Expand Down
Loading