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

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