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/branches/rt-transition/Util/splay-map-fn.sml
ViewVC logotype

Annotation of /smlnj-lib/branches/rt-transition/Util/splay-map-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 411 - (view) (download)
Original Path: sml/branches/SMLNJ/src/smlnj-lib/Util/splay-map-fn.sml

1 : monnier 2 (* splay-map-fn.sml
2 :     *
3 :     * COPYRIGHT (c) 1993 by AT&T Bell Laboratories. See COPYRIGHT file for details.
4 :     *
5 :     * Functor implementing dictionaries using splay trees.
6 :     *
7 :     *)
8 :    
9 :     functor SplayMapFn (K : ORD_KEY) : ORD_MAP =
10 :     struct
11 :     structure Key = K
12 :     open SplayTree
13 :    
14 : monnier 289 datatype 'a map
15 :     = EMPTY
16 : monnier 2 | MAP of {
17 :     root : (K.ord_key * 'a) splay ref,
18 :     nobj : int
19 :     }
20 :    
21 :     fun cmpf k (k', _) = K.compare(k',k)
22 :    
23 :     val empty = EMPTY
24 : monnier 289
25 :     fun isEmpty EMPTY = true
26 :     | isEmpty _ = false
27 :    
28 :     (* return the first item in the map (or NONE if it is empty) *)
29 :     fun first EMPTY = NONE
30 :     | first (MAP{root, ...}) = let
31 :     fun f (SplayObj{value=(_, value), left=SplayNil, ...}) = SOME value
32 :     | f (SplayObj{left, ...}) = f left
33 :     in
34 :     f (!root)
35 :     end
36 :    
37 :     (* return the first item in the map and its key (or NONE if it is empty) *)
38 :     fun firsti EMPTY = NONE
39 :     | firsti (MAP{root, ...}) = let
40 :     fun f (SplayObj{value=(key, value), left=SplayNil, ...}) = SOME(key, value)
41 :     | f (SplayObj{left, ...}) = f left
42 :     in
43 :     f (!root)
44 :     end
45 :    
46 : monnier 411 fun singleton (key, v) =
47 :     MAP{nobj=1,root=ref(SplayObj{value=(key,v),left=SplayNil,right=SplayNil})}
48 :    
49 :     (* Insert an item. *)
50 : monnier 2 fun insert (EMPTY,key,v) =
51 :     MAP{nobj=1,root=ref(SplayObj{value=(key,v),left=SplayNil,right=SplayNil})}
52 :     | insert (MAP{root,nobj},key,v) =
53 :     case splay (cmpf key, !root) of
54 :     (EQUAL,SplayObj{value,left,right}) =>
55 :     MAP{nobj=nobj,root=ref(SplayObj{value=(key,v),left=left,right=right})}
56 :     | (LESS,SplayObj{value,left,right}) =>
57 :     MAP{
58 :     nobj=nobj+1,
59 :     root=ref(SplayObj{value=(key,v),left=SplayObj{value=value,left=left,right=SplayNil},right=right})
60 :     }
61 :     | (GREATER,SplayObj{value,left,right}) =>
62 :     MAP{
63 :     nobj=nobj+1,
64 :     root=ref(SplayObj{
65 :     value=(key,v),
66 :     left=left,
67 :     right=SplayObj{value=value,left=SplayNil,right=right}
68 :     })
69 :     }
70 :     | (_,SplayNil) => raise LibBase.Impossible "SplayMapFn.insert SplayNil"
71 : monnier 29 fun insert' ((k, x), m) = insert(m, k, x)
72 : monnier 2
73 : monnier 411 fun inDomain (EMPTY, _) = false
74 :     | inDomain (MAP{root,nobj}, key) = (case splay (cmpf key, !root)
75 :     of (EQUAL, r as SplayObj{value,...}) => (root := r; true)
76 :     | (_, r) => (root := r; false)
77 :     (* end case *))
78 :    
79 : monnier 2 (* Look for an item, return NONE if the item doesn't exist *)
80 :     fun find (EMPTY,_) = NONE
81 :     | find (MAP{root,nobj},key) = (case splay (cmpf key, !root)
82 :     of (EQUAL, r as SplayObj{value,...}) => (root := r; SOME(#2 value))
83 :     | (_, r) => (root := r; NONE))
84 :    
85 :     (* Remove an item.
86 :     * Raise LibBase.NotFound if not found
87 :     *)
88 :     fun remove (EMPTY, _) = raise LibBase.NotFound
89 :     | remove (MAP{root,nobj}, key) = (case (splay (cmpf key, !root))
90 :     of (EQUAL, SplayObj{value, left, right}) =>
91 :     if nobj = 1
92 :     then (EMPTY, #2 value)
93 :     else (MAP{root=ref(join(left,right)),nobj=nobj-1}, #2 value)
94 :     | (_,r) => (root := r; raise LibBase.NotFound)
95 :     (* end case *))
96 :    
97 :     (* Return the number of items in the table *)
98 :     fun numItems EMPTY = 0
99 :     | numItems (MAP{nobj,...}) = nobj
100 :    
101 :     (* Return a list of the items (and their keys) in the dictionary *)
102 :     fun listItems EMPTY = []
103 :     | listItems (MAP{root,...}) = let
104 :     fun apply (SplayNil, l) = l
105 :     | apply (SplayObj{value=(_, v), left, right}, l) =
106 :     apply(left, v::(apply (right,l)))
107 :     in
108 :     apply (!root, [])
109 :     end
110 :     fun listItemsi EMPTY = []
111 :     | listItemsi (MAP{root,...}) = let
112 :     fun apply (SplayNil,l) = l
113 :     | apply (SplayObj{value,left,right},l) =
114 :     apply(left, value::(apply (right,l)))
115 :     in
116 :     apply (!root,[])
117 :     end
118 :    
119 : monnier 411 fun listKeys EMPTY = []
120 :     | listKeys (MAP{root,...}) = let
121 :     fun apply (SplayNil, l) = l
122 :     | apply (SplayObj{value=(key, _),left,right},l) =
123 :     apply(left, key::(apply (right,l)))
124 :     in
125 :     apply (!root, [])
126 :     end
127 :    
128 : monnier 2 local
129 :     fun next ((t as SplayObj{right, ...})::rest) = (t, left(right, rest))
130 :     | next _ = (SplayNil, [])
131 :     and left (SplayNil, rest) = rest
132 :     | left (t as SplayObj{left=l, ...}, rest) = left(l, t::rest)
133 :     in
134 :     fun collate cmpRng (EMPTY, EMPTY) = EQUAL
135 :     | collate cmpRng (EMPTY, _) = LESS
136 :     | collate cmpRng (_, EMPTY) = GREATER
137 :     | collate cmpRng (MAP{root=s1, ...}, MAP{root=s2, ...}) = let
138 :     fun cmp (t1, t2) = (case (next t1, next t2)
139 :     of ((SplayNil, _), (SplayNil, _)) => EQUAL
140 :     | ((SplayNil, _), _) => LESS
141 :     | (_, (SplayNil, _)) => GREATER
142 :     | ((SplayObj{value=(x1, y1), ...}, r1),
143 :     (SplayObj{value=(x2, y2), ...}, r2)
144 :     ) => (
145 :     case Key.compare(x1, x2)
146 :     of EQUAL => (case cmpRng (y1, y2)
147 :     of EQUAL => cmp (r1, r2)
148 :     | order => order
149 :     (* end case *))
150 :     | order => order
151 :     (* end case *))
152 :     (* end case *))
153 :     in
154 :     cmp (left(!s1, []), left(!s2, []))
155 :     end
156 :     end (* local *)
157 :    
158 :     (* Apply a function to the entries of the dictionary *)
159 :     fun appi af EMPTY = ()
160 :     | appi af (MAP{root,...}) =
161 :     let fun apply SplayNil = ()
162 :     | apply (SplayObj{value,left,right}) =
163 :     (apply left; af value; apply right)
164 :     in
165 :     apply (!root)
166 :     end
167 :    
168 :     fun app af EMPTY = ()
169 :     | app af (MAP{root,...}) =
170 :     let fun apply SplayNil = ()
171 :     | apply (SplayObj{value=(_,value),left,right}) =
172 :     (apply left; af value; apply right)
173 :     in
174 :     apply (!root)
175 :     end
176 :     (*
177 :     fun revapp af (MAP{root,...}) =
178 :     let fun apply SplayNil = ()
179 :     | apply (SplayObj{value,left,right}) =
180 :     (apply right; af value; apply left)
181 :     in
182 :     apply (!root)
183 :     end
184 :     *)
185 :    
186 :     (* Fold function *)
187 :     fun foldri (abf : K.ord_key * 'a * 'b -> 'b) b EMPTY = b
188 :     | foldri (abf : K.ord_key * 'a * 'b -> 'b) b (MAP{root,...}) =
189 :     let fun apply (SplayNil : (K.ord_key * 'a) splay, b) = b
190 :     | apply (SplayObj{value,left,right},b) =
191 :     apply(left,abf(#1 value,#2 value,apply(right,b)))
192 :     in
193 :     apply (!root,b)
194 :     end
195 :    
196 :     fun foldr (abf : 'a * 'b -> 'b) b EMPTY = b
197 :     | foldr (abf : 'a * 'b -> 'b) b (MAP{root,...}) =
198 :     let fun apply (SplayNil : (K.ord_key * 'a) splay, b) = b
199 :     | apply (SplayObj{value=(_,value),left,right},b) =
200 :     apply(left,abf(value,apply(right,b)))
201 :     in
202 :     apply (!root,b)
203 :     end
204 :    
205 :     fun foldli (abf : K.ord_key * 'a * 'b -> 'b) b EMPTY = b
206 :     | foldli (abf : K.ord_key * 'a * 'b -> 'b) b (MAP{root,...}) =
207 :     let fun apply (SplayNil : (K.ord_key * 'a) splay, b) = b
208 :     | apply (SplayObj{value,left,right},b) =
209 :     apply(right,abf(#1 value,#2 value,apply(left,b)))
210 :     in
211 :     apply (!root,b)
212 :     end
213 :    
214 :     fun foldl (abf : 'a * 'b -> 'b) b EMPTY = b
215 :     | foldl (abf : 'a * 'b -> 'b) b (MAP{root,...}) =
216 :     let fun apply (SplayNil : (K.ord_key * 'a) splay, b) = b
217 :     | apply (SplayObj{value=(_,value),left,right},b) =
218 :     apply(right,abf(value,apply(left,b)))
219 :     in
220 :     apply (!root,b)
221 :     end
222 :    
223 :     (* Map a table to a new table that has the same keys*)
224 :     fun mapi (af : K.ord_key * 'a -> 'b) EMPTY = EMPTY
225 :     | mapi (af : K.ord_key * 'a -> 'b) (MAP{root,nobj}) =
226 :     let fun ap (SplayNil : (K.ord_key * 'a) splay) = SplayNil
227 :     | ap (SplayObj{value,left,right}) = let
228 :     val left' = ap left
229 :     val value' = (#1 value, af value)
230 :     in
231 :     SplayObj{value = value', left = left', right = ap right}
232 :     end
233 :     in
234 :     MAP{root = ref(ap (!root)), nobj = nobj}
235 :     end
236 :    
237 :     fun map (af : 'a -> 'b) EMPTY = EMPTY
238 :     | map (af : 'a -> 'b) (MAP{root,nobj}) =
239 :     let fun ap (SplayNil : (K.ord_key * 'a) splay) = SplayNil
240 :     | ap (SplayObj{value,left,right}) = let
241 :     val left' = ap left
242 :     val value' = (#1 value, af (#2 value))
243 :     in
244 :     SplayObj{value = value', left = left', right = ap right}
245 :     end
246 :     in
247 :     MAP{root = ref(ap (!root)), nobj = nobj}
248 :     end
249 :    
250 :     (* the following are generic implementations of the unionWith and intersectWith
251 :     * operetions. These should be specialized for the internal representations
252 :     * at some point.
253 :     *)
254 :     fun unionWith f (m1, m2) = let
255 :     fun ins f (key, x, m) = (case find(m, key)
256 :     of NONE => insert(m, key, x)
257 :     | (SOME x') => insert(m, key, f(x, x'))
258 :     (* end case *))
259 :     in
260 :     if (numItems m1 > numItems m2)
261 :     then foldli (ins (fn (a, b) => f(b, a))) m1 m2
262 :     else foldli (ins f) m2 m1
263 :     end
264 :     fun unionWithi f (m1, m2) = let
265 :     fun ins f (key, x, m) = (case find(m, key)
266 :     of NONE => insert(m, key, x)
267 :     | (SOME x') => insert(m, key, f(key, x, x'))
268 :     (* end case *))
269 :     in
270 :     if (numItems m1 > numItems m2)
271 :     then foldli (ins (fn (k, a, b) => f(k, b, a))) m1 m2
272 :     else foldli (ins f) m2 m1
273 :     end
274 :    
275 :     fun intersectWith f (m1, m2) = let
276 :     (* iterate over the elements of m1, checking for membership in m2 *)
277 :     fun intersect f (m1, m2) = let
278 :     fun ins (key, x, m) = (case find(m2, key)
279 :     of NONE => m
280 :     | (SOME x') => insert(m, key, f(x, x'))
281 :     (* end case *))
282 :     in
283 :     foldli ins empty m1
284 :     end
285 :     in
286 :     if (numItems m1 > numItems m2)
287 :     then intersect f (m1, m2)
288 :     else intersect (fn (a, b) => f(b, a)) (m2, m1)
289 :     end
290 :    
291 :     fun intersectWithi f (m1, m2) = let
292 :     (* iterate over the elements of m1, checking for membership in m2 *)
293 :     fun intersect f (m1, m2) = let
294 :     fun ins (key, x, m) = (case find(m2, key)
295 :     of NONE => m
296 :     | (SOME x') => insert(m, key, f(key, x, x'))
297 :     (* end case *))
298 :     in
299 :     foldli ins empty m1
300 :     end
301 :     in
302 :     if (numItems m1 > numItems m2)
303 :     then intersect f (m1, m2)
304 :     else intersect (fn (k, a, b) => f(k, b, a)) (m2, m1)
305 :     end
306 :    
307 :     (* this is a generic implementation of mapPartial. It should
308 :     * be specialized to the data-structure at some point.
309 :     *)
310 :     fun mapPartial f m = let
311 :     fun g (key, item, m) = (case f item
312 :     of NONE => m
313 :     | (SOME item') => insert(m, key, item')
314 :     (* end case *))
315 :     in
316 :     foldli g empty m
317 :     end
318 :     fun mapPartiali f m = let
319 :     fun g (key, item, m) = (case f(key, item)
320 :     of NONE => m
321 :     | (SOME item') => insert(m, key, item')
322 :     (* end case *))
323 :     in
324 :     foldli g empty m
325 :     end
326 :    
327 :     (* this is a generic implementation of filter. It should
328 :     * be specialized to the data-structure at some point.
329 :     *)
330 :     fun filter predFn m = let
331 :     fun f (key, item, m) = if predFn item
332 :     then insert(m, key, item)
333 :     else m
334 :     in
335 :     foldli f empty m
336 :     end
337 :     fun filteri predFn m = let
338 :     fun f (key, item, m) = if predFn(key, item)
339 :     then insert(m, key, item)
340 :     else m
341 :     in
342 :     foldli f empty m
343 :     end
344 :    
345 :     end (* SplayDictFn *)

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