Merge pull request #402 from bluddy/master

CCVector: add `insert`
This commit is contained in:
Simon Cruanes 2022-02-21 14:47:58 -05:00 committed by GitHub
commit 5840d677c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View file

@ -345,6 +345,24 @@ let remove_unordered v i =
to_list v1 = (List.sort CCInt.compare (to_list v2))) 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 *)
if i < v.size then 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[@inline] append_iter a i = i (fun x -> push a x)
let append_seq a seq = Seq.iter (fun x -> push a x) seq let append_seq a seq = Seq.iter (fun x -> push a x) seq

View file

@ -277,6 +277,12 @@ val remove_unordered : ('a, rw) t -> int -> unit
(might swap with the last element). (might swap with the last element).
See {!remove_and_shift} if you want to keep the ordering. *) 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 val rev : ('a,_) t -> ('a, 'mut) t
(** Reverse the vector. *) (** Reverse the vector. *)