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/trunk/Util/int-list-map.sml
ViewVC logotype

Annotation of /smlnj-lib/trunk/Util/int-list-map.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3759 - (view) (download)

1 : monnier 2 (* int-list-map.sml
2 :     *
3 : jhr 3759 * COPYRIGHT (c) 2012 The Fellowship of SML/NJ (http://www.smlnj.org)
4 :     * All rights reserved.
5 :     *
6 : monnier 2 * COPYRIGHT (c) 1996 by AT&T Research. See COPYRIGHT file for details.
7 :     *
8 :     * An implementation of finite maps on integer keys, which uses a sorted list
9 :     * representation.
10 :     *)
11 :    
12 :     structure IntListMap :> ORD_MAP where type Key.ord_key = Int.int =
13 :     struct
14 :    
15 :     structure Key =
16 :     struct
17 :     type ord_key = int
18 :     val compare = Int.compare
19 :     end
20 :    
21 :     type 'a map = (int * 'a) list
22 :    
23 :     val empty = []
24 :    
25 : monnier 289 fun isEmpty [] = true
26 :     | isEmpty _ = false
27 :    
28 :     (* return the first item in the map (or NONE if it is empty) *)
29 :     fun first [] = NONE
30 :     | first ((_, value)::_) = SOME value
31 :    
32 :     (* return the first item in the map and its key (or NONE if it is empty) *)
33 :     fun firsti [] = NONE
34 :     | firsti ((key, value)::_) = SOME(key, value)
35 :    
36 : monnier 411 fun singleton (key, item) = [(key, item)]
37 :    
38 : monnier 2 fun insert (l, key, item) = let
39 :     fun f [] = [(key, item)]
40 :     | f ((elem as (key', _))::r) = (case Key.compare(key, key')
41 :     of LESS => (key, item) :: elem :: r
42 :     | EQUAL => (key, item) :: r
43 :     | GREATER => elem :: (f r)
44 :     (* end case *))
45 :     in
46 :     f l
47 :     end
48 : monnier 29 fun insert' ((k, x), m) = insert(m, k, x)
49 : monnier 2
50 : monnier 411 (* return true if the key is in the map's domain *)
51 :     fun inDomain (l, key) = let
52 :     fun f [] = false
53 :     | f ((key', x) :: r) = (key' <= key) andalso ((key' = key) orelse f r)
54 :     in
55 :     f l
56 :     end
57 :    
58 : monnier 2 (* Look for an item, return NONE if the item doesn't exist *)
59 :     fun find (l, key) = let
60 :     fun f [] = NONE
61 : monnier 411 | f ((key', x) :: r) =
62 :     if (key < key') then NONE
63 :     else if (key = key') then SOME x
64 :     else f r
65 : monnier 2 in
66 :     f l
67 :     end
68 :    
69 : jhr 2274 (* Look for an item, raise NotFound if the item doesn't exist *)
70 :     fun lookup (l, key) = let
71 :     fun f [] = raise LibBase.NotFound
72 :     | f ((key', x) :: r) =
73 :     if (key < key') then raise LibBase.NotFound
74 :     else if (key = key') then x
75 :     else f r
76 :     in
77 :     f l
78 :     end
79 :    
80 : monnier 2 (* Remove an item, returning new map and value removed.
81 :     * Raise LibBase.NotFound if not found.
82 :     *)
83 :     fun remove (l, key) = let
84 :     fun f (_, []) = raise LibBase.NotFound
85 :     | f (prefix, (elem as (key', x)) :: r) = (case Key.compare(key, key')
86 :     of LESS => raise LibBase.NotFound
87 :     | EQUAL => (List.revAppend(prefix, r), x)
88 :     | GREATER => f(elem :: prefix, r)
89 :     (* end case *))
90 :     in
91 :     f ([], l)
92 :     end
93 :    
94 :     (* Return the number of items in the map *)
95 :     fun numItems l = List.length l
96 :    
97 :     (* Return a list of the items (and their keys) in the map *)
98 :     fun listItems (l : 'a map) = List.map #2 l
99 :     fun listItemsi l = l
100 :    
101 : monnier 411 fun listKeys (l : 'a map) = List.map #1 l
102 :    
103 : monnier 2 fun collate cmpRng = let
104 :     fun cmp ([], []) = EQUAL
105 :     | cmp ([], _) = LESS
106 :     | cmp (_, []) = GREATER
107 :     | cmp ((x1, y1)::r1, (x2, y2)::r2) = (case Key.compare(x1, x2)
108 :     of EQUAL => (case cmpRng(y1, y2)
109 :     of EQUAL => cmp (r1, r2)
110 :     | order => order
111 :     (* end case *))
112 :     | order => order
113 :     (* end case *))
114 :     in
115 :     cmp
116 :     end
117 :    
118 :     (* return a map whose domain is the union of the domains of the two input
119 :     * maps, using the supplied function to define the map on elements that
120 :     * are in both domains.
121 :     *)
122 :     fun unionWith f (m1 : 'a map, m2 : 'a map) = let
123 :     fun merge ([], [], l) = List.rev l
124 :     | merge ([], m2, l) = List.revAppend(l, m2)
125 :     | merge (m1, [], l) = List.revAppend(l, m1)
126 :     | merge (m1 as ((k1, x1)::r1), m2 as ((k2, x2)::r2), l) = (
127 :     case Key.compare (k1, k2)
128 :     of LESS => merge (r1, m2, (k1, x1)::l)
129 :     | EQUAL => merge (r1, r2, (k1, f(x1, x2)) :: l)
130 :     | GREATER => merge (m1, r2, (k2, x2)::l)
131 :     (* end case *))
132 :     in
133 :     merge (m1, m2, [])
134 :     end
135 :     fun unionWithi f (m1 : 'a map, m2 : 'a map) = let
136 :     fun merge ([], [], l) = List.rev l
137 :     | merge ([], m2, l) = List.revAppend(l, m2)
138 :     | merge (m1, [], l) = List.revAppend(l, m1)
139 :     | merge (m1 as ((k1, x1)::r1), m2 as ((k2, x2)::r2), l) = (
140 :     case Key.compare (k1, k2)
141 :     of LESS => merge (r1, m2, (k1, x1)::l)
142 :     | EQUAL => merge (r1, r2, (k1, f(k1, x1, x2)) :: l)
143 :     | GREATER => merge (m1, r2, (k2, x2)::l)
144 :     (* end case *))
145 :     in
146 :     merge (m1, m2, [])
147 :     end
148 :    
149 :     (* return a map whose domain is the intersection of the domains of the
150 :     * two input maps, using the supplied function to define the range.
151 :     *)
152 :     fun intersectWith f (m1 : 'a map, m2 : 'b map) = let
153 :     fun merge (m1 as ((k1, x1)::r1), m2 as ((k2, x2)::r2), l) = (
154 :     case Key.compare (k1, k2)
155 :     of LESS => merge (r1, m2, l)
156 :     | EQUAL => merge (r1, r2, (k1, f(x1, x2)) :: l)
157 :     | GREATER => merge (m1, r2, l)
158 :     (* end case *))
159 :     | merge (_, _, l) = List.rev l
160 :     in
161 :     merge (m1, m2, [])
162 :     end
163 :     fun intersectWithi f (m1 : 'a map, m2 : 'b map) = let
164 :     fun merge (m1 as ((k1, x1)::r1), m2 as ((k2, x2)::r2), l) = (
165 :     case Key.compare (k1, k2)
166 :     of LESS => merge (r1, m2, l)
167 :     | EQUAL => merge (r1, r2, (k1, f(k1, x1, x2)) :: l)
168 :     | GREATER => merge (m1, r2, l)
169 :     (* end case *))
170 :     | merge (_, _, l) = List.rev l
171 :     in
172 :     merge (m1, m2, [])
173 :     end
174 :    
175 : jhr 1193 fun mergeWith f (m1 : 'a map, m2 : 'b map) = let
176 :     fun merge (m1 as ((k1, x1)::r1), m2 as ((k2, x2)::r2), l) =
177 :     if (k1 < k2)
178 :     then mergef (k1, SOME x1, NONE, r1, m2, l)
179 :     else if (k1 = k2)
180 :     then mergef (k1, SOME x1, SOME x2, r1, r2, l)
181 :     else mergef (k2, NONE, SOME x2, m1, r2, l)
182 :     | merge ([], [], l) = List.rev l
183 :     | merge ((k1, x1)::r1, [], l) = mergef (k1, SOME x1, NONE, r1, [], l)
184 :     | merge ([], (k2, x2)::r2, l) = mergef (k2, NONE, SOME x2, [], r2, l)
185 :     and mergef (k, x1, x2, r1, r2, l) = (case f (x1, x2)
186 :     of NONE => merge (r1, r2, l)
187 :     | SOME y => merge (r1, r2, (k, y)::l)
188 :     (* end case *))
189 :     in
190 :     merge (m1, m2, [])
191 :     end
192 :     fun mergeWithi f (m1 : 'a map, m2 : 'b map) = let
193 :     fun merge (m1 as ((k1, x1)::r1), m2 as ((k2, x2)::r2), l) =
194 :     if (k1 < k2)
195 :     then mergef (k1, SOME x1, NONE, r1, m2, l)
196 :     else if (k1 = k2)
197 :     then mergef (k1, SOME x1, SOME x2, r1, r2, l)
198 :     else mergef (k2, NONE, SOME x2, m1, r2, l)
199 :     | merge ([], [], l) = List.rev l
200 :     | merge ((k1, x1)::r1, [], l) = mergef (k1, SOME x1, NONE, r1, [], l)
201 :     | merge ([], (k2, x2)::r2, l) = mergef (k2, NONE, SOME x2, [], r2, l)
202 :     and mergef (k, x1, x2, r1, r2, l) = (case f (k, x1, x2)
203 :     of NONE => merge (r1, r2, l)
204 :     | SOME y => merge (r1, r2, (k, y)::l)
205 :     (* end case *))
206 :     in
207 :     merge (m1, m2, [])
208 :     end
209 :    
210 : monnier 2 (* Apply a function to the entries of the map in map order. *)
211 :     val appi = List.app
212 :     fun app f l = appi (fn (_, item) => f item) l
213 :    
214 :     (* Create a new table by applying a map function to the
215 :     * name/value pairs in the table.
216 :     *)
217 :     fun mapi f l = List.map (fn (key, item) => (key, f(key, item))) l
218 :     fun map f l = List.map (fn (key, item) => (key, f item)) l
219 :    
220 :     (* Apply a folding function to the entries of the map
221 :     * in increasing map order.
222 :     *)
223 :     fun foldli f init l =
224 :     List.foldl (fn ((key, item), accum) => f(key, item, accum)) init l
225 :     fun foldl f init l = List.foldl (fn ((_, item), accum) => f(item, accum)) init l
226 :    
227 :     (* Apply a folding function to the entries of the map
228 :     * in decreasing map order.
229 :     *)
230 :     fun foldri f init l =
231 :     List.foldr (fn ((key, item), accum) => f(key, item, accum)) init l
232 :     fun foldr f init l = List.foldr (fn ((_, item), accum) => f(item, accum)) init l
233 :    
234 :     fun filter pred l = List.filter (fn (_, item) => pred item) l
235 :     fun filteri pred l = List.filter pred l
236 :    
237 :     fun mapPartiali f l = let
238 :     fun f' (key, item) = (case f (key, item)
239 :     of NONE => NONE
240 :     | SOME y => SOME(key, y)
241 :     (* end case *))
242 :     in
243 :     List.mapPartial f' l
244 :     end
245 :     fun mapPartial f l = mapPartiali (fn (_, item) => f item) l
246 :    
247 : jhr 3759 (* check the elements of a map with a predicate and return true if
248 :     * any element satisfies the predicate. Return false otherwise.
249 :     * Elements are checked in key order.
250 :     *)
251 :     fun exists pred = let
252 :     fun exists' [] = false
253 :     | exists' ((_, x)::r) = pred x orelse exists' r
254 :     in
255 :     exists'
256 :     end
257 :     fun existsi pred = let
258 :     fun exists' [] = false
259 :     | exists' (arg::r) = pred arg orelse exists' r
260 :     in
261 :     exists'
262 :     end
263 :    
264 :     (* check the elements of a map with a predicate and return true if
265 :     * they all satisfy the predicate. Return false otherwise. Elements
266 :     * are checked in key order.
267 :     *)
268 :     fun all pred = let
269 :     fun all' [] = false
270 :     | all' ((_, x)::r) = pred x andalso all' r
271 :     in
272 :     all'
273 :     end
274 :     fun alli pred = let
275 :     fun all' [] = false
276 :     | all' (arg::r) = pred arg andalso all' r
277 :     in
278 :     all'
279 :     end
280 :    
281 : monnier 2 end (* IntListMap *)
282 :    

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