M lib/vector.ml => lib/vector.ml +18 -0
@@ 207,6 207,24 @@ let update (t : 'a t) (i : int) (value : 'a) : 'a t =
in
{ t with root = rebuild t.root t.height }
+let pop_opt (t : 'a t) : 'a t option =
+ match t.count with
+ | 0 -> None
+ | 1 -> Some (empty ())
+ | count ->
+ (* TODO: handle tree traversal, tail promotion *)
+ if (count - 1) >= (tail_offset t) then
+ (* Shrink tail *)
+ let tail = Array.sub t.tail 0 (Array.length t.tail - 1) in
+ Some { t with tail; count = t.count - 1 }
+ else
+ Some t
+
+let pop (t : 'a t) : 'a t =
+ match pop_opt t with
+ | Some t -> t
+ | None -> invalid_arg "Popping empty vector"
+
let of_seq (xs : 'a Seq.t) : 'a t =
Seq.fold_left append (empty ()) xs
M lib/vector.mli => lib/vector.mli +4 -0
@@ 17,6 17,10 @@ val append : 'a t -> 'a -> 'a t
val update : 'a t -> int -> 'a -> 'a t
+val pop_opt : 'a t -> 'a t option
+
+val pop : 'a t -> 'a t
+
val of_seq : 'a Seq.t -> 'a t
val to_seq : 'a t -> 'a Seq.t