From 8a73ba595908d900861231210b350734eed31177 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Thu, 1 Jan 1970 00:00:00 +0000 Subject: [PATCH] Added functions sort_uniq and uniq. --- lib/diskimage_utils.ml | 13 +++++++++++++ lib/diskimage_utils.mli | 8 ++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/diskimage_utils.ml b/lib/diskimage_utils.ml index 6abb608..3ea53b7 100644 --- a/lib/diskimage_utils.ml +++ b/lib/diskimage_utils.ml @@ -263,6 +263,19 @@ let group_by ?(cmp = Pervasives.compare) ls = let ls' = List.rev ls' in List.map (fun (x, xs) -> x, List.rev xs) ls' +let rec uniq ?(cmp = Pervasives.compare) = function + | [] -> [] + | [x] -> [x] + | x :: y :: xs when cmp x y = 0 -> + uniq (x :: xs) + | x :: y :: xs -> + x :: uniq (y :: xs) + +let sort_uniq ?cmp xs = + let xs = ExtList.List.sort ?cmp xs in + let xs = uniq ?cmp xs in + xs + let rec range a b = if a < b then a :: range (a+1) b else [] diff --git a/lib/diskimage_utils.mli b/lib/diskimage_utils.mli index 6f813fe..9eccf64 100644 --- a/lib/diskimage_utils.mli +++ b/lib/diskimage_utils.mli @@ -144,6 +144,14 @@ val canonical_uuid : string -> string val group_by : ?cmp:('a -> 'a -> int) -> ('a * 'b) list -> ('a * 'b list) list (** Group a sorted list of pairs by the first element of the pair. *) +val sort_uniq : ?cmp:('a -> 'a -> int) -> 'a list -> 'a list +(** [sort_uniq xs] returns the list [xs], sorted and with all duplicate + elements removed. *) + +val uniq : ?cmp:('a -> 'a -> int) -> 'a list -> 'a list +(** [uniq xs] removes adjacent duplicate elements from a list, like + the Unix uniq(1) command. *) + val range : int -> int -> int list (** [range a b] returns the list of integers [a <= i < b]. If [a >= b] then the empty list is returned. -- 1.8.3.1