mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
CCVector: add insert
This commit is contained in:
parent
45f567dca1
commit
9bb280e353
2 changed files with 24 additions and 0 deletions
|
|
@ -345,6 +345,24 @@ let remove_unordered v i =
|
|||
to_list v1 = (List.sort CCInt.compare (to_list v2)))
|
||||
*)
|
||||
|
||||
let insert v i x =
|
||||
(* Note that we can insert at i=v.size *)
|
||||
if i < 0 || i > v.size then invalid_arg "CCVector.insert";
|
||||
if v.size = Array.length v.vec
|
||||
then grow_with_ v ~filler:x;
|
||||
(* Shift the following elements, then put the element at i *)
|
||||
Array.blit v.vec i v.vec (i+1) (v.size - i);
|
||||
v.vec.(i) <- x;
|
||||
v.size <- v.size + 1
|
||||
|
||||
(*$T
|
||||
let v = (1 -- 5) in insert v 3 9; to_list v = [1;2;3;9;4;5]
|
||||
let v = create () in insert v 0 2; to_list v = [2]
|
||||
let v = (1 -- 3) in remove_and_shift v 1; insert v 1 5; to_list v = [1;5;3]
|
||||
let v = (1 -- 3) in remove_and_shift v 0; insert v 2 5; to_list v = [2;3;5]
|
||||
let v = (1 -- 3) in insert v 3 5; to_list v = [1;2;3;5]
|
||||
*)
|
||||
|
||||
let[@inline] append_iter a i = i (fun x -> push a x)
|
||||
|
||||
let append_seq a seq = Seq.iter (fun x -> push a x) seq
|
||||
|
|
|
|||
|
|
@ -277,6 +277,12 @@ val remove_unordered : ('a, rw) t -> int -> unit
|
|||
(might swap with the last element).
|
||||
See {!remove_and_shift} if you want to keep the ordering. *)
|
||||
|
||||
val insert : ('a, rw) t -> int -> 'a -> unit
|
||||
(** [insert v i x] insert the given element at index i.
|
||||
Elements at location [i] and later are first shifted over in linear time before inserting [x].
|
||||
Preserve the order of elements in [v].
|
||||
@since 3.7 *)
|
||||
|
||||
val rev : ('a,_) t -> ('a, 'mut) t
|
||||
(** Reverse the vector. *)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue