diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/cList.ml | 9 | ||||
| -rw-r--r-- | lib/cList.mli | 4 |
2 files changed, 13 insertions, 0 deletions
diff --git a/lib/cList.ml b/lib/cList.ml index 3a792d4168..602bba6a5c 100644 --- a/lib/cList.ml +++ b/lib/cList.ml @@ -65,6 +65,7 @@ sig val except : 'a eq -> 'a -> 'a list -> 'a list val remove : 'a eq -> 'a -> 'a list -> 'a list val remove_first : ('a -> bool) -> 'a list -> 'a list + val extract_first : ('a -> bool) -> 'a list -> 'a list * 'a val insert : ('a -> 'a -> bool) -> 'a -> 'a list -> 'a list val for_all2eq : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool val sep_last : 'a list -> 'a * 'a list @@ -461,6 +462,14 @@ let rec remove_first p = function | b::l -> b::remove_first p l | [] -> raise Not_found +let extract_first p li = + let rec loop rev_left = function + | [] -> raise Not_found + | x::right -> + if p x then List.rev_append rev_left right, x + else loop (x :: rev_left) right + in loop [] li + let insert p v l = let rec insrec = function | [] -> [v] diff --git a/lib/cList.mli b/lib/cList.mli index b19d1a80fc..bc8749b4f8 100644 --- a/lib/cList.mli +++ b/lib/cList.mli @@ -125,6 +125,10 @@ sig val remove_first : ('a -> bool) -> 'a list -> 'a list (** Remove the first element satisfying a predicate, or raise [Not_found] *) + val extract_first : ('a -> bool) -> 'a list -> 'a list * 'a + (** Remove and return the first element satisfying a predicate, + or raise [Not_found] *) + val insert : ('a -> 'a -> bool) -> 'a -> 'a list -> 'a list (** Insert at the (first) position so that if the list is ordered wrt to the total order given as argument, the order is preserved *) |
