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 29 - (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 :     fun insert (l, key, item) = let
23 :     fun f [] = [(key, item)]
24 :     | f ((elem as (key', _))::r) = (case Key.compare(key, key')
25 :     of LESS => (key, item) :: elem :: r
26 :     | EQUAL => (key, item) :: r
27 :     | GREATER => elem :: (f r)
28 :     (* end case *))
29 :     in
30 :     f l
31 :     end
32 : monnier 29 fun insert' ((k, x), m) = insert(m, k, x)
33 : monnier 2
34 :     (* Look for an item, return NONE if the item doesn't exist *)
35 :     fun find (l, key) = let
36 :     fun f [] = NONE
37 :     | f ((key', x) :: r) = (case Key.compare(key, key')
38 :     of LESS => NONE
39 :     | EQUAL => SOME x
40 :     | GREATER => f r
41 :     (* end case *))
42 :     in
43 :     f l
44 :     end
45 :    
46 :     (* Remove an item, returning new map and value removed.
47 :     * Raise LibBase.NotFound if not found.
48 :     *)
49 :     fun remove (l, key) = let
50 :     fun f (_, []) = raise LibBase.NotFound
51 :     | f (prefix, (elem as (key', x)) :: r) = (case Key.compare(key, key')
52 :     of LESS => raise LibBase.NotFound
53 :     | EQUAL => (List.revAppend(prefix, r), x)
54 :     | GREATER => f(elem :: prefix, r)
55 :     (* end case *))
56 :     in
57 :     f ([], l)
58 :     end
59 :    
60 :     (* Return the number of items in the map *)
61 :     fun numItems l = List.length l
62 :    
63 :     (* Return a list of the items (and their keys) in the map *)
64 :     fun listItems (l : 'a map) = List.map #2 l
65 :     fun listItemsi l = l
66 :    
67 :     fun collate cmpRng = let
68 :     fun cmp ([], []) = EQUAL
69 :     | cmp ([], _) = LESS
70 :     | cmp (_, []) = GREATER
71 :     | cmp ((x1, y1)::r1, (x2, y2)::r2) = (case Key.compare(x1, x2)
72 :     of EQUAL => (case cmpRng(y1, y2)
73 :     of EQUAL => cmp (r1, r2)
74 :     | order => order
75 :     (* end case *))
76 :     | order => order
77 :     (* end case *))
78 :     in
79 :     cmp
80 :     end
81 :    
82 :     (* return a map whose domain is the union of the domains of the two input
83 :     * maps, using the supplied function to define the map on elements that
84 :     * are in both domains.
85 :     *)
86 :     fun unionWith f (m1 : 'a map, m2 : 'a map) = let
87 :     fun merge ([], [], l) = List.rev l
88 :     | merge ([], m2, l) = List.revAppend(l, m2)
89 :     | merge (m1, [], l) = List.revAppend(l, m1)
90 :     | merge (m1 as ((k1, x1)::r1), m2 as ((k2, x2)::r2), l) = (
91 :     case Key.compare (k1, k2)
92 :     of LESS => merge (r1, m2, (k1, x1)::l)
93 :     | EQUAL => merge (r1, r2, (k1, f(x1, x2)) :: l)
94 :     | GREATER => merge (m1, r2, (k2, x2)::l)
95 :     (* end case *))
96 :     in
97 :     merge (m1, m2, [])
98 :     end
99 :     fun unionWithi f (m1 : 'a map, m2 : 'a map) = let
100 :     fun merge ([], [], l) = List.rev l
101 :     | merge ([], m2, l) = List.revAppend(l, m2)
102 :     | merge (m1, [], l) = List.revAppend(l, m1)
103 :     | merge (m1 as ((k1, x1)::r1), m2 as ((k2, x2)::r2), l) = (
104 :     case Key.compare (k1, k2)
105 :     of LESS => merge (r1, m2, (k1, x1)::l)
106 :     | EQUAL => merge (r1, r2, (k1, f(k1, x1, x2)) :: l)
107 :     | GREATER => merge (m1, r2, (k2, x2)::l)
108 :     (* end case *))
109 :     in
110 :     merge (m1, m2, [])
111 :     end
112 :    
113 :     (* return a map whose domain is the intersection of the domains of the
114 :     * two input maps, using the supplied function to define the range.
115 :     *)
116 :     fun intersectWith f (m1 : 'a map, m2 : 'b map) = let
117 :     fun merge (m1 as ((k1, x1)::r1), m2 as ((k2, x2)::r2), l) = (
118 :     case Key.compare (k1, k2)
119 :     of LESS => merge (r1, m2, l)
120 :     | EQUAL => merge (r1, r2, (k1, f(x1, x2)) :: l)
121 :     | GREATER => merge (m1, r2, l)
122 :     (* end case *))
123 :     | merge (_, _, l) = List.rev l
124 :     in
125 :     merge (m1, m2, [])
126 :     end
127 :     fun intersectWithi f (m1 : 'a map, m2 : 'b map) = let
128 :     fun merge (m1 as ((k1, x1)::r1), m2 as ((k2, x2)::r2), l) = (
129 :     case Key.compare (k1, k2)
130 :     of LESS => merge (r1, m2, l)
131 :     | EQUAL => merge (r1, r2, (k1, f(k1, x1, x2)) :: l)
132 :     | GREATER => merge (m1, r2, l)
133 :     (* end case *))
134 :     | merge (_, _, l) = List.rev l
135 :     in
136 :     merge (m1, m2, [])
137 :     end
138 :    
139 :     (* Apply a function to the entries of the map in map order. *)
140 :     val appi = List.app
141 :     fun app f l = appi (fn (_, item) => f item) l
142 :    
143 :     (* Create a new table by applying a map function to the
144 :     * name/value pairs in the table.
145 :     *)
146 :     fun mapi f l = List.map (fn (key, item) => (key, f(key, item))) l
147 :     fun map f l = List.map (fn (key, item) => (key, f item)) l
148 :    
149 :     (* Apply a folding function to the entries of the map
150 :     * in increasing map order.
151 :     *)
152 :     fun foldli f init l =
153 :     List.foldl (fn ((key, item), accum) => f(key, item, accum)) init l
154 :     fun foldl f init l = List.foldl (fn ((_, item), accum) => f(item, accum)) init l
155 :    
156 :     (* Apply a folding function to the entries of the map
157 :     * in decreasing map order.
158 :     *)
159 :     fun foldri f init l =
160 :     List.foldr (fn ((key, item), accum) => f(key, item, accum)) init l
161 :     fun foldr f init l = List.foldr (fn ((_, item), accum) => f(item, accum)) init l
162 :    
163 :     fun filter pred l = List.filter (fn (_, item) => pred item) l
164 :     fun filteri pred l = List.filter pred l
165 :    
166 :     fun mapPartiali f l = let
167 :     fun f' (key, item) = (case f (key, item)
168 :     of NONE => NONE
169 :     | SOME y => SOME(key, y)
170 :     (* end case *))
171 :     in
172 :     List.mapPartial f' l
173 :     end
174 :     fun mapPartial f l = mapPartiali (fn (_, item) => f item) l
175 :    
176 :     end (* IntListMap *)
177 :    

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