blob: c3084c13c2e8fb5acafeb388183205d9960be22f (
plain)
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
/****************************************************************
* *
* Collection constructors, desctructors, assignments *
* *
****************************************************************/
#include <assert.h>
namespace omega {
template<class T> Bag<T>::Bag() {
contents = new List_Element <T>;
contents->tail = 0;
}
template<class T> Bag<T>::~Bag() {
delete contents;
}
template<class T> Ordered_Bag<T>::Ordered_Bag() {}
template<class T> Set<T>::Set() {}
template<class T> Bag<T>::Bag(const Bag<T> &L) {
contents = new List_Element<T>(*L.contents);
}
template<class T> Bag<T> & Bag<T>::operator=(const Bag<T> &L) {
if (this != &L) {
delete contents;
contents = new List_Element<T>(*L.contents);
}
return *this;
}
template<class T> Set<T>::Set(T e) {
assert(this->contents);
this->contents->tail = new List_Element<T>(e, 0);
}
/****************************************************************
* *
* Misc. simple Collection operations *
* *
****************************************************************/
template<class T> bool Bag<T>::empty() const {
return contents->tail == 0;
}
template<class T> Iterator<T> *Bag<T>::new_iterator()
{
return new List_Element_Iterator<T>(contents->tail);
}
template<class T> void Bag<T>::clear() {
if (contents->tail) delete contents->tail;
contents->tail = 0;
}
template<class T> int Bag<T>::size() const {
int i = 0;
List_Element<T> * p = contents->tail;
while (p) {
p = p->tail;
i++;
};
return i;
}
/****************************************************************
* *
* Collection/Element operations (e.g. insert, contains) *
* *
****************************************************************/
template<class T> void Bag<T>::remove(T e) {
List_Element<T> * p = contents;
while (p->tail && p->tail->head != e) p = p->tail;
if (p->tail && p->tail->head == e) {
List_Element<T> * q = p->tail;
p->tail = q->tail;
q->tail = 0;
delete q;
}
}
template<class T> T Bag<T>::extract() {
List_Element<T> * p = contents->tail;
T e = p->head;
contents->tail = p->tail;
p->tail = 0;
delete p;
return e;
}
template<class T> void Bag<T>::insert(T e) {
List_Element<T> * q = new List_Element<T>(e,contents->tail);
contents->tail = q;
}
template<class T> void Ordered_Bag<T>::insert(T e) {
List_Element<T> * p = this->contents;
while (p->tail && p->tail->head < e) p = p->tail;
if (!p->tail || p->tail->head != e) {
List_Element<T> * q = new List_Element<T>(e,p->tail);
p->tail = q;
}
}
template<class T> bool Bag<T>::contains(T e) const {
List_Element<T> * p = contents;
while (p->tail && p->tail->head != e) p = p->tail;
return (p->tail && p->tail->head == e);
}
template<class T> bool Ordered_Bag<T>::contains(T e) const {
List_Element<T> * p = this->contents;
while (p->tail && p->tail->head < e) p = p->tail;
return (p->tail && p->tail->head == e);
}
template<class T> bool Set<T>::contains (const Set<T>& b) const {
List_Element<T> * p = this->contents;
List_Element<T> * q = b.contents;
do {
/* consume matched elements in p and q */
p = p->tail;
q = q->tail;
if (!q) return 1; /* no more elements to match */
if (!p) return 0; /* nothing left in p to match with */
if (q->head < p->head) {
/* nothing smaller than
p->head left in p, so q->head
can't be matched */
return 0;
};
while (p && p->head < q->head) {
/* toss away some elements from p */
p = p->tail;
}
if (!p || q->head < p->head) return 0;
} while (q);
return 1;
}
/****************************************************************
* *
* Collection/Collection operations (e.g. |=) *
* *
****************************************************************/
template<class T> void Bag<T>::operator |= (const Bag<T> & b) {
assert(this != &b);
List_Element<T> * q = b.contents->tail;
while (q) {
List_Element<T> * r = new List_Element<T>(q->head,contents->tail);
contents->tail = r;
q = q->tail;
}
}
template<class T> void Ordered_Bag<T>::operator |= (const Ordered_Bag<T> & b) {
if (this == &b) return;
List_Element<T> * p = this->contents;
List_Element<T> * q = b.contents->tail;
while (q) {
while (p->tail && p->tail->head < q->head) p = p->tail;
List_Element<T> * r = new List_Element<T>(q->head,p->tail);
p->tail = r;
q = q->tail;
}
}
template<class T> void Ordered_Bag<T>::operator |= (const Bag<T> & b) {
Ordered_Bag<T> tmp;
for (List_Element<T> *p = b.contents; p; p=p->tail) {
tmp.insert(p->head);
}
*this |= tmp;
}
template<class T> void Set<T>::operator |= (const Set<T> & b) {
if (this == &b) return;
List_Element<T> * p = this->contents;
List_Element<T> * q = b.contents->tail;
while (q) {
while (p->tail && p->tail->head < q->head) p = p->tail;
if (!p->tail || p->tail->head != q->head) {
List_Element<T> * r = new List_Element<T>(q->head,p->tail);
p->tail = r;
}
q = q->tail;
}
}
template<class T> void Set<T>::operator |= (const Ordered_Bag<T> & b) {
Set<T> tmp;
for (List_Element<T> *p = b.contents; p; p=p->tail) {
tmp.insert(p->head);
}
*this |= tmp;
}
template<class T> void Set<T>::operator |= (const Bag<T> & b) {
Set<T> tmp;
for (List_Element<T> *p = b.contents; p; p=p->tail) {
tmp.insert(p->head);
}
*this |= tmp;
}
// delete items also in b
template<class T> void Set<T>::operator -= (const Set<T> & b) {
if (this == &b) {
this->clear();
return;
}
List_Element<T> * p = this->contents;
List_Element<T> * q = b.contents->tail;
while (q) {
while (p->tail && p->tail->head < q->head) p = p->tail;
if (p->tail && p->tail->head == q->head) {
List_Element<T> * r = p->tail;
p->tail = r->tail;
r->tail = 0;
delete r;
}
q = q->tail;
}
}
// delete items not in b
template<class T> void Set<T>::operator &= (const Set<T> & b)
{
if (this == &b) return;
List_Element<T> * p = this->contents;
List_Element<T> * q = b.contents->tail;
while (q) {
while (p->tail && p->tail->head < q->head) {
List_Element<T> * r = p->tail;
p->tail = r->tail;
r->tail = 0;
delete r;
};
if (p->tail && p->tail->head == q->head) {
/* allow p->tail->head into the result */
p = p->tail;
}
/* q->head has matched anything it is going to match */
q = q->tail;
}
if (p->tail) {
delete p->tail;
p->tail = 0;
};
}
template<class T> bool Set<T>::operator & (const Set<T>& b) const {
List_Element<T> * p = this->contents;
List_Element<T> * q = b.contents;
do {
p = p->tail;
q = q->tail;
while (p && q && p->head != q->head) {
while (p && p->head < q->head) p = p->tail;
while (p && q && q->head < p->head) q = q->tail;
};
if (p && q && p->head == q->head) return 1;
} while (p && q);
return 0;
}
template<class T> bool Ordered_Bag<T>::operator == (const Ordered_Bag<T>& b) const {
List_Element<T> * p = this->contents;
List_Element<T> * q = b.contents;
while (1) {
p = p->tail;
q = q->tail;
if (!p && !q) return 1;
if (!p || !q) return 0;
if (p->head != q->head) return 0;
};
}
template<class T> bool Ordered_Bag<T>::operator != (const Ordered_Bag<T>& b) const {
return !(*this == b);
}
template<class T> bool Ordered_Bag<T>::operator < (const Ordered_Bag<T>& b) const {
List_Element<T> * p = this->contents;
List_Element<T> * q = b.contents;
while (1) {
p = p->tail;
q = q->tail;
if (!p && !q) return 0;
if (!p) return 1;
if (!q) return 0;
if (p->head < q->head) return 1;
if (q->head < p->head) return 0;
};
return 1;
}
} // namespace
|