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