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/trunk/Util/int-binary-map.sml
ViewVC logotype

Annotation of /smlnj-lib/trunk/Util/int-binary-map.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 651 - (view) (download)
Original Path: sml/trunk/src/smlnj-lib/Util/int-binary-map.sml

1 : monnier 2 (* int-binary-map.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 :     *
17 :     * Name(s): Stephen Adams.
18 :     * Department, Institution: Electronics & Computer Science,
19 :     * University of Southampton
20 :     * Address: Electronics & Computer Science
21 :     * University of Southampton
22 :     * Southampton SO9 5NH
23 :     * Great Britian
24 :     * E-mail: sra@ecs.soton.ac.uk
25 :     *
26 :     * Comments:
27 :     *
28 :     * 1. The implementation is based on Binary search trees of Bounded
29 :     * Balance, similar to Nievergelt & Reingold, SIAM J. Computing
30 :     * 2(1), March 1973. The main advantage of these trees is that
31 :     * they keep the size of the tree in the node, giving a constant
32 :     * time size operation.
33 :     *
34 :     * 2. The bounded balance criterion is simpler than N&R's alpha.
35 :     * Simply, one subtree must not have more than `weight' times as
36 :     * many elements as the opposite subtree. Rebalancing is
37 :     * guaranteed to reinstate the criterion for weight>2.23, but
38 :     * the occasional incorrect behaviour for weight=2 is not
39 :     * detrimental to performance.
40 :     *
41 :     * Altered to work as a geneal intmap - Emden Gansner
42 :     *)
43 :    
44 :     structure IntBinaryMap :> ORD_MAP where type Key.ord_key = Int.int =
45 :     struct
46 :    
47 :     structure Key =
48 :     struct
49 :     type ord_key = Int.int
50 :     val compare = Int.compare
51 :     end
52 :    
53 :     (*
54 :     ** val weight = 3
55 :     ** fun wt i = weight * i
56 :     *)
57 :     fun wt (i : int) = i + i + i
58 :    
59 :     datatype 'a map
60 :     = E
61 :     | T of {
62 :     key : int,
63 :     value : 'a,
64 :     cnt : int,
65 :     left : 'a map,
66 :     right : 'a map
67 :     }
68 :    
69 : monnier 289 fun isEmpty E = true
70 :     | isEmpty _ = false
71 :    
72 : monnier 2 fun numItems E = 0
73 :     | numItems (T{cnt,...}) = cnt
74 :    
75 : monnier 289 (* return the first item in the map (or NONE if it is empty) *)
76 :     fun first E = NONE
77 :     | first (T{value, left=E, ...}) = SOME value
78 :     | first (T{left, ...}) = first left
79 :    
80 :     (* return the first item in the map and its key (or NONE if it is empty) *)
81 :     fun firsti E = NONE
82 :     | firsti (T{key, value, left=E, ...}) = SOME(key, value)
83 :     | firsti (T{left, ...}) = firsti left
84 :    
85 : monnier 2 local
86 :     fun N(k,v,E,E) = T{key=k,value=v,cnt=1,left=E,right=E}
87 :     | N(k,v,E,r as T n) = T{key=k,value=v,cnt=1+(#cnt n),left=E,right=r}
88 :     | N(k,v,l as T n,E) = T{key=k,value=v,cnt=1+(#cnt n),left=l,right=E}
89 :     | N(k,v,l as T n,r as T n') =
90 :     T{key=k,value=v,cnt=1+(#cnt n)+(#cnt n'),left=l,right=r}
91 :    
92 :     fun single_L (a,av,x,T{key=b,value=bv,left=y,right=z,...}) =
93 :     N(b,bv,N(a,av,x,y),z)
94 :     | single_L _ = raise Match
95 :     fun single_R (b,bv,T{key=a,value=av,left=x,right=y,...},z) =
96 :     N(a,av,x,N(b,bv,y,z))
97 :     | single_R _ = raise Match
98 :     fun double_L (a,av,w,T{key=c,value=cv,left=T{key=b,value=bv,left=x,right=y,...},right=z,...}) =
99 :     N(b,bv,N(a,av,w,x),N(c,cv,y,z))
100 :     | double_L _ = raise Match
101 :     fun double_R (c,cv,T{key=a,value=av,left=w,right=T{key=b,value=bv,left=x,right=y,...},...},z) =
102 :     N(b,bv,N(a,av,w,x),N(c,cv,y,z))
103 :     | double_R _ = raise Match
104 :    
105 :     fun T' (k,v,E,E) = T{key=k,value=v,cnt=1,left=E,right=E}
106 :     | T' (k,v,E,r as T{right=E,left=E,...}) =
107 :     T{key=k,value=v,cnt=2,left=E,right=r}
108 :     | T' (k,v,l as T{right=E,left=E,...},E) =
109 :     T{key=k,value=v,cnt=2,left=l,right=E}
110 :    
111 :     | T' (p as (_,_,E,T{left=T _,right=E,...})) = double_L p
112 :     | T' (p as (_,_,T{left=E,right=T _,...},E)) = double_R p
113 :    
114 :     (* these cases almost never happen with small weight*)
115 :     | T' (p as (_,_,E,T{left=T{cnt=ln,...},right=T{cnt=rn,...},...})) =
116 :     if ln < rn then single_L p else double_L p
117 :     | T' (p as (_,_,T{left=T{cnt=ln,...},right=T{cnt=rn,...},...},E)) =
118 :     if ln > rn then single_R p else double_R p
119 :    
120 :     | T' (p as (_,_,E,T{left=E,...})) = single_L p
121 :     | T' (p as (_,_,T{right=E,...},E)) = single_R p
122 :    
123 :     | T' (p as (k,v,l as T{cnt=ln,left=ll,right=lr,...},
124 :     r as T{cnt=rn,left=rl,right=rr,...})) =
125 :     if rn >= wt ln then (*right is too big*)
126 :     let val rln = numItems rl
127 :     val rrn = numItems rr
128 :     in
129 :     if rln < rrn then single_L p else double_L p
130 :     end
131 :    
132 :     else if ln >= wt rn then (*left is too big*)
133 :     let val lln = numItems ll
134 :     val lrn = numItems lr
135 :     in
136 :     if lrn < lln then single_R p else double_R p
137 :     end
138 :    
139 :     else T{key=k,value=v,cnt=ln+rn+1,left=l,right=r}
140 :    
141 :     local
142 :     fun min (T{left=E,key,value,...}) = (key,value)
143 :     | min (T{left,...}) = min left
144 :     | min _ = raise Match
145 :    
146 :     fun delmin (T{left=E,right,...}) = right
147 :     | delmin (T{key,value,left,right,...}) = T'(key,value,delmin left,right)
148 :     | delmin _ = raise Match
149 :     in
150 :     fun delete' (E,r) = r
151 :     | delete' (l,E) = l
152 :     | delete' (l,r) = let val (mink,minv) = min r in
153 :     T'(mink,minv,l,delmin r)
154 :     end
155 :     end
156 :     in
157 :     val empty = E
158 :    
159 : monnier 411 fun singleton (x,v) = T{key=x,value=v,cnt=1,left=E,right=E}
160 :    
161 : monnier 2 fun insert (E,x,v) = T{key=x,value=v,cnt=1,left=E,right=E}
162 :     | insert (T(set as {key,left,right,value,...}),x,v) =
163 :     if key > x then T'(key,value,insert(left,x,v),right)
164 :     else if key < x then T'(key,value,left,insert(right,x,v))
165 :     else T{key=x,value=v,left=left,right=right,cnt= #cnt set}
166 : monnier 29 fun insert' ((k, x), m) = insert(m, k, x)
167 : monnier 2
168 : monnier 411 fun inDomain (set, x) = let
169 :     fun mem E = false
170 :     | mem (T(n as {key,left,right,...})) =
171 :     if x > key then mem right
172 :     else if x < key then mem left
173 :     else true
174 :     in
175 :     mem set
176 :     end
177 :    
178 : monnier 2 fun find (set, x) = let
179 :     fun mem E = NONE
180 :     | mem (T(n as {key,left,right,...})) =
181 :     if x > key then mem right
182 :     else if x < key then mem left
183 :     else SOME(#value n)
184 :     in
185 :     mem set
186 :     end
187 :    
188 :     fun remove (E,x) = raise LibBase.NotFound
189 :     | remove (set as T{key,left,right,value,...},x) =
190 :     if key > x then
191 :     let val (left',v) = remove(left,x)
192 :     in (T'(key,value,left',right),v) end
193 :     else if key < x then
194 :     let val (right',v) = remove(right,x)
195 :     in (T'(key,value,left,right'),v) end
196 :     else (delete'(left,right),value)
197 :    
198 :     fun listItems d = let
199 :     fun d2l (E, l) = l
200 :     | d2l (T{key,value,left,right,...}, l) =
201 :     d2l(left, value::(d2l(right,l)))
202 :     in
203 :     d2l (d,[])
204 :     end
205 :    
206 :     fun listItemsi d = let
207 :     fun d2l (E, l) = l
208 :     | d2l (T{key,value,left,right,...}, l) =
209 :     d2l(left, (key,value)::(d2l(right,l)))
210 :     in
211 :     d2l (d,[])
212 :     end
213 :    
214 : monnier 411 fun listKeys d = let
215 :     fun d2l (E, l) = l
216 :     | d2l (T{key,left,right,...}, l) = d2l(left, key::(d2l(right,l)))
217 :     in
218 :     d2l (d,[])
219 :     end
220 :    
221 : monnier 2 local
222 :     fun next ((t as T{right, ...})::rest) = (t, left(right, rest))
223 :     | next _ = (E, [])
224 :     and left (E, rest) = rest
225 :     | left (t as T{left=l, ...}, rest) = left(l, t::rest)
226 :     in
227 :     fun collate cmpRng (s1, s2) = let
228 :     fun cmp (t1, t2) = (case (next t1, next t2)
229 :     of ((E, _), (E, _)) => EQUAL
230 :     | ((E, _), _) => LESS
231 :     | (_, (E, _)) => GREATER
232 :     | ((T{key=x1, value=y1, ...}, r1), (T{key=x2, value=y2, ...}, r2)) => (
233 :     case Key.compare(x1, x2)
234 :     of EQUAL => (case cmpRng(y1, y2)
235 :     of EQUAL => cmp (r1, r2)
236 :     | order => order
237 :     (* end case *))
238 :     | order => order
239 :     (* end case *))
240 :     (* end case *))
241 :     in
242 :     cmp (left(s1, []), left(s2, []))
243 :     end
244 :     end (* local *)
245 :    
246 :     fun appi f d = let
247 :     fun appf E = ()
248 :     | appf (T{key,value,left,right,...}) = (
249 :     appf left; f(key,value); appf right)
250 :     in
251 :     appf d
252 :     end
253 :     fun app f d = appi (fn (_, v) => f v) d
254 :    
255 :     fun mapi f d = let
256 :     fun mapf E = E
257 :     | mapf (T{key,value,left,right,cnt}) = let
258 :     val left' = mapf left
259 :     val value' = f(key, value)
260 :     val right' = mapf right
261 :     in
262 :     T{cnt=cnt, key=key, value=value', left = left', right = right'}
263 :     end
264 :     in
265 :     mapf d
266 :     end
267 :     fun map f d = mapi (fn (_, x) => f x) d
268 :    
269 :     fun foldli f init d = let
270 :     fun fold (E,v) = v
271 :     | fold (T{key,value,left,right,...},v) =
272 :     fold (right, f(key, value, fold(left, v)))
273 :     in
274 :     fold (d, init)
275 :     end
276 :     fun foldl f init d = foldli (fn (_, v, accum) => f (v, accum)) init d
277 :    
278 :     fun foldri f init d = let
279 :     fun fold (E,v) = v
280 :     | fold (T{key,value,left,right,...},v) =
281 :     fold (left, f(key, value, fold(right, v)))
282 :     in
283 :     fold (d, init)
284 :     end
285 :     fun foldr f init d = foldri (fn (_, v, accum) => f (v, accum)) init d
286 :    
287 :     end (* local *)
288 :    
289 :     (* the following are generic implementations of the unionWith and intersectWith
290 :     * operetions. These should be specialized for the internal representations
291 :     * at some point.
292 :     *)
293 :     fun unionWith f (m1, m2) = let
294 :     fun ins f (key, x, m) = (case find(m, key)
295 :     of NONE => insert(m, key, x)
296 :     | (SOME x') => insert(m, key, f(x, x'))
297 :     (* end case *))
298 :     in
299 :     if (numItems m1 > numItems m2)
300 :     then foldli (ins (fn (a, b) => f (b, a))) m1 m2
301 :     else foldli (ins f) m2 m1
302 :     end
303 :     fun unionWithi f (m1, m2) = let
304 :     fun ins f (key, x, m) = (case find(m, key)
305 :     of NONE => insert(m, key, x)
306 :     | (SOME x') => insert(m, key, f(key, x, x'))
307 :     (* end case *))
308 :     in
309 :     if (numItems m1 > numItems m2)
310 :     then foldli (ins (fn (k, a, b) => f (k, b, a))) m1 m2
311 :     else foldli (ins f) m2 m1
312 :     end
313 :    
314 :     fun intersectWith f (m1, m2) = let
315 :     (* iterate over the elements of m1, checking for membership in m2 *)
316 :     fun intersect f (m1, m2) = let
317 :     fun ins (key, x, m) = (case find(m2, key)
318 :     of NONE => m
319 :     | (SOME x') => insert(m, key, f(x, x'))
320 :     (* end case *))
321 :     in
322 :     foldli ins empty m1
323 :     end
324 :     in
325 :     if (numItems m1 > numItems m2)
326 :     then intersect f (m1, m2)
327 :     else intersect (fn (a, b) => f(b, a)) (m2, m1)
328 :     end
329 :     fun intersectWithi f (m1, m2) = let
330 :     (* iterate over the elements of m1, checking for membership in m2 *)
331 :     fun intersect f (m1, m2) = let
332 :     fun ins (key, x, m) = (case find(m2, key)
333 :     of NONE => m
334 :     | (SOME x') => insert(m, key, f(key, x, x'))
335 :     (* end case *))
336 :     in
337 :     foldli ins empty m1
338 :     end
339 :     in
340 :     if (numItems m1 > numItems m2)
341 :     then intersect f (m1, m2)
342 :     else intersect (fn (k, a, b) => f(k, b, a)) (m2, m1)
343 :     end
344 :    
345 :     (* this is a generic implementation of filter. It should
346 :     * be specialized to the data-structure at some point.
347 :     *)
348 :     fun filter predFn m = let
349 :     fun f (key, item, m) = if predFn item
350 :     then insert(m, key, item)
351 :     else m
352 :     in
353 :     foldli f empty m
354 :     end
355 :     fun filteri predFn m = let
356 :     fun f (key, item, m) = if predFn(key, item)
357 :     then insert(m, key, item)
358 :     else m
359 :     in
360 :     foldli f empty m
361 :     end
362 :    
363 :     (* this is a generic implementation of mapPartial. It should
364 :     * be specialized to the data-structure at some point.
365 :     *)
366 :     fun mapPartial f m = let
367 :     fun g (key, item, m) = (case f item
368 :     of NONE => m
369 :     | (SOME item') => insert(m, key, item')
370 :     (* end case *))
371 :     in
372 :     foldli g empty m
373 :     end
374 :     fun mapPartiali f m = let
375 :     fun g (key, item, m) = (case f(key, item)
376 :     of NONE => m
377 :     | (SOME item') => insert(m, key, item')
378 :     (* end case *))
379 :     in
380 :     foldli g empty m
381 :     end
382 :    
383 :     end

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