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 411 - (view) (download)
Original Path: sml/branches/SMLNJ/src/smlnj-lib/Util/int-list-map.sml

1 : monnier 2 (* int-list-map.sml
2 :     *
3 :     * COPYRIGHT (c) 1996 by AT&T Research. See COPYRIGHT file for details.
4 :     *
5 :     * An implementation of finite maps on integer keys, which uses a sorted list
6 :     * representation.
7 :     *)
8 :    
9 :     structure IntListMap :> ORD_MAP where type Key.ord_key = Int.int =
10 :     struct
11 :    
12 :     structure Key =
13 :     struct
14 :     type ord_key = int
15 :     val compare = Int.compare
16 :     end
17 :    
18 :     type 'a map = (int * 'a) list
19 :    
20 :     val empty = []
21 :    
22 : monnier 289 fun isEmpty [] = true
23 :     | isEmpty _ = false
24 :    
25 :     (* return the first item in the map (or NONE if it is empty) *)
26 :     fun first [] = NONE
27 :     | first ((_, value)::_) = SOME value
28 :    
29 :     (* return the first item in the map and its key (or NONE if it is empty) *)
30 :     fun firsti [] = NONE
31 :     | firsti ((key, value)::_) = SOME(key, value)
32 :    
33 : monnier 411 fun singleton (key, item) = [(key, item)]
34 :    
35 : monnier 2 fun insert (l, key, item) = let
36 :     fun f [] = [(key, item)]
37 :     | f ((elem as (key', _))::r) = (case Key.compare(key, key')
38 :     of LESS => (key, item) :: elem :: r
39 :     | EQUAL => (key, item) :: r
40 :     | GREATER => elem :: (f r)
41 :     (* end case *))
42 :     in
43 :     f l
44 :     end
45 : monnier 29 fun insert' ((k, x), m) = insert(m, k, x)
46 : monnier 2
47 : monnier 411 (* return true if the key is in the map's domain *)
48 :     fun inDomain (l, key) = let
49 :     fun f [] = false
50 :     | f ((key', x) :: r) = (key' <= key) andalso ((key' = key) orelse f r)
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 : monnier 411 | f ((key', x) :: r) =
59 :     if (key < key') then NONE
60 :     else if (key = key') then SOME x
61 :     else f r
62 : monnier 2 in
63 :     f l
64 :     end
65 :    
66 :     (* Remove an item, returning new map and value removed.
67 :     * Raise LibBase.NotFound if not found.
68 :     *)
69 :     fun remove (l, key) = let
70 :     fun f (_, []) = raise LibBase.NotFound
71 :     | f (prefix, (elem as (key', x)) :: r) = (case Key.compare(key, key')
72 :     of LESS => raise LibBase.NotFound
73 :     | EQUAL => (List.revAppend(prefix, r), x)
74 :     | GREATER => f(elem :: prefix, r)
75 :     (* end case *))
76 :     in
77 :     f ([], l)
78 :     end
79 :    
80 :     (* Return the number of items in the map *)
81 :     fun numItems l = List.length l
82 :    
83 :     (* Return a list of the items (and their keys) in the map *)
84 :     fun listItems (l : 'a map) = List.map #2 l
85 :     fun listItemsi l = l
86 :    
87 : monnier 411 fun listKeys (l : 'a map) = List.map #1 l
88 :    
89 : monnier 2 fun collate cmpRng = let
90 :     fun cmp ([], []) = EQUAL
91 :     | cmp ([], _) = LESS
92 :     | cmp (_, []) = GREATER
93 :     | cmp ((x1, y1)::r1, (x2, y2)::r2) = (case Key.compare(x1, x2)
94 :     of EQUAL => (case cmpRng(y1, y2)
95 :     of EQUAL => cmp (r1, r2)
96 :     | order => order
97 :     (* end case *))
98 :     | order => order
99 :     (* end case *))
100 :     in
101 :     cmp
102 :     end
103 :    
104 :     (* return a map whose domain is the union of the domains of the two input
105 :     * maps, using the supplied function to define the map on elements that
106 :     * are in both domains.
107 :     *)
108 :     fun unionWith f (m1 : 'a map, m2 : 'a map) = let
109 :     fun merge ([], [], l) = List.rev l
110 :     | merge ([], m2, l) = List.revAppend(l, m2)
111 :     | merge (m1, [], l) = List.revAppend(l, m1)
112 :     | merge (m1 as ((k1, x1)::r1), m2 as ((k2, x2)::r2), l) = (
113 :     case Key.compare (k1, k2)
114 :     of LESS => merge (r1, m2, (k1, x1)::l)
115 :     | EQUAL => merge (r1, r2, (k1, f(x1, x2)) :: l)
116 :     | GREATER => merge (m1, r2, (k2, x2)::l)
117 :     (* end case *))
118 :     in
119 :     merge (m1, m2, [])
120 :     end
121 :     fun unionWithi 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(k1, x1, x2)) :: l)
129 :     | GREATER => merge (m1, r2, (k2, x2)::l)
130 :     (* end case *))
131 :     in
132 :     merge (m1, m2, [])
133 :     end
134 :    
135 :     (* return a map whose domain is the intersection of the domains of the
136 :     * two input maps, using the supplied function to define the range.
137 :     *)
138 :     fun intersectWith f (m1 : 'a map, m2 : 'b map) = let
139 :     fun merge (m1 as ((k1, x1)::r1), m2 as ((k2, x2)::r2), l) = (
140 :     case Key.compare (k1, k2)
141 :     of LESS => merge (r1, m2, l)
142 :     | EQUAL => merge (r1, r2, (k1, f(x1, x2)) :: l)
143 :     | GREATER => merge (m1, r2, l)
144 :     (* end case *))
145 :     | merge (_, _, l) = List.rev l
146 :     in
147 :     merge (m1, m2, [])
148 :     end
149 :     fun intersectWithi f (m1 : 'a map, m2 : 'b map) = let
150 :     fun merge (m1 as ((k1, x1)::r1), m2 as ((k2, x2)::r2), l) = (
151 :     case Key.compare (k1, k2)
152 :     of LESS => merge (r1, m2, l)
153 :     | EQUAL => merge (r1, r2, (k1, f(k1, x1, x2)) :: l)
154 :     | GREATER => merge (m1, r2, l)
155 :     (* end case *))
156 :     | merge (_, _, l) = List.rev l
157 :     in
158 :     merge (m1, m2, [])
159 :     end
160 :    
161 :     (* Apply a function to the entries of the map in map order. *)
162 :     val appi = List.app
163 :     fun app f l = appi (fn (_, item) => f item) l
164 :    
165 :     (* Create a new table by applying a map function to the
166 :     * name/value pairs in the table.
167 :     *)
168 :     fun mapi f l = List.map (fn (key, item) => (key, f(key, item))) l
169 :     fun map f l = List.map (fn (key, item) => (key, f item)) l
170 :    
171 :     (* Apply a folding function to the entries of the map
172 :     * in increasing map order.
173 :     *)
174 :     fun foldli f init l =
175 :     List.foldl (fn ((key, item), accum) => f(key, item, accum)) init l
176 :     fun foldl f init l = List.foldl (fn ((_, item), accum) => f(item, accum)) init l
177 :    
178 :     (* Apply a folding function to the entries of the map
179 :     * in decreasing map order.
180 :     *)
181 :     fun foldri f init l =
182 :     List.foldr (fn ((key, item), accum) => f(key, item, accum)) init l
183 :     fun foldr f init l = List.foldr (fn ((_, item), accum) => f(item, accum)) init l
184 :    
185 :     fun filter pred l = List.filter (fn (_, item) => pred item) l
186 :     fun filteri pred l = List.filter pred l
187 :    
188 :     fun mapPartiali f l = let
189 :     fun f' (key, item) = (case f (key, item)
190 :     of NONE => NONE
191 :     | SOME y => SOME(key, y)
192 :     (* end case *))
193 :     in
194 :     List.mapPartial f' l
195 :     end
196 :     fun mapPartial f l = mapPartiali (fn (_, item) => f item) l
197 :    
198 :     end (* IntListMap *)
199 :    

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