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 29 - (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 : monnier 29 fun insert' ((k, x), m) = insert(m, k, x)
150 : monnier 2
151 :     fun find (set, x) = let
152 :     fun mem E = NONE
153 :     | mem (T(n as {key,left,right,...})) = (case K.compare (x,key)
154 :     of GREATER => mem right
155 :     | EQUAL => SOME(#value n)
156 :     | LESS => mem left
157 :     (* end case *))
158 :     in
159 :     mem set
160 :     end
161 :    
162 :     fun remove (E,x) = raise LibBase.NotFound
163 :     | remove (set as T{key,left,right,value,...},x) = (
164 :     case K.compare (key,x)
165 :     of GREATER => let
166 :     val (left', v) = remove(left, x)
167 :     in
168 :     (T'(key, value, left', right), v)
169 :     end
170 :     | LESS => let
171 :     val (right', v) = remove (right, x)
172 :     in
173 :     (T'(key, value, left, right'), v)
174 :     end
175 :     | _ => (delete'(left,right),value)
176 :     (* end case *))
177 :    
178 :     fun listItems d = let
179 :     fun d2l (E, l) = l
180 :     | d2l (T{key,value,left,right,...}, l) =
181 :     d2l(left, value::(d2l(right,l)))
182 :     in
183 :     d2l (d,[])
184 :     end
185 :    
186 :     fun listItemsi d = let
187 :     fun d2l (E, l) = l
188 :     | d2l (T{key,value,left,right,...}, l) =
189 :     d2l(left, (key,value)::(d2l(right,l)))
190 :     in
191 :     d2l (d,[])
192 :     end
193 :    
194 :     local
195 :     fun next ((t as T{right, ...})::rest) = (t, left(right, rest))
196 :     | next _ = (E, [])
197 :     and left (E, rest) = rest
198 :     | left (t as T{left=l, ...}, rest) = left(l, t::rest)
199 :     in
200 :     fun collate cmpRng (s1, s2) = let
201 :     fun cmp (t1, t2) = (case (next t1, next t2)
202 :     of ((E, _), (E, _)) => EQUAL
203 :     | ((E, _), _) => LESS
204 :     | (_, (E, _)) => GREATER
205 :     | ((T{key=x1, value=y1, ...}, r1), (T{key=x2, value=y2, ...}, r2)) => (
206 :     case Key.compare(x1, x2)
207 :     of EQUAL => (case cmpRng(y1, y2)
208 :     of EQUAL => cmp (r1, r2)
209 :     | order => order
210 :     (* end case *))
211 :     | order => order
212 :     (* end case *))
213 :     (* end case *))
214 :     in
215 :     cmp (left(s1, []), left(s2, []))
216 :     end
217 :     end (* local *)
218 :    
219 :     fun appi f d = let
220 :     fun app' E = ()
221 :     | app' (T{key,value,left,right,...}) = (
222 :     app' left; f(key, value); app' right)
223 :     in
224 :     app' d
225 :     end
226 :     fun app f d = let
227 :     fun app' E = ()
228 :     | app' (T{value,left,right,...}) = (
229 :     app' left; f value; app' right)
230 :     in
231 :     app' d
232 :     end
233 :    
234 :     fun mapi f d = let
235 :     fun map' E = E
236 :     | map' (T{key,value,left,right,cnt}) = let
237 :     val left' = map' left
238 :     val value' = f(key, value)
239 :     val right' = map' right
240 :     in
241 :     T{cnt=cnt, key=key, value=value', left = left', right = right'}
242 :     end
243 :     in
244 :     map' d
245 :     end
246 :     fun map f d = mapi (fn (_, x) => f x) d
247 :    
248 :     fun foldli f init d = let
249 :     fun fold (E, v) = v
250 :     | fold (T{key,value,left,right,...}, v) =
251 :     fold (right, f(key, value, fold(left, v)))
252 :     in
253 :     fold (d, init)
254 :     end
255 :     fun foldl f init d = foldli (fn (_, v, accum) => f (v, accum)) init d
256 :    
257 :     fun foldri f init d = let
258 :     fun fold (E,v) = v
259 :     | fold (T{key,value,left,right,...},v) =
260 :     fold (left, f(key, value, fold(right, v)))
261 :     in
262 :     fold (d, init)
263 :     end
264 :     fun foldr f init d = foldri (fn (_, v, accum) => f (v, accum)) init d
265 :    
266 :     (** To be implemented **
267 :     val filter : ('a -> bool) -> 'a map -> 'a map
268 :     val filteri : (Key.ord_key * 'a -> bool) -> 'a map -> 'a map
269 :     **)
270 :    
271 :     end (* local *)
272 :    
273 :     (* the following are generic implementations of the unionWith and intersectWith
274 :     * operetions. These should be specialized for the internal representations
275 :     * at some point.
276 :     *)
277 :     fun unionWith f (m1, m2) = let
278 :     fun ins f (key, x, m) = (case find(m, key)
279 :     of NONE => insert(m, key, x)
280 :     | (SOME x') => insert(m, key, f(x, x'))
281 :     (* end case *))
282 :     in
283 :     if (numItems m1 > numItems m2)
284 :     then foldli (ins (fn (a, b) => f (b, a))) m1 m2
285 :     else foldli (ins f) m2 m1
286 :     end
287 :     fun unionWithi f (m1, m2) = let
288 :     fun ins f (key, x, m) = (case find(m, key)
289 :     of NONE => insert(m, key, x)
290 :     | (SOME x') => insert(m, key, f(key, x, x'))
291 :     (* end case *))
292 :     in
293 :     if (numItems m1 > numItems m2)
294 :     then foldli (ins (fn (k, a, b) => f (k, b, a))) m1 m2
295 :     else foldli (ins f) m2 m1
296 :     end
297 :    
298 :     fun intersectWith f (m1, m2) = let
299 :     (* iterate over the elements of m1, checking for membership in m2 *)
300 :     fun intersect f (m1, m2) = let
301 :     fun ins (key, x, m) = (case find(m2, key)
302 :     of NONE => m
303 :     | (SOME x') => insert(m, key, f(x, x'))
304 :     (* end case *))
305 :     in
306 :     foldli ins empty m1
307 :     end
308 :     in
309 :     if (numItems m1 > numItems m2)
310 :     then intersect f (m1, m2)
311 :     else intersect (fn (a, b) => f(b, a)) (m2, m1)
312 :     end
313 :     fun intersectWithi f (m1, m2) = let
314 :     (* iterate over the elements of m1, checking for membership in m2 *)
315 :     fun intersect f (m1, m2) = let
316 :     fun ins (key, x, m) = (case find(m2, key)
317 :     of NONE => m
318 :     | (SOME x') => insert(m, key, f(key, x, x'))
319 :     (* end case *))
320 :     in
321 :     foldli ins empty m1
322 :     end
323 :     in
324 :     if (numItems m1 > numItems m2)
325 :     then intersect f (m1, m2)
326 :     else intersect (fn (k, a, b) => f(k, b, a)) (m2, m1)
327 :     end
328 :    
329 :     (* this is a generic implementation of filter. It should
330 :     * be specialized to the data-structure at some point.
331 :     *)
332 :     fun filter predFn m = let
333 :     fun f (key, item, m) = if predFn item
334 :     then insert(m, key, item)
335 :     else m
336 :     in
337 :     foldli f empty m
338 :     end
339 :     fun filteri predFn m = let
340 :     fun f (key, item, m) = if predFn(key, item)
341 :     then insert(m, key, item)
342 :     else m
343 :     in
344 :     foldli f empty m
345 :     end
346 :    
347 :     (* this is a generic implementation of mapPartial. It should
348 :     * be specialized to the data-structure at some point.
349 :     *)
350 :     fun mapPartial f m = let
351 :     fun g (key, item, m) = (case f item
352 :     of NONE => m
353 :     | (SOME item') => insert(m, key, item')
354 :     (* end case *))
355 :     in
356 :     foldli g empty m
357 :     end
358 :     fun mapPartiali f m = let
359 :     fun g (key, item, m) = (case f(key, item)
360 :     of NONE => m
361 :     | (SOME item') => insert(m, key, item')
362 :     (* end case *))
363 :     in
364 :     foldli g empty m
365 :     end
366 :    
367 :     end (* functor BinaryMapFn *)

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