feat: add CCVector.findi

This commit is contained in:
John Hester 2024-12-12 15:30:49 -06:00 committed by Simon Cruanes
parent e1de3da1e3
commit b140a50c46
2 changed files with 19 additions and 0 deletions

View file

@ -490,8 +490,24 @@ let find_internal_ p v =
in in
check 0 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_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 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 find_map f v =
let n = v.size in let n = v.size in

View file

@ -220,6 +220,9 @@ val for_all : ('a -> bool) -> ('a, _) t -> bool
val find : ('a -> bool) -> ('a, _) t -> 'a option val find : ('a -> bool) -> ('a, _) t -> 'a option
(** Find an element that satisfies the predicate. *) (** 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 val find_exn : ('a -> bool) -> ('a, _) t -> 'a
(** Find an element that satisfies the predicate, or (** Find an element that satisfies the predicate, or
@raise Not_found if no element does. *) @raise Not_found if no element does. *)