1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
 
//! ADT.Set implements a datatype for sets. These sets behave much 
//! like multisets, except that they are restricted to containing only 
//! one instance of each member value. 
//! 
//! From a performance viewpoint, it is probably more efficient for a 
//! Pike program to use mappings to serve as sets, rather than using 
//! an ADT.Set,so ADT.Set is mainly provided for the sake of completeness 
//! and code readability. 
 
#pike __REAL_VERSION__ 
 
 
private multiset set; 
 
 
//! Remove all items from the set. 
void reset() 
{ 
  set = (<>); 
} 
 
 
//! Add @[items] to the set. 
void add(mixed ... items) 
{ 
  foreach(items, mixed item) 
    if (!set[item]) 
      set[item] = 1; 
} 
 
 
//! Remove @[items] from the set. 
void remove(mixed ... items) 
{ 
  foreach(items, mixed item) 
    if (set[item]) 
      set[item] = 0; 
} 
 
 
//! Check whether a value is a member of the set. 
int(0..1) contains(mixed item) 
{ 
  return set[item]; 
} 
 
 
//! Returns 1 if the set is empty, otherwise 0. 
int(0..1) is_empty() 
{ 
  return sizeof(set) == 0; 
} 
 
 
//! Map the values of a set: calls the map function @[f] once for each 
//! member of the set, returning an array which contains the result of 
//! each one of those function calls. Note that since a set isn't 
//! ordered, the values in the returned array will be in more or less 
//! random order. If you need to know which member value produced which 
//! result, you have to make that a part of what the filtering function 
//! returns. 
//! 
//! The filtering function @[f] is called with a single, mixed-type 
//! argument which is the member value to be mapped. 
array(mixed) map(function f) 
{ 
  array a = allocate(sizeof(set)); 
  int   i = 0; 
 
  foreach(indices(set), mixed item) 
    a[i++] = f(item); 
 
  return a; 
} 
 
 
//! Return a filtered version of the set, containing only those members 
//! for which the filtering function @[f] returned true. 
//! 
//! The filtering function is called with a single mixed-type argument 
//! which is the member value to be checked. 
this_program filter(function f) 
{ 
  ADT.Set result = ADT.Set(); 
 
  foreach(indices(set), mixed item) 
    if (f(item)) 
      result->add(item); 
 
  return result; 
} 
 
 
//! Destructively filter the set, i.e. remove every element for which 
//! the filtering function @[f] returns 0, and then return the set. 
//! 
//! The filtering function is called with a single mixed-type argument 
//! which is the member value to be checked. 
this_program filter_destructively(function f) 
{ 
  ADT.Set result = ADT.Set(); 
 
  foreach(indices(set), mixed item) 
    if (f(item)) 
      result->add(item); 
 
  return this; 
} 
 
 
///////////////// 
/// OPERATORS /// 
///////////////// 
 
//! Subset. A <= B returns true if all items in A are also present in B. 
int(0..1) subset(ADT.Set other) 
{ 
  foreach(indices(set), mixed item) 
    if (!other->contains(item)) 
      return 0; 
 
  return 1; 
} 
 
//! Superset. A >= B returns true if all items in B are also present in A. 
int(0..1) superset(ADT.Set other) 
{ 
  return other <= this; 
} 
 
 
//! Equality. A == B returns true if all items in A are present in B, 
//! and all items in B are present in A. Otherwise, it returns false. 
int(0..1) `==(ADT.Set other) 
{ 
  foreach(indices(set), mixed item) 
    if (!other->contains(item)) 
      return 0; 
 
  foreach(indices(other), mixed item) 
    if (!contains(item)) 
      return 0; 
 
  return 1; 
} 
 
 
//! True subset. A < B returns true if each item in A is also present 
//! in B, and B contains at least one item not present in A. 
int(0..1) `<(ADT.Set other) 
{ 
  if (sizeof(this) >= sizeof(other)) 
    return 0; 
 
  return subset(other); 
} 
 
 
//! True superset. A > B returns true if each item in B is also present 
//! in A, and A contains at least one item not present in B. 
int(0..1) `>(ADT.Set other) 
{ 
  if (sizeof(this) <= sizeof(other)) 
    return 0; 
 
  return superset(other); 
} 
 
//! Union. Returns a set containing all elements present in either 
//! or both of the operand sets. 
this_program `|(ADT.Set other) 
{ 
  ADT.Set result = ADT.Set(this); 
 
  foreach(indices(other), mixed item) 
    result->add(item); 
 
  return result; 
} 
 
mixed `+ = `|; // Addition on sets works the same as union on sets. 
 
 
//! Intersection. Returns a set containing those values that were 
//! present in both the operand sets. 
this_program `&(ADT.Set other) 
{ 
  return filter(lambda (mixed x) { return other->contains(x);}); 
} 
 
 
//! Difference. The expression 'A - B', where A and B are sets, returns 
//! all elements in A that are not also present in B. 
this_program `-(ADT.Set other) 
{ 
  return filter(lambda (mixed x) { return !other->contains(x);}); 
} 
 
 
//! Indexing a set with a value V gives 1 if V is a member of the 
//! set, otherwise 0. 
int(0..1) `[](mixed item) 
{ 
  return set[item]; 
} 
 
 
//! Setting an index V to 0 removes V from the set. Setting it to 
//! a non-0 value adds V as a member of the set. 
int `[]=(mixed item, int value) 
{ 
  if (value) 
    add(item); 
  else 
    remove(item); 
 
  return value; 
} 
 
 
//////////////// 
/// SPECIALS /// 
//////////////// 
 
//! In analogy with multisets, indices() of an ADT.Set givess an array 
//! containing all members of the set. 
array(mixed) _indices() 
{ 
  return indices(set); 
} 
 
 
//! In analogy with multisets, values() of an ADT.Set givess an array 
//! indicating the number of occurrences in the set for each position 
//! in the member array returned by indices(). (Most of the time, this 
//! is probably rather useless for sets, since the result is an array 
//! which just contain 1's, one for each member of the set. Still, 
//! this function is provided for consistency. 
array(mixed) _values() 
{ 
  return values(set); 
} 
 
 
//! An ADT.Set can be cast to an array or a multiset. 
protected mixed cast(string to) 
{ 
  switch(to) 
  { 
    case "array": 
      return indices(set); 
 
    case "multiset": 
      return copy_value(set); 
 
    default: 
      return UNDEFINED; 
  } 
} 
 
 
//! Number of items in the set. 
int _sizeof() 
{ 
  return sizeof(set); 
} 
 
 
//! Printable representation of the set. 
string _sprintf(int t) { 
  return t=='O' && sprintf("%O%O", this_program, cast("array")); 
} 
 
 
//! Create an ADT.Set, optionally initialized from another ADT.Set or 
//! a compatible type. If no initial data is given, the set will start 
//! out empty. 
void create(void|ADT.Set|array|multiset|mapping initial_data) 
{ 
  reset(); 
  if (arrayp(initial_data)) 
    add(@initial_data); 
  else if (initial_data) 
    add(@indices(initial_data)); 
}