diff --git a/src/core/CCList.ml b/src/core/CCList.ml index b2f59af8..5e8b33f8 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -797,6 +797,30 @@ let sorted_insert ~cmp ?(uniq=false) x l = List.mem x (sorted_insert ~cmp:CCInt.compare x l)) *) +let sorted_remove ~cmp ~key l = + let rec aux cmp key left l = match l with + | [] -> List.rev left + | y :: tail -> + match cmp key y with + | 0 -> aux cmp key left tail + | n when n<0 -> List.rev_append left l + | _ -> aux cmp key (y::left) tail + in + aux cmp key [] l + +(*$Q + Q.(pair small_int (list small_int)) (fun (key,l) -> \ + let l = List.sort Stdlib.compare l in \ + is_sorted ~cmp:CCInt.compare (sorted_remove ~cmp:CCInt.compare ~key l)) + Q.(pair small_int (list small_int)) (fun (key,l) -> \ + let l = List.sort Stdlib.compare l in \ + let l' = sorted_remove ~cmp:CCInt.compare ~key l in \ + List.length l' = List.length l - count (Int.equal key) l) + Q.(pair small_int (list small_int)) (fun (key,l) -> \ + let l = List.sort Stdlib.compare l in \ + not (List.mem key (sorted_remove ~cmp:CCInt.compare ~key l))) +*) + let uniq_succ ~eq l = let rec f acc l = match l with | [] -> List.rev acc diff --git a/src/core/CCList.mli b/src/core/CCList.mli index 70812386..b2f4ccc0 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -522,6 +522,11 @@ val sorted_insert : cmp:('a -> 'a -> int) -> ?uniq:bool -> 'a -> 'a list -> 'a l [x] is not duplicated. Default [false] ([x] will be inserted in any case). @since 0.17 *) +val sorted_remove : cmp:('a -> 'a -> int) -> key:'a -> 'a list -> 'a list +(** [sorted_insert ~cmp ~key l] removes [key] from a sorted list [l] such that + the return value is sorted too. + @since NEXT_RELEASE *) + val uniq_succ : eq:('a -> 'a -> bool) -> 'a list -> 'a list (** [uniq_succ ~eq l] removes duplicate elements that occur one next to the other. Examples: diff --git a/src/core/CCListLabels.mli b/src/core/CCListLabels.mli index 7e6318f4..a8a71b33 100644 --- a/src/core/CCListLabels.mli +++ b/src/core/CCListLabels.mli @@ -525,6 +525,11 @@ val sorted_insert : cmp:(('a -> 'a -> int) [@keep_label]) -> ?uniq:bool -> 'a -> [x] is not duplicated. Default [false] ([x] will be inserted in any case). @since 0.17 *) +val sorted_remove : cmp:(('a -> 'a -> int) [@keep_label]) -> key:('a [@keep_label]) -> 'a list -> 'a list +(** [sorted_insert ~cmp ~key l] removes [key] from a sorted list [l] such that + the return value is sorted too. + @since NEXT_RELEASE *) + val uniq_succ : eq:(('a -> 'a -> bool) [@keep_label]) -> 'a list -> 'a list (** [uniq_succ ~eq l] removes duplicate elements that occur one next to the other. Examples: