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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2274 - (view) (download)

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

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