factor implem for Vec.{find,find_i}

This commit is contained in:
Simon Cruanes 2024-12-13 00:19:05 -05:00
parent cad41d70d6
commit 2fda76a5f7
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 6 additions and 18 deletions

View file

@ -475,21 +475,6 @@ let for_all p v =
let member ~eq x v = exists (eq x) v
let find_internal_ 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
x
else
check (i + 1)
)
in
check 0
let find_internal_i_ p v =
let n = v.size in
let rec check i =
@ -505,8 +490,10 @@ let find_internal_i_ p v =
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 find_exn p v =
try snd (find_internal_i_ p v) with Not_found -> raise Not_found
let find p v = try Some (snd @@ find_internal_i_ 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 =

View file

@ -221,7 +221,8 @@ 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. *)
(** Find an element and its index that satisfies the predicate.
@since NEXT_RELEASE *)
val find_exn : ('a -> bool) -> ('a, _) t -> 'a
(** Find an element that satisfies the predicate, or