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 651 - (view) (download)
Original Path: sml/trunk/src/smlnj-lib/Util/list-map-fn.sml

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 :     (* Remove an item, returning new map and value removed.
68 :     * Raise LibBase.NotFound if not found.
69 :     *)
70 :     fun remove (l, key) = let
71 :     fun f (_, []) = raise LibBase.NotFound
72 :     | f (prefix, (elem as (key', x)) :: r) = (case Key.compare(key, key')
73 :     of LESS => raise LibBase.NotFound
74 :     | EQUAL => (List.revAppend(prefix, r), x)
75 :     | GREATER => f(elem :: prefix, r)
76 :     (* end case *))
77 :     in
78 :     f ([], l)
79 :     end
80 :    
81 :     (* Return the number of items in the map *)
82 :     fun numItems l = List.length l
83 :    
84 :     (* Return a list of the items (and their keys) in the map *)
85 :     fun listItems (l : 'a map) = List.map #2 l
86 :     fun listItemsi l = l
87 :    
88 : monnier 411 fun listKeys (l : 'a map) = List.map #1 l
89 :    
90 : monnier 2 fun collate cmpRng = let
91 :     fun cmp ([], []) = EQUAL
92 :     | cmp ([], _) = LESS
93 :     | cmp (_, []) = GREATER
94 :     | cmp ((x1, y1)::r1, (x2, y2)::r2) = (case Key.compare(x1, x2)
95 :     of EQUAL => (case cmpRng(y1, y2)
96 :     of EQUAL => cmp (r1, r2)
97 :     | order => order
98 :     (* end case *))
99 :     | order => order
100 :     (* end case *))
101 :     in
102 :     cmp
103 :     end
104 :    
105 :     (* return a map whose domain is the union of the domains of the two input
106 :     * maps, using the supplied function to define the map on elements that
107 :     * are in both domains.
108 :     *)
109 :     fun unionWith f (m1 : 'a map, m2 : 'a map) = let
110 :     fun merge ([], [], l) = List.rev l
111 :     | merge ([], m2, l) = List.revAppend(l, m2)
112 :     | merge (m1, [], l) = List.revAppend(l, m1)
113 :     | merge (m1 as ((k1, x1)::r1), m2 as ((k2, x2)::r2), l) = (
114 :     case Key.compare (k1, k2)
115 :     of LESS => merge (r1, m2, (k1, x1)::l)
116 :     | EQUAL => merge (r1, r2, (k1, f(x1, x2)) :: l)
117 :     | GREATER => merge (m1, r2, (k2, x2)::l)
118 :     (* end case *))
119 :     in
120 :     merge (m1, m2, [])
121 :     end
122 :     fun unionWithi 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(k1, x1, x2)) :: l)
130 :     | GREATER => merge (m1, r2, (k2, x2)::l)
131 :     (* end case *))
132 :     in
133 :     merge (m1, m2, [])
134 :     end
135 :    
136 :     (* return a map whose domain is the intersection of the domains of the
137 :     * two input maps, using the supplied function to define the range.
138 :     *)
139 :     fun intersectWith f (m1 : 'a map, m2 : 'b map) = let
140 :     fun merge (m1 as ((k1, x1)::r1), m2 as ((k2, x2)::r2), l) = (
141 :     case Key.compare (k1, k2)
142 :     of LESS => merge (r1, m2, l)
143 :     | EQUAL => merge (r1, r2, (k1, f(x1, x2)) :: l)
144 :     | GREATER => merge (m1, r2, l)
145 :     (* end case *))
146 :     | merge (_, _, l) = List.rev l
147 :     in
148 :     merge (m1, m2, [])
149 :     end
150 :     fun intersectWithi f (m1 : 'a map, m2 : 'b map) = let
151 :     fun merge (m1 as ((k1, x1)::r1), m2 as ((k2, x2)::r2), l) = (
152 :     case Key.compare (k1, k2)
153 :     of LESS => merge (r1, m2, l)
154 :     | EQUAL => merge (r1, r2, (k1, f(k1, x1, x2)) :: l)
155 :     | GREATER => merge (m1, r2, l)
156 :     (* end case *))
157 :     | merge (_, _, l) = List.rev l
158 :     in
159 :     merge (m1, m2, [])
160 :     end
161 :    
162 :     (* Apply a function to the entries of the map in map order. *)
163 :     val appi = List.app
164 :     fun app f l = appi (fn (_, item) => f item) l
165 :    
166 :     (* Create a new table by applying a map function to the
167 :     * name/value pairs in the table.
168 :     *)
169 :     fun mapi f l = List.map (fn (key, item) => (key, f(key, item))) l
170 :     fun map f l = List.map (fn (key, item) => (key, f item)) l
171 :    
172 :     (* Apply a folding function to the entries of the map
173 :     * in increasing map order.
174 :     *)
175 :     fun foldli f init l =
176 :     List.foldl (fn ((key, item), accum) => f(key, item, accum)) init l
177 :     fun foldl f init l = List.foldl (fn ((_, item), accum) => f(item, accum)) init l
178 :    
179 :     (* Apply a folding function to the entries of the map
180 :     * in decreasing map order.
181 :     *)
182 :     fun foldri f init l =
183 :     List.foldr (fn ((key, item), accum) => f(key, item, accum)) init l
184 :     fun foldr f init l = List.foldr (fn ((_, item), accum) => f(item, accum)) init l
185 :    
186 :     fun filter pred l = List.filter (fn (_, item) => pred item) l
187 :     fun filteri pred l = List.filter pred l
188 :    
189 :     fun mapPartiali f l = let
190 :     fun f' (key, item) = (case f (key, item)
191 :     of NONE => NONE
192 :     | SOME y => SOME(key, y)
193 :     (* end case *))
194 :     in
195 :     List.mapPartial f' l
196 :     end
197 :     fun mapPartial f l = mapPartiali (fn (_, item) => f item) l
198 :    
199 :     end (* functor ListMapFn *)
200 :    

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