1 (***********************************************************************)
5 (* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
7 (* Copyright 1996 Institut National de Recherche en Informatique et *)
8 (* en Automatique. All rights reserved. This file is distributed *)
9 (* under the terms of the GNU Library General Public License, with *)
10 (* the special exception on linking described in file ../LICENSE. *)
12 (***********************************************************************)
14 (* $Id: set.ml 6694 2004-11-25 00:06:06Z doligez $ *)
16 (* Sets over ordered types *)
18 module type OrderedType =
21 val compare: t -> t -> int
29 val is_empty: t -> bool
30 val mem: elt -> t -> bool
31 val add: elt -> t -> t
32 val singleton: elt -> t
33 val remove: elt -> t -> t
34 val union: t -> t -> t
35 val inter: t -> t -> t
37 val compare: t -> t -> int
38 val equal: t -> t -> bool
39 val subset: t -> t -> bool
40 val iter: (elt -> unit) -> t -> unit
41 val fold: (elt -> 'a -> 'a) -> t -> 'a -> 'a
42 val for_all: (elt -> bool) -> t -> bool
43 val exists: (elt -> bool) -> t -> bool
44 val filter: (elt -> bool) -> t -> t
45 val partition: (elt -> bool) -> t -> t * t
46 val cardinal: t -> int
47 val elements: t -> elt list
51 val split: elt -> t -> t * bool * t
54 module Make(Ord: OrderedType) =
57 type t = Empty | Node of t * elt * t * int
59 (* Sets are represented by balanced binary trees (the heights of the
60 children differ by at most 2 *)
64 | Node(_, _, _, h) -> h
66 (* Creates a new node with left son l, value v and right son r.
67 We must have all elements of l < v < all elements of r.
68 l and r must be balanced and | height l - height r | <= 2.
69 Inline expansion of height for better speed. *)
72 let hl = match l with Empty -> 0 | Node(_,_,_,h) -> h in
73 let hr = match r with Empty -> 0 | Node(_,_,_,h) -> h in
74 Node(l, v, r, (if hl >= hr then hl + 1 else hr + 1))
76 (* Same as create, but performs one step of rebalancing if necessary.
77 Assumes l and r balanced and | height l - height r | <= 3.
78 Inline expansion of create for better speed in the most frequent case
79 where no rebalancing is required. *)
82 let hl = match l with Empty -> 0 | Node(_,_,_,h) -> h in
83 let hr = match r with Empty -> 0 | Node(_,_,_,h) -> h in
84 if hl > hr + 2 then begin
86 Empty -> invalid_arg "Set.bal"
87 | Node(ll, lv, lr, _) ->
88 if height ll >= height lr then
89 create ll lv (create lr v r)
92 Empty -> invalid_arg "Set.bal"
93 | Node(lrl, lrv, lrr, _)->
94 create (create ll lv lrl) lrv (create lrr v r)
96 end else if hr > hl + 2 then begin
98 Empty -> invalid_arg "Set.bal"
99 | Node(rl, rv, rr, _) ->
100 if height rr >= height rl then
101 create (create l v rl) rv rr
104 Empty -> invalid_arg "Set.bal"
105 | Node(rll, rlv, rlr, _) ->
106 create (create l v rll) rlv (create rlr rv rr)
109 Node(l, v, r, (if hl >= hr then hl + 1 else hr + 1))
111 (* Insertion of one element *)
113 let rec add x = function
114 Empty -> Node(Empty, x, Empty, 1)
115 | Node(l, v, r, _) as t ->
116 let c = Ord.compare x v in
118 if c < 0 then bal (add x l) v r else bal l v (add x r)
120 (* Same as create and bal, but no assumptions are made on the
121 relative heights of l and r. *)
125 (Empty, _) -> add v r
126 | (_, Empty) -> add v l
127 | (Node(ll, lv, lr, lh), Node(rl, rv, rr, rh)) ->
128 if lh > rh + 2 then bal ll lv (join lr v r) else
129 if rh > lh + 2 then bal (join l v rl) rv rr else
132 (* Smallest and greatest element of a set *)
134 let rec min_elt = function
135 Empty -> raise Not_found
136 | Node(Empty, v, r, _) -> v
137 | Node(l, v, r, _) -> min_elt l
139 let rec max_elt = function
140 Empty -> raise Not_found
141 | Node(l, v, Empty, _) -> v
142 | Node(l, v, r, _) -> max_elt r
144 (* Remove the smallest element of the given set *)
146 let rec remove_min_elt = function
147 Empty -> invalid_arg "Set.remove_min_elt"
148 | Node(Empty, v, r, _) -> r
149 | Node(l, v, r, _) -> bal (remove_min_elt l) v r
151 (* Merge two trees l and r into one.
152 All elements of l must precede the elements of r.
153 Assume | height l - height r | <= 2. *)
159 | (_, _) -> bal t1 (min_elt t2) (remove_min_elt t2)
161 (* Merge two trees l and r into one.
162 All elements of l must precede the elements of r.
163 No assumption on the heights of l and r. *)
169 | (_, _) -> join t1 (min_elt t2) (remove_min_elt t2)
171 (* Splitting. split x s returns a triple (l, present, r) where
172 - l is the set of elements of s that are < x
173 - r is the set of elements of s that are > x
174 - present is false if s contains no element equal to x,
175 or true if s contains an element equal to x. *)
177 let rec split x = function
179 (Empty, false, Empty)
180 | Node(l, v, r, _) ->
181 let c = Ord.compare x v in
182 if c = 0 then (l, true, r)
184 let (ll, pres, rl) = split x l in (ll, pres, join rl v r)
186 let (lr, pres, rr) = split x r in (join l v lr, pres, rr)
188 (* Implementation of the set operations *)
192 let is_empty = function Empty -> true | _ -> false
194 let rec mem x = function
196 | Node(l, v, r, _) ->
197 let c = Ord.compare x v in
198 c = 0 || mem x (if c < 0 then l else r)
200 let singleton x = Node(Empty, x, Empty, 1)
202 let rec remove x = function
204 | Node(l, v, r, _) ->
205 let c = Ord.compare x v in
206 if c = 0 then merge l r else
207 if c < 0 then bal (remove x l) v r else bal l v (remove x r)
209 let rec union s1 s2 =
213 | (Node(l1, v1, r1, h1), Node(l2, v2, r2, h2)) ->
215 if h2 = 1 then add v2 s1 else begin
216 let (l2, _, r2) = split v1 s2 in
217 join (union l1 l2) v1 (union r1 r2)
220 if h1 = 1 then add v1 s2 else begin
221 let (l1, _, r1) = split v2 s1 in
222 join (union l1 l2) v2 (union r1 r2)
225 let rec inter s1 s2 =
228 | (t1, Empty) -> Empty
229 | (Node(l1, v1, r1, _), t2) ->
230 match split v1 t2 with
232 concat (inter l1 l2) (inter r1 r2)
234 join (inter l1 l2) v1 (inter r1 r2)
240 | (Node(l1, v1, r1, _), t2) ->
241 match split v1 t2 with
243 join (diff l1 l2) v1 (diff r1 r2)
245 concat (diff l1 l2) (diff r1 r2)
247 type enumeration = End | More of elt * t * enumeration
249 let rec cons_enum s e =
252 | Node(l, v, r, _) -> cons_enum l (More(v, r, e))
254 let rec compare_aux e1 e2 =
259 | (More(v1, r1, e1), More(v2, r2, e2)) ->
260 let c = Ord.compare v1 v2 in
263 else compare_aux (cons_enum r1 e1) (cons_enum r2 e2)
266 compare_aux (cons_enum s1 End) (cons_enum s2 End)
271 let rec subset s1 s2 =
277 | Node (l1, v1, r1, _), (Node (l2, v2, r2, _) as t2) ->
278 let c = Ord.compare v1 v2 in
280 subset l1 l2 && subset r1 r2
282 subset (Node (l1, v1, Empty, 0)) l2 && subset r1 t2
284 subset (Node (Empty, v1, r1, 0)) r2 && subset l1 t2
286 let rec iter f = function
288 | Node(l, v, r, _) -> iter f l; f v; iter f r
290 let rec fold f s accu =
293 | Node(l, v, r, _) -> fold f r (f v (fold f l accu))
295 let rec for_all p = function
297 | Node(l, v, r, _) -> p v && for_all p l && for_all p r
299 let rec exists p = function
301 | Node(l, v, r, _) -> p v || exists p l || exists p r
304 let rec filt accu = function
306 | Node(l, v, r, _) ->
307 filt (filt (if p v then add v accu else accu) l) r in
311 let rec part (t, f as accu) = function
313 | Node(l, v, r, _) ->
314 part (part (if p v then (add v t, f) else (t, add v f)) l) r in
315 part (Empty, Empty) s
317 let rec cardinal = function
319 | Node(l, v, r, _) -> cardinal l + 1 + cardinal r
321 let rec elements_aux accu = function
323 | Node(l, v, r, _) -> elements_aux (v :: elements_aux accu r) l