1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
2001-12-07 05:41:02 -08:00
|
|
|
(* under the terms of the GNU Library General Public License, with *)
|
|
|
|
(* the special exception on linking described in file ../LICENSE. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Sets over ordered types *)
|
|
|
|
|
|
|
|
module type OrderedType =
|
|
|
|
sig
|
|
|
|
type t
|
|
|
|
val compare: t -> t -> int
|
|
|
|
end
|
|
|
|
|
|
|
|
module type S =
|
|
|
|
sig
|
|
|
|
type elt
|
|
|
|
type t
|
|
|
|
val empty: t
|
|
|
|
val is_empty: t -> bool
|
|
|
|
val mem: elt -> t -> bool
|
|
|
|
val add: elt -> t -> t
|
1998-07-02 02:48:14 -07:00
|
|
|
val singleton: elt -> t
|
1995-05-04 03:15:53 -07:00
|
|
|
val remove: elt -> t -> t
|
|
|
|
val union: t -> t -> t
|
|
|
|
val inter: t -> t -> t
|
|
|
|
val diff: t -> t -> t
|
|
|
|
val compare: t -> t -> int
|
|
|
|
val equal: t -> t -> bool
|
1997-05-10 08:38:17 -07:00
|
|
|
val subset: t -> t -> bool
|
1997-10-31 04:59:29 -08:00
|
|
|
val iter: (elt -> unit) -> t -> unit
|
1995-05-04 03:15:53 -07:00
|
|
|
val fold: (elt -> 'a -> 'a) -> t -> 'a -> 'a
|
2000-04-13 05:16:26 -07:00
|
|
|
val for_all: (elt -> bool) -> t -> bool
|
|
|
|
val exists: (elt -> bool) -> t -> bool
|
|
|
|
val filter: (elt -> bool) -> t -> t
|
|
|
|
val partition: (elt -> bool) -> t -> t * t
|
1995-06-22 03:11:18 -07:00
|
|
|
val cardinal: t -> int
|
1995-05-04 03:15:53 -07:00
|
|
|
val elements: t -> elt list
|
1998-04-27 02:55:50 -07:00
|
|
|
val min_elt: t -> elt
|
|
|
|
val max_elt: t -> elt
|
1995-05-30 06:33:57 -07:00
|
|
|
val choose: t -> elt
|
2004-04-14 04:10:22 -07:00
|
|
|
val split: elt -> t -> t * bool * t
|
2013-01-08 01:01:02 -08:00
|
|
|
val find: elt -> t -> elt
|
1995-05-04 03:15:53 -07:00
|
|
|
end
|
|
|
|
|
1995-10-01 06:39:43 -07:00
|
|
|
module Make(Ord: OrderedType) =
|
1995-05-04 03:15:53 -07:00
|
|
|
struct
|
|
|
|
type elt = Ord.t
|
1995-05-30 06:33:57 -07:00
|
|
|
type t = Empty | Node of t * elt * t * int
|
|
|
|
|
|
|
|
(* Sets are represented by balanced binary trees (the heights of the
|
|
|
|
children differ by at most 2 *)
|
|
|
|
|
|
|
|
let height = function
|
|
|
|
Empty -> 0
|
|
|
|
| Node(_, _, _, h) -> h
|
|
|
|
|
2003-06-23 00:28:34 -07:00
|
|
|
(* Creates a new node with left son l, value v and right son r.
|
|
|
|
We must have all elements of l < v < all elements of r.
|
1995-05-30 06:33:57 -07:00
|
|
|
l and r must be balanced and | height l - height r | <= 2.
|
|
|
|
Inline expansion of height for better speed. *)
|
|
|
|
|
2003-06-23 00:28:34 -07:00
|
|
|
let create l v r =
|
1995-05-30 06:33:57 -07:00
|
|
|
let hl = match l with Empty -> 0 | Node(_,_,_,h) -> h in
|
|
|
|
let hr = match r with Empty -> 0 | Node(_,_,_,h) -> h in
|
2003-06-23 00:28:34 -07:00
|
|
|
Node(l, v, r, (if hl >= hr then hl + 1 else hr + 1))
|
1995-05-30 06:33:57 -07:00
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
(* Same as create, but performs one step of rebalancing if necessary.
|
2003-06-23 00:28:34 -07:00
|
|
|
Assumes l and r balanced and | height l - height r | <= 3.
|
1996-04-22 04:15:41 -07:00
|
|
|
Inline expansion of create for better speed in the most frequent case
|
1995-05-30 06:33:57 -07:00
|
|
|
where no rebalancing is required. *)
|
|
|
|
|
2003-06-23 00:28:34 -07:00
|
|
|
let bal l v r =
|
1995-05-30 06:33:57 -07:00
|
|
|
let hl = match l with Empty -> 0 | Node(_,_,_,h) -> h in
|
|
|
|
let hr = match r with Empty -> 0 | Node(_,_,_,h) -> h in
|
|
|
|
if hl > hr + 2 then begin
|
|
|
|
match l with
|
|
|
|
Empty -> invalid_arg "Set.bal"
|
|
|
|
| Node(ll, lv, lr, _) ->
|
|
|
|
if height ll >= height lr then
|
2003-06-23 00:28:34 -07:00
|
|
|
create ll lv (create lr v r)
|
1995-05-30 06:33:57 -07:00
|
|
|
else begin
|
|
|
|
match lr with
|
|
|
|
Empty -> invalid_arg "Set.bal"
|
|
|
|
| Node(lrl, lrv, lrr, _)->
|
2003-06-23 00:28:34 -07:00
|
|
|
create (create ll lv lrl) lrv (create lrr v r)
|
1995-05-30 06:33:57 -07:00
|
|
|
end
|
|
|
|
end else if hr > hl + 2 then begin
|
|
|
|
match r with
|
|
|
|
Empty -> invalid_arg "Set.bal"
|
|
|
|
| Node(rl, rv, rr, _) ->
|
|
|
|
if height rr >= height rl then
|
2003-06-23 00:28:34 -07:00
|
|
|
create (create l v rl) rv rr
|
1995-05-30 06:33:57 -07:00
|
|
|
else begin
|
|
|
|
match rl with
|
|
|
|
Empty -> invalid_arg "Set.bal"
|
|
|
|
| Node(rll, rlv, rlr, _) ->
|
2003-06-23 00:28:34 -07:00
|
|
|
create (create l v rll) rlv (create rlr rv rr)
|
1995-05-30 06:33:57 -07:00
|
|
|
end
|
|
|
|
end else
|
2003-06-23 00:28:34 -07:00
|
|
|
Node(l, v, r, (if hl >= hr then hl + 1 else hr + 1))
|
1995-05-30 06:33:57 -07:00
|
|
|
|
2003-06-23 00:28:34 -07:00
|
|
|
(* Insertion of one element *)
|
1995-05-30 06:33:57 -07:00
|
|
|
|
2003-06-23 00:28:34 -07:00
|
|
|
let rec add x = function
|
|
|
|
Empty -> Node(Empty, x, Empty, 1)
|
|
|
|
| Node(l, v, r, _) as t ->
|
|
|
|
let c = Ord.compare x v in
|
|
|
|
if c = 0 then t else
|
|
|
|
if c < 0 then bal (add x l) v r else bal l v (add x r)
|
|
|
|
|
2012-01-16 01:03:16 -08:00
|
|
|
let singleton x = Node(Empty, x, Empty, 1)
|
|
|
|
|
|
|
|
(* Beware: those two functions assume that the added v is *strictly*
|
|
|
|
smaller (or bigger) than all the present elements in the tree; it
|
|
|
|
does not test for equality with the current min (or max) element.
|
|
|
|
Indeed, they are only used during the "join" operation which
|
|
|
|
respects this precondition.
|
|
|
|
*)
|
|
|
|
|
|
|
|
let rec add_min_element v = function
|
|
|
|
| Empty -> singleton v
|
|
|
|
| Node (l, x, r, h) ->
|
|
|
|
bal (add_min_element v l) x r
|
|
|
|
|
|
|
|
let rec add_max_element v = function
|
|
|
|
| Empty -> singleton v
|
|
|
|
| Node (l, x, r, h) ->
|
|
|
|
bal l x (add_max_element v r)
|
|
|
|
|
2003-06-23 00:28:34 -07:00
|
|
|
(* Same as create and bal, but no assumptions are made on the
|
|
|
|
relative heights of l and r. *)
|
|
|
|
|
|
|
|
let rec join l v r =
|
|
|
|
match (l, r) with
|
2012-01-16 01:03:16 -08:00
|
|
|
(Empty, _) -> add_min_element v r
|
|
|
|
| (_, Empty) -> add_max_element v l
|
2003-06-23 00:28:34 -07:00
|
|
|
| (Node(ll, lv, lr, lh), Node(rl, rv, rr, rh)) ->
|
|
|
|
if lh > rh + 2 then bal ll lv (join lr v r) else
|
|
|
|
if rh > lh + 2 then bal (join l v rl) rv rr else
|
|
|
|
create l v r
|
|
|
|
|
|
|
|
(* Smallest and greatest element of a set *)
|
|
|
|
|
|
|
|
let rec min_elt = function
|
|
|
|
Empty -> raise Not_found
|
|
|
|
| Node(Empty, v, r, _) -> v
|
|
|
|
| Node(l, v, r, _) -> min_elt l
|
|
|
|
|
|
|
|
let rec max_elt = function
|
|
|
|
Empty -> raise Not_found
|
|
|
|
| Node(l, v, Empty, _) -> v
|
|
|
|
| Node(l, v, r, _) -> max_elt r
|
|
|
|
|
|
|
|
(* Remove the smallest element of the given set *)
|
|
|
|
|
|
|
|
let rec remove_min_elt = function
|
|
|
|
Empty -> invalid_arg "Set.remove_min_elt"
|
|
|
|
| Node(Empty, v, r, _) -> r
|
|
|
|
| Node(l, v, r, _) -> bal (remove_min_elt l) v r
|
1995-05-30 06:33:57 -07:00
|
|
|
|
|
|
|
(* Merge two trees l and r into one.
|
|
|
|
All elements of l must precede the elements of r.
|
2003-06-23 00:28:34 -07:00
|
|
|
Assume | height l - height r | <= 2. *)
|
1995-05-30 06:33:57 -07:00
|
|
|
|
2003-06-23 00:28:34 -07:00
|
|
|
let merge t1 t2 =
|
1995-05-30 06:33:57 -07:00
|
|
|
match (t1, t2) with
|
|
|
|
(Empty, t) -> t
|
|
|
|
| (t, Empty) -> t
|
2003-06-23 00:28:34 -07:00
|
|
|
| (_, _) -> bal t1 (min_elt t2) (remove_min_elt t2)
|
1995-05-30 06:33:57 -07:00
|
|
|
|
2003-06-23 00:28:34 -07:00
|
|
|
(* Merge two trees l and r into one.
|
|
|
|
All elements of l must precede the elements of r.
|
|
|
|
No assumption on the heights of l and r. *)
|
1995-05-30 06:33:57 -07:00
|
|
|
|
2003-06-23 00:28:34 -07:00
|
|
|
let concat t1 t2 =
|
1995-05-30 06:33:57 -07:00
|
|
|
match (t1, t2) with
|
|
|
|
(Empty, t) -> t
|
|
|
|
| (t, Empty) -> t
|
2003-06-23 00:28:34 -07:00
|
|
|
| (_, _) -> join t1 (min_elt t2) (remove_min_elt t2)
|
1995-05-30 06:33:57 -07:00
|
|
|
|
2003-06-23 00:28:34 -07:00
|
|
|
(* Splitting. split x s returns a triple (l, present, r) where
|
|
|
|
- l is the set of elements of s that are < x
|
|
|
|
- r is the set of elements of s that are > x
|
|
|
|
- present is false if s contains no element equal to x,
|
|
|
|
or true if s contains an element equal to x. *)
|
1995-05-30 06:33:57 -07:00
|
|
|
|
|
|
|
let rec split x = function
|
|
|
|
Empty ->
|
2003-06-23 00:28:34 -07:00
|
|
|
(Empty, false, Empty)
|
1995-05-30 06:33:57 -07:00
|
|
|
| Node(l, v, r, _) ->
|
|
|
|
let c = Ord.compare x v in
|
2003-06-23 00:28:34 -07:00
|
|
|
if c = 0 then (l, true, r)
|
1995-05-30 06:33:57 -07:00
|
|
|
else if c < 0 then
|
2003-06-23 00:28:34 -07:00
|
|
|
let (ll, pres, rl) = split x l in (ll, pres, join rl v r)
|
1995-05-30 06:33:57 -07:00
|
|
|
else
|
2003-06-23 00:28:34 -07:00
|
|
|
let (lr, pres, rr) = split x r in (join l v lr, pres, rr)
|
1995-05-30 06:33:57 -07:00
|
|
|
|
|
|
|
(* Implementation of the set operations *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let empty = Empty
|
|
|
|
|
|
|
|
let is_empty = function Empty -> true | _ -> false
|
|
|
|
|
1995-05-30 06:33:57 -07:00
|
|
|
let rec mem x = function
|
|
|
|
Empty -> false
|
|
|
|
| Node(l, v, r, _) ->
|
|
|
|
let c = Ord.compare x v in
|
1998-04-27 02:55:50 -07:00
|
|
|
c = 0 || mem x (if c < 0 then l else r)
|
1995-05-30 06:33:57 -07:00
|
|
|
|
|
|
|
let rec remove x = function
|
|
|
|
Empty -> Empty
|
|
|
|
| Node(l, v, r, _) ->
|
|
|
|
let c = Ord.compare x v in
|
|
|
|
if c = 0 then merge l r else
|
|
|
|
if c < 0 then bal (remove x l) v r else bal l v (remove x r)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let rec union s1 s2 =
|
|
|
|
match (s1, s2) with
|
|
|
|
(Empty, t2) -> t2
|
|
|
|
| (t1, Empty) -> t1
|
1998-07-02 02:48:14 -07:00
|
|
|
| (Node(l1, v1, r1, h1), Node(l2, v2, r2, h2)) ->
|
|
|
|
if h1 >= h2 then
|
|
|
|
if h2 = 1 then add v2 s1 else begin
|
|
|
|
let (l2, _, r2) = split v1 s2 in
|
|
|
|
join (union l1 l2) v1 (union r1 r2)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if h1 = 1 then add v1 s2 else begin
|
|
|
|
let (l1, _, r1) = split v2 s1 in
|
|
|
|
join (union l1 l2) v2 (union r1 r2)
|
|
|
|
end
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let rec inter s1 s2 =
|
|
|
|
match (s1, s2) with
|
|
|
|
(Empty, t2) -> Empty
|
|
|
|
| (t1, Empty) -> Empty
|
|
|
|
| (Node(l1, v1, r1, _), t2) ->
|
1995-05-30 06:33:57 -07:00
|
|
|
match split v1 t2 with
|
2003-06-23 00:28:34 -07:00
|
|
|
(l2, false, r2) ->
|
1995-05-30 06:33:57 -07:00
|
|
|
concat (inter l1 l2) (inter r1 r2)
|
2003-06-23 00:28:34 -07:00
|
|
|
| (l2, true, r2) ->
|
1995-05-30 06:33:57 -07:00
|
|
|
join (inter l1 l2) v1 (inter r1 r2)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let rec diff s1 s2 =
|
|
|
|
match (s1, s2) with
|
|
|
|
(Empty, t2) -> Empty
|
|
|
|
| (t1, Empty) -> t1
|
|
|
|
| (Node(l1, v1, r1, _), t2) ->
|
1995-05-30 06:33:57 -07:00
|
|
|
match split v1 t2 with
|
2003-06-23 00:28:34 -07:00
|
|
|
(l2, false, r2) ->
|
1995-05-30 06:33:57 -07:00
|
|
|
join (diff l1 l2) v1 (diff r1 r2)
|
2003-06-23 00:28:34 -07:00
|
|
|
| (l2, true, r2) ->
|
1995-05-30 06:33:57 -07:00
|
|
|
concat (diff l1 l2) (diff r1 r2)
|
|
|
|
|
2004-04-23 03:01:54 -07:00
|
|
|
type enumeration = End | More of elt * t * enumeration
|
|
|
|
|
|
|
|
let rec cons_enum s e =
|
|
|
|
match s with
|
|
|
|
Empty -> e
|
|
|
|
| Node(l, v, r, _) -> cons_enum l (More(v, r, e))
|
|
|
|
|
|
|
|
let rec compare_aux e1 e2 =
|
|
|
|
match (e1, e2) with
|
|
|
|
(End, End) -> 0
|
|
|
|
| (End, _) -> -1
|
|
|
|
| (_, End) -> 1
|
|
|
|
| (More(v1, r1, e1), More(v2, r2, e2)) ->
|
1995-05-30 06:33:57 -07:00
|
|
|
let c = Ord.compare v1 v2 in
|
2004-04-23 03:01:54 -07:00
|
|
|
if c <> 0
|
|
|
|
then c
|
|
|
|
else compare_aux (cons_enum r1 e1) (cons_enum r2 e2)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let compare s1 s2 =
|
2004-04-23 03:01:54 -07:00
|
|
|
compare_aux (cons_enum s1 End) (cons_enum s2 End)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let equal s1 s2 =
|
|
|
|
compare s1 s2 = 0
|
|
|
|
|
1997-05-10 08:38:17 -07:00
|
|
|
let rec subset s1 s2 =
|
|
|
|
match (s1, s2) with
|
|
|
|
Empty, _ ->
|
|
|
|
true
|
|
|
|
| _, Empty ->
|
|
|
|
false
|
|
|
|
| Node (l1, v1, r1, _), (Node (l2, v2, r2, _) as t2) ->
|
|
|
|
let c = Ord.compare v1 v2 in
|
|
|
|
if c = 0 then
|
|
|
|
subset l1 l2 && subset r1 r2
|
|
|
|
else if c < 0 then
|
|
|
|
subset (Node (l1, v1, Empty, 0)) l2 && subset r1 t2
|
|
|
|
else
|
|
|
|
subset (Node (Empty, v1, r1, 0)) r2 && subset l1 t2
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let rec iter f = function
|
|
|
|
Empty -> ()
|
|
|
|
| Node(l, v, r, _) -> iter f l; f v; iter f r
|
|
|
|
|
|
|
|
let rec fold f s accu =
|
|
|
|
match s with
|
|
|
|
Empty -> accu
|
2004-11-24 16:06:06 -08:00
|
|
|
| Node(l, v, r, _) -> fold f r (f v (fold f l accu))
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2000-04-13 05:16:26 -07:00
|
|
|
let rec for_all p = function
|
|
|
|
Empty -> true
|
|
|
|
| Node(l, v, r, _) -> p v && for_all p l && for_all p r
|
|
|
|
|
|
|
|
let rec exists p = function
|
|
|
|
Empty -> false
|
|
|
|
| Node(l, v, r, _) -> p v || exists p l || exists p r
|
|
|
|
|
2012-01-16 01:03:16 -08:00
|
|
|
let rec filter p = function
|
|
|
|
Empty -> Empty
|
|
|
|
| Node(l, v, r, _) ->
|
2012-05-30 04:28:28 -07:00
|
|
|
(* call [p] in the expected left-to-right order *)
|
|
|
|
let l' = filter p l in
|
|
|
|
let pv = p v in
|
|
|
|
let r' = filter p r in
|
|
|
|
if pv then join l' v r' else concat l' r'
|
2012-01-16 01:03:16 -08:00
|
|
|
|
|
|
|
let rec partition p = function
|
|
|
|
Empty -> (Empty, Empty)
|
|
|
|
| Node(l, v, r, _) ->
|
2012-05-30 04:28:28 -07:00
|
|
|
(* call [p] in the expected left-to-right order *)
|
|
|
|
let (lt, lf) = partition p l in
|
|
|
|
let pv = p v in
|
|
|
|
let (rt, rf) = partition p r in
|
|
|
|
if pv
|
2012-01-16 01:03:16 -08:00
|
|
|
then (join lt v rt, concat lf rf)
|
|
|
|
else (concat lt rt, join lf v rf)
|
2000-04-13 05:16:26 -07:00
|
|
|
|
1995-06-22 03:11:18 -07:00
|
|
|
let rec cardinal = function
|
|
|
|
Empty -> 0
|
|
|
|
| Node(l, v, r, _) -> cardinal l + 1 + cardinal r
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let rec elements_aux accu = function
|
|
|
|
Empty -> accu
|
|
|
|
| Node(l, v, r, _) -> elements_aux (v :: elements_aux accu r) l
|
|
|
|
|
|
|
|
let elements s =
|
|
|
|
elements_aux [] s
|
|
|
|
|
1998-04-27 02:55:50 -07:00
|
|
|
let choose = min_elt
|
|
|
|
|
2013-01-08 01:01:02 -08:00
|
|
|
let rec find x = function
|
|
|
|
Empty -> raise Not_found
|
|
|
|
| Node(l, v, r, _) ->
|
|
|
|
let c = Ord.compare x v in
|
|
|
|
if c = 0 then v
|
|
|
|
else find x (if c < 0 then l else r)
|
1995-05-04 03:15:53 -07:00
|
|
|
end
|