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