From 2fda76a5f70c6fc9f253f4fd1c49a94fd8c21b24 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Fri, 13 Dec 2024 00:19:05 -0500 Subject: [PATCH] factor implem for Vec.{find,find_i} --- src/core/CCVector.ml | 21 ++++----------------- src/core/CCVector.mli | 3 ++- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/src/core/CCVector.ml b/src/core/CCVector.ml index be20260e..93b01e48 100644 --- a/src/core/CCVector.ml +++ b/src/core/CCVector.ml @@ -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 = diff --git a/src/core/CCVector.mli b/src/core/CCVector.mli index aed8e0c7..d8e47a33 100644 --- a/src/core/CCVector.mli +++ b/src/core/CCVector.mli @@ -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