1 (* ========================================================================= *) |
1 (* ========================================================================= *) |
2 (* FINITE MAPS WITH A FIXED KEY TYPE *) |
2 (* FINITE MAPS WITH A FIXED KEY TYPE *) |
3 (* Copyright (c) 2004-2006 Joe Hurd, distributed under the BSD License *) |
3 (* Copyright (c) 2004 Joe Hurd, distributed under the BSD License *) |
4 (* ========================================================================= *) |
4 (* ========================================================================= *) |
5 |
5 |
6 signature KeyMap = |
6 signature KeyMap = |
7 sig |
7 sig |
8 |
8 |
|
9 (* ------------------------------------------------------------------------- *) |
|
10 (* A type of map keys. *) |
|
11 (* ------------------------------------------------------------------------- *) |
|
12 |
9 type key |
13 type key |
10 |
14 |
11 (* ------------------------------------------------------------------------- *) |
15 (* ------------------------------------------------------------------------- *) |
12 (* Finite maps *) |
16 (* A type of finite maps. *) |
13 (* ------------------------------------------------------------------------- *) |
17 (* ------------------------------------------------------------------------- *) |
14 |
18 |
15 type 'a map |
19 type 'a map |
16 |
20 |
|
21 (* ------------------------------------------------------------------------- *) |
|
22 (* Constructors. *) |
|
23 (* ------------------------------------------------------------------------- *) |
|
24 |
17 val new : unit -> 'a map |
25 val new : unit -> 'a map |
|
26 |
|
27 val singleton : key * 'a -> 'a map |
|
28 |
|
29 (* ------------------------------------------------------------------------- *) |
|
30 (* Map size. *) |
|
31 (* ------------------------------------------------------------------------- *) |
18 |
32 |
19 val null : 'a map -> bool |
33 val null : 'a map -> bool |
20 |
34 |
21 val size : 'a map -> int |
35 val size : 'a map -> int |
22 |
36 |
23 val singleton : key * 'a -> 'a map |
37 (* ------------------------------------------------------------------------- *) |
|
38 (* Querying. *) |
|
39 (* ------------------------------------------------------------------------- *) |
24 |
40 |
25 val inDomain : key -> 'a map -> bool |
41 val peekKey : 'a map -> key -> (key * 'a) option |
26 |
42 |
27 val peek : 'a map -> key -> 'a option |
43 val peek : 'a map -> key -> 'a option |
|
44 |
|
45 val get : 'a map -> key -> 'a (* raises Error *) |
|
46 |
|
47 val pick : 'a map -> key * 'a (* an arbitrary key/value pair *) |
|
48 |
|
49 val nth : 'a map -> int -> key * 'a (* in the range [0,size-1] *) |
|
50 |
|
51 val random : 'a map -> key * 'a |
|
52 |
|
53 (* ------------------------------------------------------------------------- *) |
|
54 (* Adding. *) |
|
55 (* ------------------------------------------------------------------------- *) |
28 |
56 |
29 val insert : 'a map -> key * 'a -> 'a map |
57 val insert : 'a map -> key * 'a -> 'a map |
30 |
58 |
31 val insertList : 'a map -> (key * 'a) list -> 'a map |
59 val insertList : 'a map -> (key * 'a) list -> 'a map |
32 |
60 |
33 val get : 'a map -> key -> 'a (* raises Error *) |
61 (* ------------------------------------------------------------------------- *) |
|
62 (* Removing. *) |
|
63 (* ------------------------------------------------------------------------- *) |
34 |
64 |
35 (* Union and intersect prefer keys in the second map *) |
65 val delete : 'a map -> key -> 'a map (* must be present *) |
36 |
66 |
37 val union : ('a * 'a -> 'a option) -> 'a map -> 'a map -> 'a map |
67 val remove : 'a map -> key -> 'a map |
38 |
68 |
39 val intersect : ('a * 'a -> 'a option) -> 'a map -> 'a map -> 'a map |
69 val deletePick : 'a map -> (key * 'a) * 'a map |
40 |
70 |
41 val delete : 'a map -> key -> 'a map (* raises Error *) |
71 val deleteNth : 'a map -> int -> (key * 'a) * 'a map |
42 |
72 |
43 val difference : 'a map -> 'a map -> 'a map |
73 val deleteRandom : 'a map -> (key * 'a) * 'a map |
|
74 |
|
75 (* ------------------------------------------------------------------------- *) |
|
76 (* Joining (all join operations prefer keys in the second map). *) |
|
77 (* ------------------------------------------------------------------------- *) |
|
78 |
|
79 val merge : |
|
80 {first : key * 'a -> 'c option, |
|
81 second : key * 'b -> 'c option, |
|
82 both : (key * 'a) * (key * 'b) -> 'c option} -> |
|
83 'a map -> 'b map -> 'c map |
|
84 |
|
85 val union : |
|
86 ((key * 'a) * (key * 'a) -> 'a option) -> |
|
87 'a map -> 'a map -> 'a map |
|
88 |
|
89 val intersect : |
|
90 ((key * 'a) * (key * 'b) -> 'c option) -> |
|
91 'a map -> 'b map -> 'c map |
|
92 |
|
93 (* ------------------------------------------------------------------------- *) |
|
94 (* Set operations on the domain. *) |
|
95 (* ------------------------------------------------------------------------- *) |
|
96 |
|
97 val inDomain : key -> 'a map -> bool |
|
98 |
|
99 val unionDomain : 'a map -> 'a map -> 'a map |
|
100 |
|
101 val unionListDomain : 'a map list -> 'a map |
|
102 |
|
103 val intersectDomain : 'a map -> 'a map -> 'a map |
|
104 |
|
105 val intersectListDomain : 'a map list -> 'a map |
|
106 |
|
107 val differenceDomain : 'a map -> 'a map -> 'a map |
|
108 |
|
109 val symmetricDifferenceDomain : 'a map -> 'a map -> 'a map |
|
110 |
|
111 val equalDomain : 'a map -> 'a map -> bool |
44 |
112 |
45 val subsetDomain : 'a map -> 'a map -> bool |
113 val subsetDomain : 'a map -> 'a map -> bool |
46 |
114 |
47 val equalDomain : 'a map -> 'a map -> bool |
115 val disjointDomain : 'a map -> 'a map -> bool |
|
116 |
|
117 (* ------------------------------------------------------------------------- *) |
|
118 (* Mapping and folding. *) |
|
119 (* ------------------------------------------------------------------------- *) |
48 |
120 |
49 val mapPartial : (key * 'a -> 'b option) -> 'a map -> 'b map |
121 val mapPartial : (key * 'a -> 'b option) -> 'a map -> 'b map |
50 |
|
51 val filter : (key * 'a -> bool) -> 'a map -> 'a map |
|
52 |
122 |
53 val map : (key * 'a -> 'b) -> 'a map -> 'b map |
123 val map : (key * 'a -> 'b) -> 'a map -> 'b map |
54 |
124 |
55 val app : (key * 'a -> unit) -> 'a map -> unit |
125 val app : (key * 'a -> unit) -> 'a map -> unit |
56 |
126 |
57 val transform : ('a -> 'b) -> 'a map -> 'b map |
127 val transform : ('a -> 'b) -> 'a map -> 'b map |
58 |
128 |
|
129 val filter : (key * 'a -> bool) -> 'a map -> 'a map |
|
130 |
|
131 val partition : |
|
132 (key * 'a -> bool) -> 'a map -> 'a map * 'a map |
|
133 |
59 val foldl : (key * 'a * 's -> 's) -> 's -> 'a map -> 's |
134 val foldl : (key * 'a * 's -> 's) -> 's -> 'a map -> 's |
60 |
135 |
61 val foldr : (key * 'a * 's -> 's) -> 's -> 'a map -> 's |
136 val foldr : (key * 'a * 's -> 's) -> 's -> 'a map -> 's |
|
137 |
|
138 (* ------------------------------------------------------------------------- *) |
|
139 (* Searching. *) |
|
140 (* ------------------------------------------------------------------------- *) |
62 |
141 |
63 val findl : (key * 'a -> bool) -> 'a map -> (key * 'a) option |
142 val findl : (key * 'a -> bool) -> 'a map -> (key * 'a) option |
64 |
143 |
65 val findr : (key * 'a -> bool) -> 'a map -> (key * 'a) option |
144 val findr : (key * 'a -> bool) -> 'a map -> (key * 'a) option |
66 |
145 |
70 |
149 |
71 val exists : (key * 'a -> bool) -> 'a map -> bool |
150 val exists : (key * 'a -> bool) -> 'a map -> bool |
72 |
151 |
73 val all : (key * 'a -> bool) -> 'a map -> bool |
152 val all : (key * 'a -> bool) -> 'a map -> bool |
74 |
153 |
75 val domain : 'a map -> key list |
154 val count : (key * 'a -> bool) -> 'a map -> int |
|
155 |
|
156 (* ------------------------------------------------------------------------- *) |
|
157 (* Comparing. *) |
|
158 (* ------------------------------------------------------------------------- *) |
|
159 |
|
160 val compare : ('a * 'a -> order) -> 'a map * 'a map -> order |
|
161 |
|
162 val equal : ('a -> 'a -> bool) -> 'a map -> 'a map -> bool |
|
163 |
|
164 (* ------------------------------------------------------------------------- *) |
|
165 (* Converting to and from lists. *) |
|
166 (* ------------------------------------------------------------------------- *) |
|
167 |
|
168 val keys : 'a map -> key list |
|
169 |
|
170 val values : 'a map -> 'a list |
76 |
171 |
77 val toList : 'a map -> (key * 'a) list |
172 val toList : 'a map -> (key * 'a) list |
78 |
173 |
79 val fromList : (key * 'a) list -> 'a map |
174 val fromList : (key * 'a) list -> 'a map |
80 |
175 |
81 val compare : ('a * 'a -> order) -> 'a map * 'a map -> order |
176 (* ------------------------------------------------------------------------- *) |
82 |
177 (* Pretty-printing. *) |
83 val equal : ('a -> 'a -> bool) -> 'a map -> 'a map -> bool |
178 (* ------------------------------------------------------------------------- *) |
84 |
|
85 val random : 'a map -> key * 'a (* raises Empty *) |
|
86 |
179 |
87 val toString : 'a map -> string |
180 val toString : 'a map -> string |
88 |
181 |
89 (* ------------------------------------------------------------------------- *) |
182 (* ------------------------------------------------------------------------- *) |
90 (* Iterators over maps *) |
183 (* Iterators over maps. *) |
91 (* ------------------------------------------------------------------------- *) |
184 (* ------------------------------------------------------------------------- *) |
92 |
185 |
93 type 'a iterator |
186 type 'a iterator |
94 |
187 |
95 val mkIterator : 'a map -> 'a iterator option |
188 val mkIterator : 'a map -> 'a iterator option |