mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
feat(CCVector): add remove
remove keep the order of the element but need to move all element after the removed one.
This commit is contained in:
parent
c1461940c2
commit
c8d61a1248
2 changed files with 37 additions and 2 deletions
|
|
@ -247,6 +247,15 @@ let set v i x =
|
|||
if i < 0 || i >= v.size then invalid_arg "CCVector.set";
|
||||
Array.unsafe_set v.vec i x
|
||||
|
||||
let remove v i =
|
||||
if i < 0 || i >= v.size then invalid_arg "CCVector.remove";
|
||||
(* if v.(i) not the last element, then put last element at index i *)
|
||||
if i < v.size - 1
|
||||
then Array.blit v.vec (i+1) v.vec i (v.size - i - 1);
|
||||
(* remove one element *)
|
||||
v.size <- v.size - 1;
|
||||
fill_with_junk_ v.vec v.size 1
|
||||
|
||||
let remove_unordered v i =
|
||||
if i < 0 || i >= v.size then invalid_arg "CCVector.remove_unordered";
|
||||
(* if v.(i) not the last element, then put last element at index i *)
|
||||
|
|
@ -256,6 +265,25 @@ let remove_unordered v i =
|
|||
v.size <- v.size - 1;
|
||||
fill_with_junk_ v.vec v.size 1
|
||||
|
||||
(*$Q remove
|
||||
Q.(list_of_size (Gen.int_range 10 10) small_int) (fun l -> \
|
||||
let v1 = of_list l and v2 = of_list l in \
|
||||
remove v1 9; \
|
||||
remove_unordered v2 9; \
|
||||
to_list v1 = (to_list v2))
|
||||
Q.(list_of_size (Gen.int_range 10 10) small_int) (fun l -> \
|
||||
let l = List.sort CCInt.compare l in \
|
||||
let v = of_list l in\
|
||||
remove v 3; \
|
||||
to_list v = (List.sort CCInt.compare (to_list v)))
|
||||
Q.(list_of_size (Gen.int_range 10 10) small_int) (fun l -> \
|
||||
let l = List.sort CCInt.compare l in \
|
||||
let v1 = of_list l and v2 = of_list l in \
|
||||
remove v1 3; \
|
||||
remove_unordered v2 3; \
|
||||
to_list v1 = (List.sort CCInt.compare (to_list v2)))
|
||||
*)
|
||||
|
||||
let append_iter a i = i (fun x -> push a x)
|
||||
|
||||
let append_std_seq a seq = Seq.iter (fun x -> push a x) seq
|
||||
|
|
|
|||
|
|
@ -234,9 +234,16 @@ val set : ('a, rw) t -> int -> 'a -> unit
|
|||
(** Modify element at given index, or
|
||||
@raise Invalid_argument if bad index. *)
|
||||
|
||||
val remove : ('a, rw) t -> int -> unit
|
||||
(** [remove v i] remove the [i-th] element from [v].
|
||||
Move elements that are after the [i-th] in [v].
|
||||
Preserve the order of the elements in [v].
|
||||
See {!remove_unordered} for constant time function. *)
|
||||
|
||||
val remove_unordered : ('a, rw) t -> int -> unit
|
||||
(** Remove the [n-th] element of the vector. Does {b NOT} preserve the order
|
||||
of the elements (might swap with the last element). *)
|
||||
(** [remove_unordered v i] remove the [i-th] element from [v].
|
||||
Does {b NOT} preserve the order of the elements in [v]
|
||||
(might swap with the last element). *)
|
||||
|
||||
val rev : ('a,_) t -> ('a, 'mut) t
|
||||
(** Reverse the vector. *)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue