mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-05 19:00:31 -05:00
feat: add CCVector.findi
This commit is contained in:
parent
e1de3da1e3
commit
b140a50c46
2 changed files with 19 additions and 0 deletions
|
|
@ -490,8 +490,24 @@ let find_internal_ p v =
|
|||
in
|
||||
check 0
|
||||
|
||||
let find_internal_i_ p v =
|
||||
let n = v.size in
|
||||
let rec check i =
|
||||
if i = n then
|
||||
raise_notrace Not_found
|
||||
else (
|
||||
let x = v.vec.(i) in
|
||||
if p x then
|
||||
i,x
|
||||
else
|
||||
check (i + 1)
|
||||
)
|
||||
in
|
||||
check 0
|
||||
|
||||
let find_exn p v = try find_internal_ p v with Not_found -> raise Not_found
|
||||
let find p v = try Some (find_internal_ p v) with Not_found -> None
|
||||
let findi p v = try Some (find_internal_i_ p v) with Not_found -> None
|
||||
|
||||
let find_map f v =
|
||||
let n = v.size in
|
||||
|
|
|
|||
|
|
@ -220,6 +220,9 @@ val for_all : ('a -> bool) -> ('a, _) t -> bool
|
|||
val find : ('a -> bool) -> ('a, _) t -> 'a option
|
||||
(** Find an element that satisfies the predicate. *)
|
||||
|
||||
val findi : ('a -> bool) -> ('a, _) t -> (int * 'a) option
|
||||
(** Find an element and its index that satisfies the predicate. *)
|
||||
|
||||
val find_exn : ('a -> bool) -> ('a, _) t -> 'a
|
||||
(** Find an element that satisfies the predicate, or
|
||||
@raise Not_found if no element does. *)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue