Skip to content

Commit

Permalink
factor implem for Vec.{find,find_i}
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed Dec 13, 2024
1 parent cad41d7 commit 2fda76a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 18 deletions.
21 changes: 4 additions & 17 deletions src/core/CCVector.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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 =
Expand Down
3 changes: 2 additions & 1 deletion src/core/CCVector.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2fda76a

Please sign in to comment.