mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
CCVector.{top,top_exn}
This commit is contained in:
parent
af850a88c1
commit
a47bd108ec
2 changed files with 22 additions and 0 deletions
|
|
@ -215,6 +215,19 @@ let pop v =
|
|||
try Some (pop_exn v)
|
||||
with Failure _ -> None
|
||||
|
||||
let top v =
|
||||
if v.size = 0 then None else Some v.vec.(v.size-1)
|
||||
|
||||
let top_exn v =
|
||||
if v.size = 0 then failwith "Vector.top";
|
||||
v.vec.(v.size-1)
|
||||
|
||||
(*$T
|
||||
1 -- 10 |> top = Some 10
|
||||
create () |> top = None
|
||||
1 -- 10 |> top_exn = 10
|
||||
*)
|
||||
|
||||
let copy v = {
|
||||
size = v.size;
|
||||
vec = Array.sub v.vec 0 v.size;
|
||||
|
|
|
|||
|
|
@ -99,6 +99,15 @@ val pop_exn : ('a, rw) t -> 'a
|
|||
(** remove last element, or raise a Failure if empty
|
||||
@raise Failure on an empty vector *)
|
||||
|
||||
val top : ('a, _) t -> 'a option
|
||||
(** Top element, if present
|
||||
@since NEXT_RELEASE *)
|
||||
|
||||
val top_exn : ('a, _) t -> 'a
|
||||
(** Top element, if present
|
||||
@raise Failure on an empty vector
|
||||
@since NEXT_RELEASE *)
|
||||
|
||||
val copy : ('a,_) t -> ('a,'mut) t
|
||||
(** Shallow copy (may give an immutable or mutable vector) *)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue