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 /sml/trunk/src/smlnj-lib/Util/redblack-set-fn.sml
ViewVC logotype

Annotation of /sml/trunk/src/smlnj-lib/Util/redblack-set-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log


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

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

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