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/branches/SMLNJ/src/smlnj-lib/Util/int-binary-set.sml
ViewVC logotype

Annotation of /sml/branches/SMLNJ/src/smlnj-lib/Util/int-binary-set.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (view) (download)

1 : monnier 2 (* int-binary-set.sml
2 :     *
3 :     * COPYRIGHT (c) 1993 by AT&T Bell Laboratories. See COPYRIGHT file for details.
4 :     *
5 :     * This code was adapted from Stephen Adams' binary tree implementation
6 :     * of applicative integer sets.
7 :     *
8 :     * Copyright 1992 Stephen Adams.
9 :     *
10 :     * This software may be used freely provided that:
11 :     * 1. This copyright notice is attached to any copy, derived work,
12 :     * or work including all or part of this software.
13 :     * 2. Any derived work must contain a prominent notice stating that
14 :     * it has been altered from the original.
15 :     *
16 :     * Altered to conform to SML library interface - Emden Gansner
17 :     *
18 :     *
19 :     * Name(s): Stephen Adams.
20 :     * Department, Institution: Electronics & Computer Science,
21 :     * University of Southampton
22 :     * Address: Electronics & Computer Science
23 :     * University of Southampton
24 :     * Southampton SO9 5NH
25 :     * Great Britian
26 :     * E-mail: sra@ecs.soton.ac.uk
27 :     *
28 :     * Comments:
29 :     *
30 :     * 1. The implementation is based on Binary search trees of Bounded
31 :     * Balance, similar to Nievergelt & Reingold, SIAM J. Computing
32 :     * 2(1), March 1973. The main advantage of these trees is that
33 :     * they keep the size of the tree in the node, giving a constant
34 :     * time size operation.
35 :     *
36 :     * 2. The bounded balance criterion is simpler than N&R's alpha.
37 :     * Simply, one subtree must not have more than `weight' times as
38 :     * many elements as the opposite subtree. Rebalancing is
39 :     * guaranteed to reinstate the criterion for weight>2.23, but
40 :     * the occasional incorrect behaviour for weight=2 is not
41 :     * detrimental to performance.
42 :     *
43 :     * 3. There are two implementations of union. The default,
44 :     * hedge_union, is much more complex and usually 20% faster. I
45 :     * am not sure that the performance increase warrants the
46 :     * complexity (and time it took to write), but I am leaving it
47 :     * in for the competition. It is derived from the original
48 :     * union by replacing the split_lt(gt) operations with a lazy
49 :     * version. The `obvious' version is called old_union.
50 :     *
51 :     * 4. Most time is spent in T', the rebalancing constructor. If my
52 :     * understanding of the output of *<file> in the sml batch
53 :     * compiler is correct then the code produced by NJSML 0.75
54 :     * (sparc) for the final case is very disappointing. Most
55 :     * invocations fall through to this case and most of these cases
56 :     * fall to the else part, i.e. the plain contructor,
57 :     * T(v,ln+rn+1,l,r). The poor code allocates a 16 word vector
58 :     * and saves lots of registers into it. In the common case it
59 :     * then retrieves a few of the registers and allocates the 5
60 :     * word T node. The values that it retrieves were live in
61 :     * registers before the massive save.
62 :     *)
63 :    
64 :     structure IntBinarySet :> ORD_SET where type Key.ord_key = Int.int =
65 :     struct
66 :    
67 :     structure Key =
68 :     struct
69 :     type ord_key = Int.int
70 :     val compare = Int.compare
71 :     end
72 :    
73 :     type item = Key.ord_key
74 :    
75 :     datatype set
76 :     = E
77 :     | T of {
78 :     elt : item,
79 :     cnt : int,
80 :     left : set,
81 :     right : set
82 :     }
83 :    
84 :     fun numItems E = 0
85 :     | numItems (T{cnt,...}) = cnt
86 :    
87 :     fun isEmpty E = true
88 :     | isEmpty _ = false
89 :    
90 :     fun mkT(v,n,l,r) = T{elt=v,cnt=n,left=l,right=r}
91 :    
92 :     (* N(v,l,r) = T(v,1+numItems(l)+numItems(r),l,r) *)
93 :     fun N(v,E,E) = mkT(v,1,E,E)
94 :     | N(v,E,r as T{cnt=n,...}) = mkT(v,n+1,E,r)
95 :     | N(v,l as T{cnt=n,...}, E) = mkT(v,n+1,l,E)
96 :     | N(v,l as T{cnt=n,...}, r as T{cnt=m,...}) = mkT(v,n+m+1,l,r)
97 :    
98 :     fun single_L (a,x,T{elt=b,left=y,right=z,...}) = N(b,N(a,x,y),z)
99 :     | single_L _ = raise Match
100 :     fun single_R (b,T{elt=a,left=x,right=y,...},z) = N(a,x,N(b,y,z))
101 :     | single_R _ = raise Match
102 :     fun double_L (a,w,T{elt=c,left=T{elt=b,left=x,right=y,...},right=z,...}) =
103 :     N(b,N(a,w,x),N(c,y,z))
104 :     | double_L _ = raise Match
105 :     fun double_R (c,T{elt=a,left=w,right=T{elt=b,left=x,right=y,...},...},z) =
106 :     N(b,N(a,w,x),N(c,y,z))
107 :     | double_R _ = raise Match
108 :    
109 :     (*
110 :     ** val weight = 3
111 :     ** fun wt i = weight * i
112 :     *)
113 :     fun wt (i : int) = i + i + i
114 :    
115 :     fun T' (v,E,E) = mkT(v,1,E,E)
116 :     | T' (v,E,r as T{left=E,right=E,...}) = mkT(v,2,E,r)
117 :     | T' (v,l as T{left=E,right=E,...},E) = mkT(v,2,l,E)
118 :    
119 :     | T' (p as (_,E,T{left=T _,right=E,...})) = double_L p
120 :     | T' (p as (_,T{left=E,right=T _,...},E)) = double_R p
121 :    
122 :     (* these cases almost never happen with small weight*)
123 :     | T' (p as (_,E,T{left=T{cnt=ln,...},right=T{cnt=rn,...},...})) =
124 :     if ln<rn then single_L p else double_L p
125 :     | T' (p as (_,T{left=T{cnt=ln,...},right=T{cnt=rn,...},...},E)) =
126 :     if ln>rn then single_R p else double_R p
127 :    
128 :     | T' (p as (_,E,T{left=E,...})) = single_L p
129 :     | T' (p as (_,T{right=E,...},E)) = single_R p
130 :    
131 :     | T' (p as (v,l as T{elt=lv,cnt=ln,left=ll,right=lr},
132 :     r as T{elt=rv,cnt=rn,left=rl,right=rr})) =
133 :     if rn >= wt ln (*right is too big*)
134 :     then
135 :     let val rln = numItems rl
136 :     val rrn = numItems rr
137 :     in
138 :     if rln < rrn then single_L p else double_L p
139 :     end
140 :     else if ln >= wt rn (*left is too big*)
141 :     then
142 :     let val lln = numItems ll
143 :     val lrn = numItems lr
144 :     in
145 :     if lrn < lln then single_R p else double_R p
146 :     end
147 :     else mkT(v,ln+rn+1,l,r)
148 :    
149 :     fun add (E,x) = mkT(x,1,E,E)
150 :     | add (set as T{elt=v,left=l,right=r,cnt},x) = (
151 :     case Key.compare(x,v)
152 :     of LESS => T'(v,add(l,x),r)
153 :     | GREATER => T'(v,l,add(r,x))
154 :     | EQUAL => mkT(x,cnt,l,r)
155 :     (* end case *))
156 :    
157 :     fun concat3 (E,v,r) = add(r,v)
158 :     | concat3 (l,v,E) = add(l,v)
159 :     | concat3 (l as T{elt=v1,cnt=n1,left=l1,right=r1}, v,
160 :     r as T{elt=v2,cnt=n2,left=l2,right=r2}) =
161 :     if wt n1 < n2 then T'(v2,concat3(l,v,l2),r2)
162 :     else if wt n2 < n1 then T'(v1,l1,concat3(r1,v,r))
163 :     else N(v,l,r)
164 :    
165 :     fun split_lt (E,x) = E
166 :     | split_lt (T{elt=v,left=l,right=r,...},x) =
167 :     case Key.compare(v,x) of
168 :     GREATER => split_lt(l,x)
169 :     | LESS => concat3(l,v,split_lt(r,x))
170 :     | _ => l
171 :    
172 :     fun split_gt (E,x) = E
173 :     | split_gt (T{elt=v,left=l,right=r,...},x) =
174 :     case Key.compare(v,x) of
175 :     LESS => split_gt(r,x)
176 :     | GREATER => concat3(split_gt(l,x),v,r)
177 :     | _ => r
178 :    
179 :     fun min (T{elt=v,left=E,...}) = v
180 :     | min (T{left=l,...}) = min l
181 :     | min _ = raise Match
182 :    
183 :     fun delmin (T{left=E,right=r,...}) = r
184 :     | delmin (T{elt=v,left=l,right=r,...}) = T'(v,delmin l,r)
185 :     | delmin _ = raise Match
186 :    
187 :     fun delete' (E,r) = r
188 :     | delete' (l,E) = l
189 :     | delete' (l,r) = T'(min r,l,delmin r)
190 :    
191 :     fun concat (E, s) = s
192 :     | concat (s, E) = s
193 :     | concat (t1 as T{elt=v1,cnt=n1,left=l1,right=r1},
194 :     t2 as T{elt=v2,cnt=n2,left=l2,right=r2}) =
195 :     if wt n1 < n2 then T'(v2,concat(t1,l2),r2)
196 :     else if wt n2 < n1 then T'(v1,l1,concat(r1,t2))
197 :     else T'(min t2,t1, delmin t2)
198 :    
199 :    
200 :     local
201 :     fun trim (lo,hi,E) = E
202 :     | trim (lo,hi,s as T{elt=v,left=l,right=r,...}) =
203 :     if (v > lo)
204 :     then if (v < hi) then s else trim(lo,hi,l)
205 :     else trim(lo,hi,r)
206 :    
207 :     fun uni_bd (s,E,_,_) = s
208 :     | uni_bd (E,T{elt=v,left=l,right=r,...},lo,hi) =
209 :     concat3(split_gt(l,lo),v,split_lt(r,hi))
210 :     | uni_bd (T{elt=v,left=l1,right=r1,...},
211 :     s2 as T{elt=v2,left=l2,right=r2,...},lo,hi) =
212 :     concat3(uni_bd(l1,trim(lo,v,s2),lo,v),
213 :     v,
214 :     uni_bd(r1,trim(v,hi,s2),v,hi))
215 :     (* inv: lo < v < hi *)
216 :    
217 :     (* all the other versions of uni and trim are
218 :     * specializations of the above two functions with
219 :     * lo=-infinity and/or hi=+infinity
220 :     *)
221 :    
222 :     fun trim_lo (_, E) = E
223 :     | trim_lo (lo,s as T{elt=v,right=r,...}) =
224 :     case Key.compare(v,lo) of
225 :     GREATER => s
226 :     | _ => trim_lo(lo,r)
227 :    
228 :     fun trim_hi (_, E) = E
229 :     | trim_hi (hi,s as T{elt=v,left=l,...}) =
230 :     case Key.compare(v,hi) of
231 :     LESS => s
232 :     | _ => trim_hi(hi,l)
233 :    
234 :     fun uni_hi (s,E,_) = s
235 :     | uni_hi (E,T{elt=v,left=l,right=r,...},hi) =
236 :     concat3(l,v,split_lt(r,hi))
237 :     | uni_hi (T{elt=v,left=l1,right=r1,...},
238 :     s2 as T{elt=v2,left=l2,right=r2,...},hi) =
239 :     concat3(uni_hi(l1,trim_hi(v,s2),v),v,uni_bd(r1,trim(v,hi,s2),v,hi))
240 :    
241 :     fun uni_lo (s,E,_) = s
242 :     | uni_lo (E,T{elt=v,left=l,right=r,...},lo) =
243 :     concat3(split_gt(l,lo),v,r)
244 :     | uni_lo (T{elt=v,left=l1,right=r1,...},
245 :     s2 as T{elt=v2,left=l2,right=r2,...},lo) =
246 :     concat3(uni_bd(l1,trim(lo,v,s2),lo,v),v,uni_lo(r1,trim_lo(v,s2),v))
247 :    
248 :     fun uni (s,E) = s
249 :     | uni (E,s) = s
250 :     | uni (T{elt=v,left=l1,right=r1,...},
251 :     s2 as T{elt=v2,left=l2,right=r2,...}) =
252 :     concat3(uni_hi(l1,trim_hi(v,s2),v), v, uni_lo(r1,trim_lo(v,s2),v))
253 :    
254 :     in
255 :     val hedge_union = uni
256 :     end
257 :    
258 :     (* The old_union version is about 20% slower than
259 :     * hedge_union in most cases
260 :     *)
261 :     fun old_union (E,s2) = s2
262 :     | old_union (s1,E) = s1
263 :     | old_union (T{elt=v,left=l,right=r,...},s2) =
264 :     let val l2 = split_lt(s2,v)
265 :     val r2 = split_gt(s2,v)
266 :     in
267 :     concat3(old_union(l,l2),v,old_union(r,r2))
268 :     end
269 :    
270 :     val empty = E
271 :     fun singleton x = T{elt=x,cnt=1,left=E,right=E}
272 :    
273 :     fun addList (s,l) = List.foldl (fn (i,s) => add(s,i)) s l
274 :    
275 :     val add = add
276 :    
277 :     fun member (set, x) = let
278 :     fun pk E = false
279 :     | pk (T{elt=v, left=l, right=r, ...}) = (
280 :     case Key.compare(x,v)
281 :     of LESS => pk l
282 :     | EQUAL => true
283 :     | GREATER => pk r
284 :     (* end case *))
285 :     in
286 :     pk set
287 :     end
288 :    
289 :     local
290 :     (* true if every item in t is in t' *)
291 :     fun treeIn (t,t') = let
292 :     fun isIn E = true
293 :     | isIn (T{elt,left=E,right=E,...}) = member(t',elt)
294 :     | isIn (T{elt,left,right=E,...}) =
295 :     member(t',elt) andalso isIn left
296 :     | isIn (T{elt,left=E,right,...}) =
297 :     member(t',elt) andalso isIn right
298 :     | isIn (T{elt,left,right,...}) =
299 :     member(t',elt) andalso isIn left andalso isIn right
300 :     in
301 :     isIn t
302 :     end
303 :     in
304 :     fun isSubset (E,_) = true
305 :     | isSubset (_,E) = false
306 :     | isSubset (t as T{cnt=n,...},t' as T{cnt=n',...}) =
307 :     (n<=n') andalso treeIn (t,t')
308 :    
309 :     fun equal (E,E) = true
310 :     | equal (t as T{cnt=n,...},t' as T{cnt=n',...}) =
311 :     (n=n') andalso treeIn (t,t')
312 :     | equal _ = false
313 :     end
314 :    
315 :     local
316 :     fun next ((t as T{right, ...})::rest) = (t, left(right, rest))
317 :     | next _ = (E, [])
318 :     and left (E, rest) = rest
319 :     | left (t as T{left=l, ...}, rest) = left(l, t::rest)
320 :     in
321 :     fun compare (s1, s2) = let
322 :     fun cmp (t1, t2) = (case (next t1, next t2)
323 :     of ((E, _), (E, _)) => EQUAL
324 :     | ((E, _), _) => LESS
325 :     | (_, (E, _)) => GREATER
326 :     | ((T{elt=e1, ...}, r1), (T{elt=e2, ...}, r2)) => (
327 :     case Key.compare(e1, e2)
328 :     of EQUAL => cmp (r1, r2)
329 :     | order => order
330 :     (* end case *))
331 :     (* end case *))
332 :     in
333 :     cmp (left(s1, []), left(s2, []))
334 :     end
335 :     end
336 :    
337 :     fun delete (E,x) = raise LibBase.NotFound
338 :     | delete (set as T{elt=v,left=l,right=r,...},x) =
339 :     case Key.compare(x,v) of
340 :     LESS => T'(v,delete(l,x),r)
341 :     | GREATER => T'(v,l,delete(r,x))
342 :     | _ => delete'(l,r)
343 :    
344 :     val union = hedge_union
345 :    
346 :     fun intersection (E, _) = E
347 :     | intersection (_, E) = E
348 :     | intersection (s, T{elt=v,left=l,right=r,...}) = let
349 :     val l2 = split_lt(s,v)
350 :     val r2 = split_gt(s,v)
351 :     in
352 :     if member(s,v)
353 :     then concat3(intersection(l2,l),v,intersection(r2,r))
354 :     else concat(intersection(l2,l),intersection(r2,r))
355 :     end
356 :    
357 :     fun difference (E,s) = E
358 :     | difference (s,E) = s
359 :     | difference (s, T{elt=v,left=l,right=r,...}) =
360 :     let val l2 = split_lt(s,v)
361 :     val r2 = split_gt(s,v)
362 :     in
363 :     concat(difference(l2,l),difference(r2,r))
364 :     end
365 :    
366 :     fun map f set = let
367 :     fun map'(acc, E) = acc
368 :     | map'(acc, T{elt,left,right,...}) =
369 :     map' (add (map' (acc, left), f elt), right)
370 :     in
371 :     map' (E, set)
372 :     end
373 :    
374 :     fun app apf =
375 :     let fun apply E = ()
376 :     | apply (T{elt,left,right,...}) =
377 :     (apply left;apf elt; apply right)
378 :     in
379 :     apply
380 :     end
381 :    
382 :     fun foldl f b set = let
383 :     fun foldf (E, b) = b
384 :     | foldf (T{elt,left,right,...}, b) =
385 :     foldf (right, f(elt, foldf (left, b)))
386 :     in
387 :     foldf (set, b)
388 :     end
389 :    
390 :     fun foldr f b set = let
391 :     fun foldf (E, b) = b
392 :     | foldf (T{elt,left,right,...}, b) =
393 :     foldf (left, f(elt, foldf (right, b)))
394 :     in
395 :     foldf (set, b)
396 :     end
397 :    
398 :     fun listItems set = foldr (op::) [] set
399 :    
400 :     fun filter pred set =
401 :     foldl (fn (item, s) => if (pred item) then add(s, item) else s)
402 :     empty set
403 :    
404 :     fun find p E = NONE
405 :     | find p (T{elt,left,right,...}) = (case find p left
406 :     of NONE => if (p elt)
407 :     then SOME elt
408 :     else find p right
409 :     | a => a
410 :     (* end case *))
411 :    
412 :     fun exists p E = false
413 :     | exists p (T{elt, left, right,...}) =
414 :     (exists p left) orelse (p elt) orelse (exists p right)
415 :    
416 :     end (* IntBinarySet *)

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