Skip to content

Commit b140a50

Browse files
hesterjengc-cube
authored andcommitted
feat: add CCVector.findi
1 parent e1de3da commit b140a50

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/core/CCVector.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,24 @@ let find_internal_ p v =
490490
in
491491
check 0
492492

493+
let find_internal_i_ p v =
494+
let n = v.size in
495+
let rec check i =
496+
if i = n then
497+
raise_notrace Not_found
498+
else (
499+
let x = v.vec.(i) in
500+
if p x then
501+
i,x
502+
else
503+
check (i + 1)
504+
)
505+
in
506+
check 0
507+
493508
let find_exn p v = try find_internal_ p v with Not_found -> raise Not_found
494509
let find p v = try Some (find_internal_ p v) with Not_found -> None
510+
let findi p v = try Some (find_internal_i_ p v) with Not_found -> None
495511

496512
let find_map f v =
497513
let n = v.size in

src/core/CCVector.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ val for_all : ('a -> bool) -> ('a, _) t -> bool
220220
val find : ('a -> bool) -> ('a, _) t -> 'a option
221221
(** Find an element that satisfies the predicate. *)
222222

223+
val findi : ('a -> bool) -> ('a, _) t -> (int * 'a) option
224+
(** Find an element and its index that satisfies the predicate. *)
225+
223226
val find_exn : ('a -> bool) -> ('a, _) t -> 'a
224227
(** Find an element that satisfies the predicate, or
225228
@raise Not_found if no element does. *)

0 commit comments

Comments
 (0)