aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMaxime Dénès2016-06-27 15:05:29 +0200
committerMaxime Dénès2016-06-27 15:06:42 +0200
commit653e5307c846079a4ba3d2392fc55158ea4ee3c6 (patch)
tree8f12fda9671aafa5c05846ab1eed1a4107727785 /lib
parent13af70bece67a6d8f9fcd06cedf24bb2f0dc279a (diff)
parentc83fa10156545bce96ef4a0f93e8695ec353c834 (diff)
Merge branch 'sort-fields' into trunk
Was PR#86 Simplify `interp/constrintern.ml:sort_fields`.
Diffstat (limited to 'lib')
-rw-r--r--lib/cList.ml9
-rw-r--r--lib/cList.mli4
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 *)