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 475 - (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 : 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 (int * tree * digit)
253 :     | TWO of (int * tree * int * 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 :     if (x < y)
284 :     then union' (r1, t2, n+1, addItem(x, result))
285 :     else if (x = y)
286 :     then union' (r1, r2, n+1, addItem(x, result))
287 :     else union' (t1, r2, n+1, addItem(y, result))
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 :     if (x < y)
299 :     then intersect (r1, t2, n, result)
300 :     else if (x = y)
301 :     then intersect (r1, r2, n+1, addItem(x, result))
302 :     else intersect (t1, r2, n, result)
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 :     if (x < y)
320 :     then diff (r1, t2, n+1, addItem(x, result))
321 :     else if (x = y)
322 :     then diff (r1, r2, n, result)
323 :     else diff (t1, r2, n, result)
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