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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 498 - (view) (download)

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

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