Skip to content

Commit

Permalink
feat: add CCVector.findi
Browse files Browse the repository at this point in the history
  • Loading branch information
hesterjeng committed Dec 12, 2024
1 parent e1de3da commit 4118f87
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/core/CCVector.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/core/CCVector.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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. *)
Expand Down

0 comments on commit 4118f87

Please sign in to comment.