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/binary-map-fn.sml
ViewVC logotype

Annotation of /sml/branches/SMLNJ/src/smlnj-lib/Util/binary-map-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (view) (download)

1 : monnier 2 (* binary-map-fn.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 :     *)
42 :    
43 :     functor BinaryMapFn (K : ORD_KEY) : ORD_MAP =
44 :     struct
45 :    
46 :     structure Key = K
47 :    
48 :     (*
49 :     ** val weight = 3
50 :     ** fun wt i = weight * i
51 :     *)
52 :     fun wt (i : int) = i + i + i
53 :    
54 :     datatype 'a map
55 :     = E
56 :     | T of {
57 :     key : K.ord_key,
58 :     value : 'a,
59 :     cnt : int,
60 :     left : 'a map,
61 :     right : 'a map
62 :     }
63 :    
64 :     val empty = E
65 :    
66 :     fun numItems E = 0
67 :     | numItems (T{cnt,...}) = cnt
68 :    
69 :     local
70 :     fun N(k,v,E,E) = T{key=k,value=v,cnt=1,left=E,right=E}
71 :     | N(k,v,E,r as T n) = T{key=k,value=v,cnt=1+(#cnt n),left=E,right=r}
72 :     | N(k,v,l as T n,E) = T{key=k,value=v,cnt=1+(#cnt n),left=l,right=E}
73 :     | N(k,v,l as T n,r as T n') =
74 :     T{key=k,value=v,cnt=1+(#cnt n)+(#cnt n'),left=l,right=r}
75 :    
76 :     fun single_L (a,av,x,T{key=b,value=bv,left=y,right=z,...}) =
77 :     N(b,bv,N(a,av,x,y),z)
78 :     | single_L _ = raise Match
79 :     fun single_R (b,bv,T{key=a,value=av,left=x,right=y,...},z) =
80 :     N(a,av,x,N(b,bv,y,z))
81 :     | single_R _ = raise Match
82 :     fun double_L (a,av,w,T{key=c,value=cv,left=T{key=b,value=bv,left=x,right=y,...},right=z,...}) =
83 :     N(b,bv,N(a,av,w,x),N(c,cv,y,z))
84 :     | double_L _ = raise Match
85 :     fun double_R (c,cv,T{key=a,value=av,left=w,right=T{key=b,value=bv,left=x,right=y,...},...},z) =
86 :     N(b,bv,N(a,av,w,x),N(c,cv,y,z))
87 :     | double_R _ = raise Match
88 :    
89 :     fun T' (k,v,E,E) = T{key=k,value=v,cnt=1,left=E,right=E}
90 :     | T' (k,v,E,r as T{right=E,left=E,...}) =
91 :     T{key=k,value=v,cnt=2,left=E,right=r}
92 :     | T' (k,v,l as T{right=E,left=E,...},E) =
93 :     T{key=k,value=v,cnt=2,left=l,right=E}
94 :    
95 :     | T' (p as (_,_,E,T{left=T _,right=E,...})) = double_L p
96 :     | T' (p as (_,_,T{left=E,right=T _,...},E)) = double_R p
97 :    
98 :     (* these cases almost never happen with small weight*)
99 :     | T' (p as (_,_,E,T{left=T{cnt=ln,...},right=T{cnt=rn,...},...})) =
100 :     if ln < rn then single_L p else double_L p
101 :     | T' (p as (_,_,T{left=T{cnt=ln,...},right=T{cnt=rn,...},...},E)) =
102 :     if ln > rn then single_R p else double_R p
103 :    
104 :     | T' (p as (_,_,E,T{left=E,...})) = single_L p
105 :     | T' (p as (_,_,T{right=E,...},E)) = single_R p
106 :    
107 :     | T' (p as (k,v,l as T{cnt=ln,left=ll,right=lr,...},
108 :     r as T{cnt=rn,left=rl,right=rr,...})) =
109 :     if rn >= wt ln then (*right is too big*)
110 :     let val rln = numItems rl
111 :     val rrn = numItems rr
112 :     in
113 :     if rln < rrn then single_L p else double_L p
114 :     end
115 :    
116 :     else if ln >= wt rn then (*left is too big*)
117 :     let val lln = numItems ll
118 :     val lrn = numItems lr
119 :     in
120 :     if lrn < lln then single_R p else double_R p
121 :     end
122 :    
123 :     else T{key=k,value=v,cnt=ln+rn+1,left=l,right=r}
124 :    
125 :     local
126 :     fun min (T{left=E,key,value,...}) = (key,value)
127 :     | min (T{left,...}) = min left
128 :     | min _ = raise Match
129 :    
130 :     fun delmin (T{left=E,right,...}) = right
131 :     | delmin (T{key,value,left,right,...}) = T'(key,value,delmin left,right)
132 :     | delmin _ = raise Match
133 :     in
134 :     fun delete' (E,r) = r
135 :     | delete' (l,E) = l
136 :     | delete' (l,r) = let val (mink,minv) = min r in
137 :     T'(mink,minv,l,delmin r)
138 :     end
139 :     end
140 :     in
141 :     fun mkDict () = E
142 :    
143 :     fun insert (E,x,v) = T{key=x,value=v,cnt=1,left=E,right=E}
144 :     | insert (T(set as {key,left,right,value,...}),x,v) =
145 :     case K.compare (key,x) of
146 :     GREATER => T'(key,value,insert(left,x,v),right)
147 :     | LESS => T'(key,value,left,insert(right,x,v))
148 :     | _ => T{key=x,value=v,left=left,right=right,cnt= #cnt set}
149 :    
150 :     fun find (set, x) = let
151 :     fun mem E = NONE
152 :     | mem (T(n as {key,left,right,...})) = (case K.compare (x,key)
153 :     of GREATER => mem right
154 :     | EQUAL => SOME(#value n)
155 :     | LESS => mem left
156 :     (* end case *))
157 :     in
158 :     mem set
159 :     end
160 :    
161 :     fun remove (E,x) = raise LibBase.NotFound
162 :     | remove (set as T{key,left,right,value,...},x) = (
163 :     case K.compare (key,x)
164 :     of GREATER => let
165 :     val (left', v) = remove(left, x)
166 :     in
167 :     (T'(key, value, left', right), v)
168 :     end
169 :     | LESS => let
170 :     val (right', v) = remove (right, x)
171 :     in
172 :     (T'(key, value, left, right'), v)
173 :     end
174 :     | _ => (delete'(left,right),value)
175 :     (* end case *))
176 :    
177 :     fun listItems d = let
178 :     fun d2l (E, l) = l
179 :     | d2l (T{key,value,left,right,...}, l) =
180 :     d2l(left, value::(d2l(right,l)))
181 :     in
182 :     d2l (d,[])
183 :     end
184 :    
185 :     fun listItemsi d = let
186 :     fun d2l (E, l) = l
187 :     | d2l (T{key,value,left,right,...}, l) =
188 :     d2l(left, (key,value)::(d2l(right,l)))
189 :     in
190 :     d2l (d,[])
191 :     end
192 :    
193 :     local
194 :     fun next ((t as T{right, ...})::rest) = (t, left(right, rest))
195 :     | next _ = (E, [])
196 :     and left (E, rest) = rest
197 :     | left (t as T{left=l, ...}, rest) = left(l, t::rest)
198 :     in
199 :     fun collate cmpRng (s1, s2) = let
200 :     fun cmp (t1, t2) = (case (next t1, next t2)
201 :     of ((E, _), (E, _)) => EQUAL
202 :     | ((E, _), _) => LESS
203 :     | (_, (E, _)) => GREATER
204 :     | ((T{key=x1, value=y1, ...}, r1), (T{key=x2, value=y2, ...}, r2)) => (
205 :     case Key.compare(x1, x2)
206 :     of EQUAL => (case cmpRng(y1, y2)
207 :     of EQUAL => cmp (r1, r2)
208 :     | order => order
209 :     (* end case *))
210 :     | order => order
211 :     (* end case *))
212 :     (* end case *))
213 :     in
214 :     cmp (left(s1, []), left(s2, []))
215 :     end
216 :     end (* local *)
217 :    
218 :     fun appi f d = let
219 :     fun app' E = ()
220 :     | app' (T{key,value,left,right,...}) = (
221 :     app' left; f(key, value); app' right)
222 :     in
223 :     app' d
224 :     end
225 :     fun app f d = let
226 :     fun app' E = ()
227 :     | app' (T{value,left,right,...}) = (
228 :     app' left; f value; app' right)
229 :     in
230 :     app' d
231 :     end
232 :    
233 :     fun mapi f d = let
234 :     fun map' E = E
235 :     | map' (T{key,value,left,right,cnt}) = let
236 :     val left' = map' left
237 :     val value' = f(key, value)
238 :     val right' = map' right
239 :     in
240 :     T{cnt=cnt, key=key, value=value', left = left', right = right'}
241 :     end
242 :     in
243 :     map' d
244 :     end
245 :     fun map f d = mapi (fn (_, x) => f x) d
246 :    
247 :     fun foldli f init d = let
248 :     fun fold (E, v) = v
249 :     | fold (T{key,value,left,right,...}, v) =
250 :     fold (right, f(key, value, fold(left, v)))
251 :     in
252 :     fold (d, init)
253 :     end
254 :     fun foldl f init d = foldli (fn (_, v, accum) => f (v, accum)) init d
255 :    
256 :     fun foldri f init d = let
257 :     fun fold (E,v) = v
258 :     | fold (T{key,value,left,right,...},v) =
259 :     fold (left, f(key, value, fold(right, v)))
260 :     in
261 :     fold (d, init)
262 :     end
263 :     fun foldr f init d = foldri (fn (_, v, accum) => f (v, accum)) init d
264 :    
265 :     (** To be implemented **
266 :     val filter : ('a -> bool) -> 'a map -> 'a map
267 :     val filteri : (Key.ord_key * 'a -> bool) -> 'a map -> 'a map
268 :     **)
269 :    
270 :     end (* local *)
271 :    
272 :     (* the following are generic implementations of the unionWith and intersectWith
273 :     * operetions. These should be specialized for the internal representations
274 :     * at some point.
275 :     *)
276 :     fun unionWith f (m1, m2) = let
277 :     fun ins f (key, x, m) = (case find(m, key)
278 :     of NONE => insert(m, key, x)
279 :     | (SOME x') => insert(m, key, f(x, x'))
280 :     (* end case *))
281 :     in
282 :     if (numItems m1 > numItems m2)
283 :     then foldli (ins (fn (a, b) => f (b, a))) m1 m2
284 :     else foldli (ins f) m2 m1
285 :     end
286 :     fun unionWithi f (m1, m2) = let
287 :     fun ins f (key, x, m) = (case find(m, key)
288 :     of NONE => insert(m, key, x)
289 :     | (SOME x') => insert(m, key, f(key, x, x'))
290 :     (* end case *))
291 :     in
292 :     if (numItems m1 > numItems m2)
293 :     then foldli (ins (fn (k, a, b) => f (k, b, a))) m1 m2
294 :     else foldli (ins f) m2 m1
295 :     end
296 :    
297 :     fun intersectWith f (m1, m2) = let
298 :     (* iterate over the elements of m1, checking for membership in m2 *)
299 :     fun intersect f (m1, m2) = let
300 :     fun ins (key, x, m) = (case find(m2, key)
301 :     of NONE => m
302 :     | (SOME x') => insert(m, key, f(x, x'))
303 :     (* end case *))
304 :     in
305 :     foldli ins empty m1
306 :     end
307 :     in
308 :     if (numItems m1 > numItems m2)
309 :     then intersect f (m1, m2)
310 :     else intersect (fn (a, b) => f(b, a)) (m2, m1)
311 :     end
312 :     fun intersectWithi f (m1, m2) = let
313 :     (* iterate over the elements of m1, checking for membership in m2 *)
314 :     fun intersect f (m1, m2) = let
315 :     fun ins (key, x, m) = (case find(m2, key)
316 :     of NONE => m
317 :     | (SOME x') => insert(m, key, f(key, x, x'))
318 :     (* end case *))
319 :     in
320 :     foldli ins empty m1
321 :     end
322 :     in
323 :     if (numItems m1 > numItems m2)
324 :     then intersect f (m1, m2)
325 :     else intersect (fn (k, a, b) => f(k, b, a)) (m2, m1)
326 :     end
327 :    
328 :     (* this is a generic implementation of filter. It should
329 :     * be specialized to the data-structure at some point.
330 :     *)
331 :     fun filter predFn m = let
332 :     fun f (key, item, m) = if predFn item
333 :     then insert(m, key, item)
334 :     else m
335 :     in
336 :     foldli f empty m
337 :     end
338 :     fun filteri predFn m = let
339 :     fun f (key, item, m) = if predFn(key, item)
340 :     then insert(m, key, item)
341 :     else m
342 :     in
343 :     foldli f empty m
344 :     end
345 :    
346 :     (* this is a generic implementation of mapPartial. It should
347 :     * be specialized to the data-structure at some point.
348 :     *)
349 :     fun mapPartial f m = let
350 :     fun g (key, item, m) = (case f item
351 :     of NONE => m
352 :     | (SOME item') => insert(m, key, item')
353 :     (* end case *))
354 :     in
355 :     foldli g empty m
356 :     end
357 :     fun mapPartiali f m = let
358 :     fun g (key, item, m) = (case f(key, item)
359 :     of NONE => m
360 :     | (SOME item') => insert(m, key, item')
361 :     (* end case *))
362 :     in
363 :     foldli g empty m
364 :     end
365 :    
366 :     end (* functor BinaryMapFn *)

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