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

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