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

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