Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /smlnj-lib/releases/release-110.61/Util/int-redblack-set.sml
ViewVC logotype

Annotation of /smlnj-lib/releases/release-110.61/Util/int-redblack-set.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 468 - (view) (download)
Original Path: sml/branches/SMLNJ/src/smlnj-lib/Util/int-redblack-set.sml

1 : monnier 467 (* int-redblack-set.sml
2 :     *
3 :     * COPYRIGHT (c) 1999 Bell Labs, Lucent Technologies.
4 :     *
5 :     * This code is based on Chris Okasaki's implementation of
6 :     * red-black trees. The linear-time tree construction code is
7 :     * based on the paper "Constructing red-black trees" by Hinze,
8 :     * and the delete function is based on the description in Cormen,
9 :     * Leiserson, and Rivest.
10 :     *
11 :     * A red-black tree should satisfy the following two invariants:
12 :     *
13 :     * Red Invariant: each red node has a black parent.
14 :     *
15 :     * Black Condition: each path from the root to an empty node has the
16 :     * same number of black nodes (the tree's black height).
17 :     *
18 :     * The Red condition implies that the root is always black and the Black
19 :     * condition implies that any node with only one child will be black and
20 :     * its child will be a red leaf.
21 :     *)
22 :    
23 :     structure IntRedBlackSet :> ORD_SET where type Key.ord_key = int =
24 :     struct
25 :    
26 :     structure Key =
27 :     struct
28 :     type ord_key = int
29 :     val compare = Int.compare
30 :     end
31 :    
32 :     type item = int
33 :    
34 :     datatype color = R | B
35 :    
36 :     datatype tree
37 :     = E
38 :     | T of (color * tree * item * tree)
39 :    
40 :     datatype set = SET of (int * tree)
41 :    
42 :     fun isEmpty (SET(_, E)) = true
43 :     | isEmpty _ = false
44 :    
45 :     val empty = SET(0, E)
46 :    
47 :     fun singleton x = SET(1, T(R, E, x, E))
48 :    
49 :     fun add (SET(nItems, m), x) = let
50 :     val nItems' = ref nItems
51 :     fun ins E = (nItems' := nItems+1; T(R, E, x, E))
52 :     | ins (s as T(color, a, y, b)) =
53 :     if (x < y)
54 :     then (case a
55 :     of T(R, c, z, d) =>
56 :     if (x < z)
57 :     then (case ins c
58 :     of T(R, e, w, f) =>
59 :     T(R, T(B,e,w,f), z, T(B,d,y,b))
60 :     | c => T(B, T(R,c,z,d), y, b)
61 :     (* end case *))
62 :     else if (x = z)
63 :     then T(color, T(R, c, x, d), y, b)
64 :     else (case ins d
65 :     of T(R, e, w, f) =>
66 :     T(R, T(B,c,z,e), w, T(B,f,y,b))
67 :     | d => T(B, T(R,c,z,d), y, b)
68 :     (* end case *))
69 :     | _ => T(B, ins a, y, b)
70 :     (* end case *))
71 :     else if (x = y)
72 :     then T(color, a, x, b)
73 :     else (case b
74 :     of T(R, c, z, d) =>
75 :     if (x < z)
76 :     then (case ins c
77 :     of T(R, e, w, f) =>
78 :     T(R, T(B,a,y,e), w, T(B,f,z,d))
79 :     | c => T(B, a, y, T(R,c,z,d))
80 :     (* end case *))
81 :     else if (x = z)
82 :     then T(color, a, y, T(R, c, x, d))
83 :     else (case ins d
84 :     of T(R, e, w, f) =>
85 :     T(R, T(B,a,y,c), z, T(B,e,w,f))
86 :     | d => T(B, a, y, T(R,c,z,d))
87 :     (* end case *))
88 :     | _ => T(B, a, y, ins b)
89 :     (* end case *))
90 :     val m = ins m
91 :     in
92 :     SET(!nItems', m)
93 :     end
94 :     fun add' (x, m) = add (m, x)
95 :    
96 :     fun addList (s, []) = s
97 :     | addList (s, x::r) = addList(add(s, x), r)
98 :    
99 :     (* Remove an item. Raises LibBase.NotFound if not found. *)
100 :     local
101 :     datatype zipper
102 :     = TOP
103 :     | LEFT of (color * int * tree * zipper)
104 :     | RIGHT of (color * tree * int * zipper)
105 :     in
106 :     fun delete (SET(nItems, t), k) = let
107 :     fun zip (TOP, t) = t
108 :     | zip (LEFT(color, x, b, z), a) = zip(z, T(color, a, x, b))
109 :     | zip (RIGHT(color, a, x, z), b) = zip(z, T(color, a, x, b))
110 :     (* bbZip propagates a black deficit up the tree until either the top
111 :     * is reached, or the deficit can be covered. It returns a boolean
112 :     * that is true if there is still a deficit and the zipped tree.
113 :     *)
114 :     fun bbZip (TOP, t) = (true, t)
115 :     | bbZip (LEFT(B, x, T(R, c, y, d), z), a) = (* case 1L *)
116 :     bbZip (LEFT(R, x, c, LEFT(B, y, d, z)), a)
117 :     | bbZip (LEFT(color, x, T(B, T(R, c, y, d), w, e), z), a) = (* case 3L *)
118 :     bbZip (LEFT(color, x, T(B, c, y, T(R, d, w, e)), z), a)
119 :     | bbZip (LEFT(color, x, T(B, c, y, T(R, d, w, e)), z), a) = (* case 4L *)
120 :     (false, zip (z, T(color, T(B, a, x, c), y, T(B, d, w, e))))
121 :     | bbZip (LEFT(R, x, T(B, c, y, d), z), a) = (* case 2L *)
122 :     (false, zip (z, T(B, a, x, T(R, c, y, d))))
123 :     | bbZip (LEFT(B, x, T(B, c, y, d), z), a) = (* case 2L *)
124 :     bbZip (z, T(B, a, x, T(R, c, y, d)))
125 :     | bbZip (RIGHT(color, T(R, c, y, d), x, z), b) = (* case 1R *)
126 :     bbZip (RIGHT(R, d, x, RIGHT(B, c, y, z)), b)
127 :     | bbZip (RIGHT(color, T(B, T(R, c, w, d), y, e), x, z), b) = (* case 3R *)
128 :     bbZip (RIGHT(color, T(B, c, w, T(R, d, y, e)), x, z), b)
129 :     | bbZip (RIGHT(color, T(B, c, y, T(R, d, w, e)), x, z), b) = (* case 4R *)
130 :     (false, zip (z, T(color, c, y, T(B, T(R, d, w, e), x, b))))
131 :     | bbZip (RIGHT(R, T(B, c, y, d), x, z), b) = (* case 2R *)
132 :     (false, zip (z, T(B, T(R, c, y, d), x, b)))
133 :     | bbZip (RIGHT(B, T(B, c, y, d), x, z), b) = (* case 2R *)
134 :     bbZip (z, T(B, T(R, c, y, d), x, b))
135 :     | bbZip (z, t) = (false, zip(z, t))
136 :     fun delMin (T(R, E, y, b), z) = (y, (false, zip(z, b)))
137 :     | delMin (T(B, E, y, b), z) = (y, bbZip(z, b))
138 :     | delMin (T(color, a, y, b), z) = delMin(a, LEFT(color, y, b, z))
139 :     fun join (R, E, E, z) = zip(z, E)
140 :     | join (_, a, E, z) = #2(bbZip(z, a)) (* color = black *)
141 :     | join (_, E, b, z) = #2(bbZip(z, b)) (* color = black *)
142 :     | join (color, a, b, z) = let
143 :     val (x, (needB, b')) = delMin(b, TOP)
144 :     in
145 :     if needB
146 :     then #2(bbZip(z, T(color, a, x, b')))
147 :     else zip(z, T(color, a, x, b'))
148 :     end
149 :     fun del (E, z) = raise LibBase.NotFound
150 :     | del (T(color, a, y, b), z) =
151 :     if (k < y)
152 :     then del (a, LEFT(color, y, b, z))
153 :     else if (k = y)
154 :     then join (color, a, b, z)
155 :     else del (b, RIGHT(color, a, y, z))
156 :     in
157 :     SET(nItems-1, del(t, TOP))
158 :     end
159 :     end (* local *)
160 :    
161 :     (* Return true if and only if item is an element in the set *)
162 :     fun member (SET(_, t), k) = let
163 :     fun find' E = false
164 :     | find' (T(_, a, y, b)) =
165 :     (k = y) orelse ((k < y) andalso find' a) orelse find' b
166 :     in
167 :     find' t
168 :     end
169 :    
170 :     (* Return the number of items in the map *)
171 :     fun numItems (SET(n, _)) = n
172 :    
173 :     fun foldl f = let
174 :     fun foldf (E, accum) = accum
175 :     | foldf (T(_, a, x, b), accum) =
176 :     foldf(b, f(x, foldf(a, accum)))
177 :     in
178 :     fn init => fn (SET(_, m)) => foldf(m, init)
179 :     end
180 :    
181 :     fun foldr f = let
182 :     fun foldf (E, accum) = accum
183 :     | foldf (T(_, a, x, b), accum) =
184 :     foldf(a, f(x, foldf(b, accum)))
185 :     in
186 :     fn init => fn (SET(_, m)) => foldf(m, init)
187 :     end
188 :    
189 :     (* return an ordered list of the items in the set. *)
190 :     fun listItems s = foldr (fn (x, l) => x::l) [] s
191 :    
192 :     (* functions for walking the tree while keeping a stack of parents
193 :     * to be visited.
194 :     *)
195 :     fun next ((t as T(_, _, _, b))::rest) = (t, left(b, rest))
196 :     | next _ = (E, [])
197 :     and left (E, rest) = rest
198 :     | left (t as T(_, a, _, _), rest) = left(a, t::rest)
199 :     fun start m = left(m, [])
200 :    
201 :     (* Return true if and only if the two sets are equal *)
202 :     fun equal (SET(_, s1), SET(_, s2)) = let
203 :     fun cmp (t1, t2) = (case (next t1, next t2)
204 :     of ((E, _), (E, _)) => true
205 :     | ((E, _), _) => false
206 :     | (_, (E, _)) => false
207 :     | ((T(_, _, x, _), r1), (T(_, _, y, _), r2)) =>
208 :     (x = y) andalso cmp (r1, r2)
209 :     (* end case *))
210 :     in
211 :     cmp (start s1, start s2)
212 :     end
213 :    
214 :     (* Return the lexical order of two sets *)
215 :     fun compare (SET(_, s1), SET(_, s2)) = let
216 :     fun cmp (t1, t2) = (case (next t1, next t2)
217 :     of ((E, _), (E, _)) => EQUAL
218 :     | ((E, _), _) => LESS
219 :     | (_, (E, _)) => GREATER
220 :     | ((T(_, _, x, _), r1), (T(_, _, y, _), r2)) =>
221 :     if (x = y)
222 :     then cmp (r1, r2)
223 :     else if (x < y)
224 :     then LESS
225 :     else GREATER
226 :     (* end case *))
227 :     in
228 :     cmp (start s1, start s2)
229 :     end
230 :    
231 :     (* Return true if and only if the first set is a subset of the second *)
232 :     fun isSubset (SET(_, s1), SET(_, s2)) = let
233 :     fun cmp (t1, t2) = (case (next t1, next t2)
234 :     of ((E, _), (E, _)) => true
235 :     | ((E, _), _) => true
236 :     | (_, (E, _)) => false
237 :     | ((T(_, _, x, _), r1), (T(_, _, y, _), r2)) =>
238 :     ((x = y) andalso cmp (r1, r2))
239 :     orelse ((x > y) andalso cmp (t1, r2))
240 :     (* end case *))
241 :     in
242 :     cmp (start s1, start s2)
243 :     end
244 :    
245 :     (* support for constructing red-black trees in linear time from ordered
246 :     * sequences (based on a description by R. Hinze).
247 :     *)
248 :     datatype digit
249 :     = ZERO
250 :     | ONE of (int * tree * digit)
251 :     | TWO of (int * tree * int * tree * digit)
252 :     fun addItem (a, l) = let
253 :     fun incr (a, t, ZERO) = ONE(a, t, ZERO)
254 :     | incr (a1, t1, ONE(a2, t2, r)) = TWO(a1, t1, a2, t2, r)
255 :     | incr (a1, t1, TWO(a2, t2, a3, t3, r)) =
256 :     ONE(a1, t1, incr(a2, T(B, t2, a3, t3), r))
257 :     in
258 :     incr(a, E, l)
259 :     end
260 :     fun linkAll t = let
261 :     fun link (t, ZERO) = t
262 :     | link (t1, ONE(a, t2, r)) = link(T(B, t1, a, t2), r)
263 :     | link (t, TWO(a1, t1, a2, t2, r)) =
264 :     link(T(B, T(R, t, a1, t1), a2, t2), r)
265 :     in
266 :     link (E, t)
267 :     end
268 :    
269 :     (* return the union of the two sets *)
270 :     fun union (SET(_, s1), SET(_, s2)) = let
271 :     fun ins ((E, _), n, result) = (n, result)
272 :     | ins ((T(_, _, x, _), r), n, result) =
273 :     ins(next r, n+1, addItem(x, result))
274 :     fun union' (t1, t2, n, result) = (case (next t1, next t2)
275 :     of ((E, _), (E, _)) => (n, result)
276 :     | ((E, _), t2) => ins(t2, n, result)
277 :     | (t1, (E, _)) => ins(t1, n, result)
278 :     | ((T(_, _, x, _), r1), (T(_, _, y, _), r2)) =>
279 :     if (x < y)
280 :     then union' (r1, t2, n+1, addItem(x, result))
281 :     else if (x = y)
282 :     then union' (r1, r2, n+1, addItem(x, result))
283 :     else union' (t1, r2, n+1, addItem(y, result))
284 :     (* end case *))
285 :     val (n, result) = union' (start s1, start s2, 0, ZERO)
286 :     in
287 :     SET(n, linkAll result)
288 :     end
289 :    
290 :     (* return the intersection of the two sets *)
291 :     fun intersection (SET(_, s1), SET(_, s2)) = let
292 :     fun intersect (t1, t2, n, result) = (case (next t1, next t2)
293 :     of ((T(_, _, x, _), r1), (T(_, _, y, _), r2)) =>
294 :     if (x < y)
295 :     then intersect (r1, t2, n, result)
296 :     else if (x = y)
297 :     then intersect (r1, r2, n+1, addItem(x, result))
298 :     else intersect (t1, r2, n, result)
299 :     | _ => (n, result)
300 :     (* end case *))
301 :     val (n, result) = intersect (start s1, start s2, 0, ZERO)
302 :     in
303 :     SET(n, linkAll result)
304 :     end
305 :    
306 :     (* return the set difference *)
307 :     fun difference (SET(_, s1), SET(_, s2)) = let
308 :     fun ins ((E, _), n, result) = (n, result)
309 :     | ins ((T(_, _, x, _), r), n, result) =
310 :     ins(next r, n+1, addItem(x, result))
311 :     fun diff (t1, t2, n, result) = (case (next t1, next t2)
312 :     of ((E, _), _) => (n, result)
313 :     | (t1, (E, _)) => ins(t1, n, result)
314 :     | ((T(_, _, x, _), r1), (T(_, _, y, _), r2)) =>
315 :     if (x < y)
316 :     then diff (r1, t2, n+1, addItem(x, result))
317 :     else if (x = y)
318 :     then diff (r1, r2, n, result)
319 :     else diff (t1, r2, n, result)
320 :     (* end case *))
321 :     val (n, result) = diff (start s1, start s2, 0, ZERO)
322 :     in
323 :     SET(n, linkAll result)
324 :     end
325 :    
326 :     fun app f = let
327 :     fun appf E = ()
328 :     | appf (T(_, a, x, b)) = (appf a; f x; appf b)
329 :     in
330 :     fn (SET(_, m)) => appf m
331 :     end
332 :    
333 :     fun map f = let
334 :     fun addf (x, m) = add(m, f x)
335 :     in
336 :     foldl addf empty
337 :     end
338 :    
339 :     (* Filter out those elements of the set that do not satisfy the
340 :     * predicate. The filtering is done in increasing map order.
341 :     *)
342 :     fun filter pred (SET(_, t)) = let
343 :     fun walk (E, n, result) = (n, result)
344 :     | walk (T(_, a, x, b), n, result) = let
345 :     val (n, result) = walk(a, n, result)
346 :     in
347 :     if (pred x)
348 :     then walk(b, n+1, addItem(x, result))
349 :     else walk(b, n, result)
350 :     end
351 :     val (n, result) = walk (t, 0, ZERO)
352 :     in
353 :     SET(n, linkAll result)
354 :     end
355 :    
356 :     fun exists pred = let
357 :     fun test E = false
358 :     | test (T(_, a, x, b)) = test a orelse pred x orelse test b
359 :     in
360 :     fn (SET(_, t)) => test t
361 :     end
362 :    
363 :     fun all pred = let
364 :     fun test E = true
365 :     | test (T(_, a, x, b)) = test a andalso pred x andalso test b
366 :     in
367 :     fn (SET(_, t)) => test t
368 :     end
369 :    
370 :     fun find pred = let
371 :     fun test E = NONE
372 :     | test (T(_, a, x, b)) = (case test a
373 :     of NONE => if pred x then SOME x else test b
374 :     | someItem => someItem
375 :     (* end case *))
376 :     in
377 :     fn (SET(_, t)) => test t
378 :     end
379 :    
380 :     end;

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0