mirror of
https://github.com/c-cube/sidekick.git
synced 2025-12-11 21:48:50 -05:00
feat(vec): add append, fix prepend
This commit is contained in:
parent
1f9c43afa8
commit
4d3b278754
2 changed files with 14 additions and 0 deletions
|
|
@ -96,6 +96,16 @@ let[@inline] fast_remove t i =
|
||||||
Array.unsafe_set t.data i @@ Array.unsafe_get t.data (t.sz - 1);
|
Array.unsafe_set t.data i @@ Array.unsafe_get t.data (t.sz - 1);
|
||||||
t.sz <- t.sz - 1
|
t.sz <- t.sz - 1
|
||||||
|
|
||||||
|
let append ~into v : unit =
|
||||||
|
if v.sz = 0 then ()
|
||||||
|
else (
|
||||||
|
if v.sz + into.sz > Array.length into.data then (
|
||||||
|
resize_ into v.data.(0) (v.sz + into.sz);
|
||||||
|
);
|
||||||
|
Array.blit v.data 0 into.data into.sz v.sz;
|
||||||
|
into.sz <- into.sz + v.sz;
|
||||||
|
)
|
||||||
|
|
||||||
let prepend v ~into : unit =
|
let prepend v ~into : unit =
|
||||||
if v.sz = 0 then ()
|
if v.sz = 0 then ()
|
||||||
else (
|
else (
|
||||||
|
|
@ -104,6 +114,7 @@ let prepend v ~into : unit =
|
||||||
);
|
);
|
||||||
Array.blit into.data 0 into.data v.sz into.sz; (* shift elements *)
|
Array.blit into.data 0 into.data v.sz into.sz; (* shift elements *)
|
||||||
Array.blit v.data 0 into.data 0 v.sz;
|
Array.blit v.data 0 into.data 0 v.sz;
|
||||||
|
into.sz <- into.sz + v.sz;
|
||||||
)
|
)
|
||||||
|
|
||||||
let filter_in_place f vec =
|
let filter_in_place f vec =
|
||||||
|
|
|
||||||
|
|
@ -70,6 +70,9 @@ val fast_remove : 'a t -> int -> unit
|
||||||
(** Remove element at index [i] without preserving order
|
(** Remove element at index [i] without preserving order
|
||||||
(swap with last element) *)
|
(swap with last element) *)
|
||||||
|
|
||||||
|
val append : into:'a t -> 'a t -> unit
|
||||||
|
(** [append ~into v] pushes elements of [v] in the vector [into] *)
|
||||||
|
|
||||||
val prepend : 'a t -> into:'a t -> unit
|
val prepend : 'a t -> into:'a t -> unit
|
||||||
(** [prepend v ~into] pushes all elements of [v] into [into],
|
(** [prepend v ~into] pushes all elements of [v] into [into],
|
||||||
at the beginning. consumes [v]. *)
|
at the beginning. consumes [v]. *)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue