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/branches/rt-transition/Util/word-redblack-set.sml
ViewVC logotype

Annotation of /smlnj-lib/branches/rt-transition/Util/word-redblack-set.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4070 - (view) (download)

1 : jhr 702 (* word-redblack-set.sml
2 :     *
3 : jhr 4070 * COPYRIGHT (c) 2014 The Fellowship of SML/NJ (http://www.smlnj.org)
4 :     * All rights reserved.
5 : jhr 702 *
6 : jhr 4070 * COPYRIGHT (c) 1999 Bell Labs, Lucent Technologies.
7 :     *
8 : jhr 702 * This code is based on Chris Okasaki's implementation of
9 :     * red-black trees. The linear-time tree construction code is
10 :     * based on the paper "Constructing red-black trees" by Hinze,
11 :     * and the delete function is based on the description in Cormen,
12 :     * Leiserson, and Rivest.
13 :     *
14 :     * A red-black tree should satisfy the following two invariants:
15 :     *
16 : jhr 4070 * Red Invariant: each red node has black children (empty nodes are
17 :     * considered black).
18 : jhr 702 *
19 : jhr 4070 * Black Invariant: each path from the root to an empty node has the
20 : jhr 702 * same number of black nodes (the tree's black height).
21 :     *
22 : jhr 4070 * The Black invariant implies that any node with only one child
23 :     * will be black and its child will be a red leaf.
24 : jhr 702 *)
25 :    
26 :     structure WordRedBlackSet :> ORD_SET where type Key.ord_key = word =
27 :     struct
28 :    
29 :     structure Key =
30 :     struct
31 :     type ord_key = word
32 :     val compare = Word.compare
33 :     end
34 :    
35 : jhr 4070 type item = Key.ord_key
36 : jhr 702
37 :     datatype color = R | B
38 :    
39 :     datatype tree
40 :     = E
41 :     | T of (color * tree * item * tree)
42 :    
43 :     datatype set = SET of (int * tree)
44 :    
45 :     fun isEmpty (SET(_, E)) = true
46 :     | isEmpty _ = false
47 :    
48 :     val empty = SET(0, E)
49 :    
50 : jhr 4070 fun singleton x = SET(1, T(B, E, x, E))
51 : jhr 702
52 :     fun add (SET(nItems, m), x) = let
53 :     val nItems' = ref nItems
54 :     fun ins E = (nItems' := nItems+1; T(R, E, x, E))
55 :     | ins (s as T(color, a, y, b)) =
56 :     if (x < y)
57 :     then (case a
58 :     of T(R, c, z, d) =>
59 :     if (x < z)
60 :     then (case ins c
61 : jhr 4070 of T(R, e, w, f) => T(R, T(B,e,w,f), z, T(B,d,y,b))
62 : jhr 702 | c => T(B, T(R,c,z,d), y, b)
63 :     (* end case *))
64 :     else if (x = z)
65 :     then T(color, T(R, c, x, d), y, b)
66 :     else (case ins d
67 : jhr 4070 of T(R, e, w, f) => T(R, T(B,c,z,e), w, T(B,f,y,b))
68 : jhr 702 | d => T(B, T(R,c,z,d), y, b)
69 :     (* end case *))
70 :     | _ => T(B, ins a, y, b)
71 :     (* end case *))
72 :     else if (x = y)
73 :     then T(color, a, x, b)
74 :     else (case b
75 :     of T(R, c, z, d) =>
76 :     if (x < z)
77 :     then (case ins c
78 : jhr 4070 of T(R, e, w, f) => T(R, T(B,a,y,e), w, T(B,f,z,d))
79 : jhr 702 | 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 : jhr 4070 of T(R, e, w, f) => T(R, T(B,a,y,c), z, T(B,e,w,f))
85 : jhr 702 | d => T(B, a, y, T(R,c,z,d))
86 :     (* end case *))
87 :     | _ => T(B, a, y, ins b)
88 :     (* end case *))
89 : jhr 4070 val T(_, a, y, b) = ins m
90 : jhr 702 in
91 : jhr 4070 SET(!nItems', T(B, a, y, b))
92 : jhr 702 end
93 :     fun add' (x, m) = add (m, x)
94 :    
95 :     fun addList (s, []) = s
96 :     | addList (s, x::r) = addList(add(s, x), r)
97 :    
98 :     (* Remove an item. Raises LibBase.NotFound if not found. *)
99 :     local
100 :     datatype zipper
101 :     = TOP
102 :     | LEFT of (color * item * tree * zipper)
103 :     | RIGHT of (color * tree * item * zipper)
104 :     in
105 :     fun delete (SET(nItems, t), k) = let
106 : jhr 4070 (* zip the zipper *)
107 : jhr 702 fun zip (TOP, t) = t
108 : jhr 4070 | zip (LEFT(color, x, b, p), a) = zip(p, T(color, a, x, b))
109 :     | zip (RIGHT(color, a, x, p), b) = zip(p, T(color, a, x, b))
110 :     (* zip the zipper while resolving a black deficit *)
111 :     fun fixupZip (TOP, t) = (true, t)
112 :     (* case 1 from CLR *)
113 :     | fixupZip (LEFT(B, x, T(R, a, y, b), p), t) = (case a
114 :     of T(_, T(R, a11, w, a12), z, a2) => (* case 1L ==> case 3L ==> case 4L *)
115 :     (false, zip (p, T(B, T(R, T(B, t, x, a11), w, T(B, a12, z, a2)), y, b)))
116 :     | T(_, a1, z, T(R, a21, w, t22)) => (* case 1L ==> case 4L *)
117 :     (false, zip (p, T(B, T(R, T(B, t, x, a1), z, T(B, a21, w, t22)), y, b)))
118 :     | T(_, a1, z, a2) => (* case 1L ==> case 2L; rotate + recolor fixes deficit *)
119 :     (false, zip (p, T(B, T(B, t, x, T(R, a1, z, a2)), y, b)))
120 :     | _ => fixupZip (LEFT(R, x, a, LEFT(B, y, b, p)), t)
121 :     (* end case *))
122 :     | fixupZip (RIGHT(B, T(R, a, x, b), y, p), t) = (case b
123 :     of T(_, b1, z, T(R, b21, w, b22)) => (* case 1R ==> case 3R ==> case 4R *)
124 :     (false, zip (p, T(B, a, x, T(R, T(B, b1, z, b21), w, T(B, b22, y, t)))))
125 :     | T(_, T(R, b11, w, b12), z, b2) => (* case 1R ==> case 4R *)
126 :     (false, zip (p, T(B, a, x, T(R, T(B, b11, w, b12), z, T(B, b2, y, t)))))
127 :     | T(_, b1, z, b2) => (* case 1L ==> case 2L; rotate + recolor fixes deficit *)
128 :     (false, zip (p, T(B, a, x, T(B, T(R, b1, z, b2), y, t))))
129 :     | _ => fixupZip (RIGHT(R, b, y, RIGHT(B, a, x, p)), t)
130 :     (* end case *))
131 :     (* case 3 from CLR *)
132 :     | fixupZip (LEFT(color, x, T(B, T(R, a1, y, a2), z, b), p), t) =
133 :     (* case 3L ==> case 4L *)
134 :     (false, zip (p, T(color, T(B, t, x, a1), y, T(B, a2, z, b))))
135 :     | fixupZip (RIGHT(color, T(B, a, x, T(R, b1, y, b2)), z, p), t) =
136 :     (* case 3R ==> case 4R; rotate, recolor, plus rotate fixes deficit *)
137 :     (false, zip (p, T(color, T(B, a, x, b1), y, T(B, b2, z, t))))
138 :     (* case 4 from CLR *)
139 :     | fixupZip (LEFT(color, x, T(B, a, y, T(R, b1, z, b2)), p), t) =
140 :     (false, zip (p, T(color, T(B, t, x, a), y, T(B, b1, z, b2))))
141 :     | fixupZip (RIGHT(color, T(B, T(R, a1, z, a2), x, b), y, p), t) =
142 :     (false, zip (p, T(color, T(B, a1, z, a2), x, T(B, b, y, t))))
143 :     (* case 2 from CLR; note that "a" and "b" are guaranteed to be black, since we did
144 :     * not match cases 3 or 4.
145 :     *)
146 :     | fixupZip (LEFT(R, x, T(B, a, y, b), p), t) =
147 :     (false, zip (p, T(B, t, x, T(R, a, y, b))))
148 :     | fixupZip (LEFT(B, x, T(B, a, y, b), p), t) =
149 :     fixupZip (p, T(B, t, x, T(R, a, y, b)))
150 :     | fixupZip (RIGHT(R, T(B, a, x, b), y, p), t) =
151 :     (false, zip (p, T(B, T(R, a, x, b), y, t)))
152 :     | fixupZip (RIGHT(B, T(B, a, x, b), y, p), t) =
153 :     fixupZip (p, T(B, T(R, a, x, b), y, t))
154 :     (* push deficit up the tree by recoloring a black node as red *)
155 :     | fixupZip (LEFT(_, y, E, p), t) = fixupZip (p, T(R, t, y, E))
156 :     | fixupZip (RIGHT(_, E, y, p), t) = fixupZip (p, T(R, E, y, t))
157 :     (* impossible cases that violate the red invariant *)
158 :     | fixupZip _ = raise Fail "Red invariant violation"
159 :     (* delete the minimum value from a non-empty tree, returning a triple
160 :     * (elem, bd, tr), where elem is the minimum element, tr is the residual
161 :     * tree with elem removed, and bd is true if tr has a black-depth that is
162 :     * less than the original tree.
163 : jhr 702 *)
164 : jhr 4070 fun delMin (T(R, E, y, b), p) =
165 :     (* replace the node by its right subtree (which must be E) *)
166 :     (y, false, zip(p, b))
167 :     | delMin (T(B, E, y, T(R, a', y', b')), p) =
168 :     (* replace the node with its right child, while recoloring the child black to
169 :     * preserve the black invariant.
170 :     *)
171 :     (y, false, zip (p, T(B, a', y', b')))
172 :     | delMin (T(B, E, y, E), p) = let
173 :     (* delete the node, which reduces the black-depth by one, so we attempt to fix
174 :     * the deficit on the path back.
175 :     *)
176 :     val (blkDeficit, t) = fixupZip (p, E)
177 :     in
178 :     (y, blkDeficit, t)
179 :     end
180 : jhr 702 | delMin (T(color, a, y, b), z) = delMin(a, LEFT(color, y, b, z))
181 :     | delMin (E, _) = raise Match
182 :     fun del (E, z) = raise LibBase.NotFound
183 : jhr 4070 | del (T(color, a, y, b), p) =
184 : jhr 702 if (k < y)
185 : jhr 4070 then del (a, LEFT(color, y, b, p))
186 : jhr 702 else if (k = y)
187 : jhr 4070 then (case (color, a, b)
188 :     of (R, E, E) => zip(p, E)
189 :     | (B, E, E) => #2 (fixupZip (p, E))
190 :     | (_, T(_, a', y', b'), E) =>
191 :     (* node is black and left child is red; we replace the node with its
192 :     * left child recolored to black.
193 :     *)
194 :     zip(p, T(B, a', y', b'))
195 :     | (_, E, T(_, a', y', b')) =>
196 :     (* node is black and right child is red; we replace the node with its
197 :     * right child recolored to black.
198 :     *)
199 :     zip(p, T(B, a', y', b'))
200 :     | _ => let
201 :     val (minSucc, blkDeficit, b) = delMin (b, TOP)
202 :     in
203 :     if blkDeficit
204 :     then #2 (fixupZip (RIGHT(color, a, minSucc, p), b))
205 :     else zip (p, T(color, a, minSucc, b))
206 :     end
207 :     (* end case *))
208 :     else del (b, RIGHT(color, a, y, p))
209 : jhr 702 in
210 : jhr 4070 case del(t, TOP)
211 :     of T(R, a, x, b) => SET(nItems-1, T(B, a, x, b))
212 :     | t => SET(nItems-1, t)
213 :     (* end case *)
214 : jhr 702 end
215 :     end (* local *)
216 :    
217 :     (* Return true if and only if item is an element in the set *)
218 :     fun member (SET(_, t), k) = let
219 :     fun find' E = false
220 :     | find' (T(_, a, y, b)) =
221 :     (k = y) orelse ((k < y) andalso find' a) orelse find' b
222 :     in
223 :     find' t
224 :     end
225 :    
226 :     (* Return the number of items in the map *)
227 :     fun numItems (SET(n, _)) = n
228 :    
229 :     fun foldl f = let
230 :     fun foldf (E, accum) = accum
231 :     | foldf (T(_, a, x, b), accum) =
232 :     foldf(b, f(x, foldf(a, accum)))
233 :     in
234 :     fn init => fn (SET(_, m)) => foldf(m, init)
235 :     end
236 :    
237 :     fun foldr f = let
238 :     fun foldf (E, accum) = accum
239 :     | foldf (T(_, a, x, b), accum) =
240 :     foldf(a, f(x, foldf(b, accum)))
241 :     in
242 :     fn init => fn (SET(_, m)) => foldf(m, init)
243 :     end
244 :    
245 :     (* return an ordered list of the items in the set. *)
246 :     fun listItems s = foldr (fn (x, l) => x::l) [] s
247 :    
248 :     (* functions for walking the tree while keeping a stack of parents
249 :     * to be visited.
250 :     *)
251 :     fun next ((t as T(_, _, _, b))::rest) = (t, left(b, rest))
252 :     | next _ = (E, [])
253 :     and left (E, rest) = rest
254 :     | left (t as T(_, a, _, _), rest) = left(a, t::rest)
255 :     fun start m = left(m, [])
256 :    
257 :     (* Return true if and only if the two sets are equal *)
258 :     fun equal (SET(_, s1), SET(_, s2)) = let
259 :     fun cmp (t1, t2) = (case (next t1, next t2)
260 :     of ((E, _), (E, _)) => true
261 :     | ((E, _), _) => false
262 :     | (_, (E, _)) => false
263 :     | ((T(_, _, x, _), r1), (T(_, _, y, _), r2)) =>
264 :     (x = y) andalso cmp (r1, r2)
265 :     (* end case *))
266 :     in
267 :     cmp (start s1, start s2)
268 :     end
269 :    
270 :     (* Return the lexical order of two sets *)
271 :     fun compare (SET(_, s1), SET(_, s2)) = let
272 :     fun cmp (t1, t2) = (case (next t1, next t2)
273 :     of ((E, _), (E, _)) => EQUAL
274 :     | ((E, _), _) => LESS
275 :     | (_, (E, _)) => GREATER
276 :     | ((T(_, _, x, _), r1), (T(_, _, y, _), r2)) =>
277 :     if (x = y)
278 :     then cmp (r1, r2)
279 :     else if (x < y)
280 :     then LESS
281 :     else GREATER
282 :     (* end case *))
283 :     in
284 :     cmp (start s1, start s2)
285 :     end
286 :    
287 :     (* Return true if and only if the first set is a subset of the second *)
288 :     fun isSubset (SET(_, s1), SET(_, s2)) = let
289 :     fun cmp (t1, t2) = (case (next t1, next t2)
290 :     of ((E, _), (E, _)) => true
291 :     | ((E, _), _) => true
292 :     | (_, (E, _)) => false
293 :     | ((T(_, _, x, _), r1), (T(_, _, y, _), r2)) =>
294 :     ((x = y) andalso cmp (r1, r2))
295 :     orelse ((x > y) andalso cmp (t1, r2))
296 :     (* end case *))
297 :     in
298 :     cmp (start s1, start s2)
299 :     end
300 :    
301 :     (* support for constructing red-black trees in linear time from increasing
302 :     * ordered sequences (based on a description by R. Hinze). Note that the
303 :     * elements in the digits are ordered with the largest on the left, whereas
304 :     * the elements of the trees are ordered with the largest on the right.
305 :     *)
306 :     datatype digit
307 :     = ZERO
308 :     | ONE of (item * tree * digit)
309 :     | TWO of (item * tree * item * tree * digit)
310 :     (* add an item that is guaranteed to be larger than any in l *)
311 :     fun addItem (a, l) = let
312 :     fun incr (a, t, ZERO) = ONE(a, t, ZERO)
313 :     | incr (a1, t1, ONE(a2, t2, r)) = TWO(a1, t1, a2, t2, r)
314 :     | incr (a1, t1, TWO(a2, t2, a3, t3, r)) =
315 :     ONE(a1, t1, incr(a2, T(B, t3, a3, t2), r))
316 :     in
317 :     incr(a, E, l)
318 :     end
319 :     (* link the digits into a tree *)
320 :     fun linkAll t = let
321 :     fun link (t, ZERO) = t
322 :     | link (t1, ONE(a, t2, r)) = link(T(B, t2, a, t1), r)
323 :     | link (t, TWO(a1, t1, a2, t2, r)) =
324 :     link(T(B, T(R, t2, a2, t1), a1, t), r)
325 :     in
326 :     link (E, t)
327 :     end
328 :    
329 : jhr 2272 (* create a set from a list of items; this function works in linear time if the list
330 :     * is in increasing order.
331 :     *)
332 :     fun fromList [] = empty
333 :     | fromList (first::rest) = let
334 :     fun add (prev, x::xs, n, accum) = if (prev < x)
335 :     then add(x, xs, n+1, addItem(x, accum))
336 :     else (* list not in order, so fall back to addList code *)
337 :     addList(SET(n, linkAll accum), x::xs)
338 :     | add (_, [], n, accum) = SET(n, linkAll accum)
339 :     in
340 :     add (first, rest, 1, addItem(first, ZERO))
341 :     end
342 :    
343 : jhr 702 (* return the union of the two sets *)
344 :     fun union (SET(_, s1), SET(_, s2)) = let
345 :     fun ins ((E, _), n, result) = (n, result)
346 :     | ins ((T(_, _, x, _), r), n, result) =
347 :     ins(next r, n+1, addItem(x, result))
348 :     fun union' (t1, t2, n, result) = (case (next t1, next t2)
349 :     of ((E, _), (E, _)) => (n, result)
350 :     | ((E, _), t2) => ins(t2, n, result)
351 :     | (t1, (E, _)) => ins(t1, n, result)
352 :     | ((T(_, _, x, _), r1), (T(_, _, y, _), r2)) =>
353 :     if (x < y)
354 :     then union' (r1, t2, n+1, addItem(x, result))
355 :     else if (x = y)
356 :     then union' (r1, r2, n+1, addItem(x, result))
357 :     else union' (t1, r2, n+1, addItem(y, result))
358 :     (* end case *))
359 :     val (n, result) = union' (start s1, start s2, 0, ZERO)
360 :     in
361 :     SET(n, linkAll result)
362 :     end
363 :    
364 :     (* return the intersection of the two sets *)
365 :     fun intersection (SET(_, s1), SET(_, s2)) = let
366 :     fun intersect (t1, t2, n, result) = (case (next t1, next t2)
367 :     of ((T(_, _, x, _), r1), (T(_, _, y, _), r2)) =>
368 :     if (x < y)
369 :     then intersect (r1, t2, n, result)
370 :     else if (x = y)
371 :     then intersect (r1, r2, n+1, addItem(x, result))
372 :     else intersect (t1, r2, n, result)
373 :     | _ => (n, result)
374 :     (* end case *))
375 :     val (n, result) = intersect (start s1, start s2, 0, ZERO)
376 :     in
377 :     SET(n, linkAll result)
378 :     end
379 :    
380 :     (* return the set difference *)
381 :     fun difference (SET(_, s1), SET(_, s2)) = let
382 :     fun ins ((E, _), n, result) = (n, result)
383 :     | ins ((T(_, _, x, _), r), n, result) =
384 :     ins(next r, n+1, addItem(x, result))
385 :     fun diff (t1, t2, n, result) = (case (next t1, next t2)
386 :     of ((E, _), _) => (n, result)
387 :     | (t1, (E, _)) => ins(t1, n, result)
388 :     | ((T(_, _, x, _), r1), (T(_, _, y, _), r2)) =>
389 :     if (x < y)
390 :     then diff (r1, t2, n+1, addItem(x, result))
391 :     else if (x = y)
392 :     then diff (r1, r2, n, result)
393 :     else diff (t1, r2, n, result)
394 :     (* end case *))
395 :     val (n, result) = diff (start s1, start s2, 0, ZERO)
396 :     in
397 :     SET(n, linkAll result)
398 :     end
399 :    
400 : jhr 3736 fun subtract (s, item) = difference (s, singleton item)
401 :     fun subtract' (item, s) = subtract (s, item)
402 :    
403 :     fun subtractList (l, items) = let
404 :     val items' = List.foldl (fn (x, set) => add(set, x)) (SET(0, E)) items
405 :     in
406 :     difference (l, items')
407 :     end
408 :    
409 : jhr 702 fun app f = let
410 :     fun appf E = ()
411 :     | appf (T(_, a, x, b)) = (appf a; f x; appf b)
412 :     in
413 :     fn (SET(_, m)) => appf m
414 :     end
415 :    
416 :     fun map f = let
417 :     fun addf (x, m) = add(m, f x)
418 :     in
419 :     foldl addf empty
420 :     end
421 :    
422 :     (* Filter out those elements of the set that do not satisfy the
423 :     * predicate. The filtering is done in increasing map order.
424 :     *)
425 :     fun filter pred (SET(_, t)) = let
426 :     fun walk (E, n, result) = (n, result)
427 :     | walk (T(_, a, x, b), n, result) = let
428 :     val (n, result) = walk(a, n, result)
429 :     in
430 :     if (pred x)
431 :     then walk(b, n+1, addItem(x, result))
432 :     else walk(b, n, result)
433 :     end
434 :     val (n, result) = walk (t, 0, ZERO)
435 :     in
436 :     SET(n, linkAll result)
437 :     end
438 :    
439 : jhr 816 fun partition pred (SET(_, t)) = let
440 :     fun walk (E, n1, result1, n2, result2) = (n1, result1, n2, result2)
441 :     | walk (T(_, a, x, b), n1, result1, n2, result2) = let
442 :     val (n1, result1, n2, result2) = walk(a, n1, result1, n2, result2)
443 :     in
444 :     if (pred x)
445 :     then walk(b, n1+1, addItem(x, result1), n2, result2)
446 :     else walk(b, n1, result1, n2+1, addItem(x, result2))
447 :     end
448 :     val (n1, result1, n2, result2) = walk (t, 0, ZERO, 0, ZERO)
449 :     in
450 :     (SET(n1, linkAll result1), SET(n2, linkAll result2))
451 :     end
452 :    
453 : jhr 702 fun exists pred = let
454 :     fun test E = false
455 :     | test (T(_, a, x, b)) = test a orelse pred x orelse test b
456 :     in
457 :     fn (SET(_, t)) => test t
458 :     end
459 :    
460 :     fun all pred = let
461 :     fun test E = true
462 :     | test (T(_, a, x, b)) = test a andalso pred x andalso test b
463 :     in
464 :     fn (SET(_, t)) => test t
465 :     end
466 :    
467 :     fun find pred = let
468 :     fun test E = NONE
469 :     | test (T(_, a, x, b)) = (case test a
470 :     of NONE => if pred x then SOME x else test b
471 :     | someItem => someItem
472 :     (* end case *))
473 :     in
474 :     fn (SET(_, t)) => test t
475 :     end
476 :    
477 :     end;

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