Skip to content

Commit 2fda76a

Browse files
committed
factor implem for Vec.{find,find_i}
1 parent cad41d7 commit 2fda76a

File tree

2 files changed

+6
-18
lines changed

2 files changed

+6
-18
lines changed

src/core/CCVector.ml

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -475,21 +475,6 @@ let for_all p v =
475475

476476
let member ~eq x v = exists (eq x) v
477477

478-
let find_internal_ p v =
479-
let n = v.size in
480-
let rec check i =
481-
if i = n then
482-
raise_notrace Not_found
483-
else (
484-
let x = v.vec.(i) in
485-
if p x then
486-
x
487-
else
488-
check (i + 1)
489-
)
490-
in
491-
check 0
492-
493478
let find_internal_i_ p v =
494479
let n = v.size in
495480
let rec check i =
@@ -505,8 +490,10 @@ let find_internal_i_ p v =
505490
in
506491
check 0
507492

508-
let find_exn p v = try find_internal_ p v with Not_found -> raise Not_found
509-
let find p v = try Some (find_internal_ p v) with Not_found -> None
493+
let find_exn p v =
494+
try snd (find_internal_i_ p v) with Not_found -> raise Not_found
495+
496+
let find p v = try Some (snd @@ find_internal_i_ p v) with Not_found -> None
510497
let findi p v = try Some (find_internal_i_ p v) with Not_found -> None
511498

512499
let find_map f v =

src/core/CCVector.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ val find : ('a -> bool) -> ('a, _) t -> 'a option
221221
(** Find an element that satisfies the predicate. *)
222222

223223
val findi : ('a -> bool) -> ('a, _) t -> (int * 'a) option
224-
(** Find an element and its index that satisfies the predicate. *)
224+
(** Find an element and its index that satisfies the predicate.
225+
@since NEXT_RELEASE *)
225226

226227
val find_exn : ('a -> bool) -> ('a, _) t -> 'a
227228
(** Find an element that satisfies the predicate, or

0 commit comments

Comments
 (0)