| 1 |
// { dg-do run } |
| 2 |
// { dg-options "" } |
| 3 |
// { dg-skip-if "requires hosted libstdc++ for iostream" { ! hostedlib } } |
| 4 |
# 1 "SetLS.cc" |
| 5 |
// GROUPS passed templates nested-classes |
| 6 |
// |
| 7 |
// The SetLS template test |
| 8 |
// |
| 9 |
// Wendell Baker, Berkeley CAD Group, 1993 ([email protected]) |
| 10 |
// |
| 11 |
|
| 12 |
#pragma implementation "ListS.h" |
| 13 |
#pragma implementation "SetLS.h" |
| 14 |
|
| 15 |
#include <cstdlib> |
| 16 |
#include <iostream> |
| 17 |
using namespace std; |
| 18 |
|
| 19 |
# 1 "../../templates/SetLS.h" 1 |
| 20 |
// -*- C++ -*- |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
// |
| 25 |
// A Set Template - implemented with an ListS |
| 26 |
// |
| 27 |
// Wendell Baker, Berkeley CAD Group, 1993 ([email protected]) |
| 28 |
// |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#pragma interface |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
#define XTRUE true |
| 41 |
#define XFALSE false |
| 42 |
|
| 43 |
# 37 "../../templates/SetLS.h" |
| 44 |
|
| 45 |
|
| 46 |
# 1 "../../templates/ListS.h" 1 |
| 47 |
// -*- C++ -*- |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
// |
| 52 |
// A List Template - providing a singly linked capability |
| 53 |
// |
| 54 |
// Wendell Baker, Berkeley CAD Group, 1993 ([email protected]) |
| 55 |
// |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
#pragma interface |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
# 1 "/projects/gnu-cygnus/gnu-cygnus-14/mips/lib/gcc-lib/decstation/cygnus-reno-1/g++-include/bool.h" 1 3 |
| 69 |
// Defining XTRUE and XFALSE is usually a Bad Idea, |
| 70 |
// because you will probably be inconsistent with anyone |
| 71 |
// else who had the same clever idea. |
| 72 |
// Therefore: DON'T USE THIS FILE. |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
# 23 "../../templates/ListS.h" 2 |
| 83 |
|
| 84 |
# 37 "../../templates/ListS.h" |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
// g++ reno-1 is not yet capable of creating templates with nested |
| 89 |
// classes which instantiate the template arguments. |
| 90 |
template<class T> |
| 91 |
struct ListS_link { |
| 92 |
T item; |
| 93 |
ListS_link<T> *next; |
| 94 |
|
| 95 |
ListS_link(const T& i, ListS_link<T> *n = 0): item(i), next(n) |
| 96 |
{ } |
| 97 |
}; |
| 98 |
|
| 99 |
|
| 100 |
// |
| 101 |
// For now, errors are raised by ::abort() because exceptions |
| 102 |
// are not well implemented in cxx or at all in CC 3.0.1 |
| 103 |
// |
| 104 |
template<class T> |
| 105 |
class ListS { |
| 106 |
public: |
| 107 |
ListS(); |
| 108 |
ListS(const ListS<T>&); |
| 109 |
~ListS(); |
| 110 |
|
| 111 |
void operator=(const ListS<T>&); |
| 112 |
|
| 113 |
unsigned length() const |
| 114 |
{ return count; } |
| 115 |
|
| 116 |
void prepend(const T& item); |
| 117 |
void append(const T& item); |
| 118 |
void clear(); |
| 119 |
|
| 120 |
const T& head() const |
| 121 |
{ ensure_1(); |
| 122 |
return head_link->item; } |
| 123 |
T& head() |
| 124 |
{ ensure_1(); |
| 125 |
return head_link->item; } |
| 126 |
void head(T& fill) const |
| 127 |
{ ensure_1(); |
| 128 |
fill = head_link->item; } |
| 129 |
void remove_head() |
| 130 |
{ remove_head_filling(0); } |
| 131 |
void remove_head(T& fill) |
| 132 |
{ remove_head_filling(&fill); } |
| 133 |
|
| 134 |
const T& tail() const |
| 135 |
{ ensure_1(); |
| 136 |
return tail_link->item; } |
| 137 |
T& tail() |
| 138 |
{ ensure_1(); |
| 139 |
return tail_link->item; } |
| 140 |
void tail(T& fill) const |
| 141 |
{ ensure_1(); |
| 142 |
fill = tail_link->item; } |
| 143 |
|
| 144 |
class Vix { |
| 145 |
public: |
| 146 |
Vix(): owner(0), index(0) |
| 147 |
{ } |
| 148 |
|
| 149 |
// These are friend functions so that v == x is the same as x == v |
| 150 |
friend int operator==(void *v, const Vix& x) |
| 151 |
{ return v == x.index; } |
| 152 |
friend int operator==(const Vix& x, void *v) |
| 153 |
{ return v == x.index; } |
| 154 |
friend int operator!=(void *v, const Vix& x) |
| 155 |
{ return v != x.index; } |
| 156 |
friend int operator!=(const Vix& x, void *v) |
| 157 |
{ return v != x.index; } |
| 158 |
friend int operator==(const Vix& x1, const Vix& x2) |
| 159 |
{ return x1.owner == x2.owner && x1.index == x2.index; } |
| 160 |
friend int operator!=(const Vix& x1, const Vix& x2) |
| 161 |
{ return x1.owner != x2.owner || x1.index != x2.index; } |
| 162 |
private: |
| 163 |
friend class ListS<T>; |
| 164 |
|
| 165 |
|
| 166 |
Vix(const ListS<T> *o, ListS_link<T> *i): owner(o), index(i) |
| 167 |
{ } |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
const ListS<T> *owner; |
| 174 |
|
| 175 |
ListS_link<T> *index; |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
}; |
| 180 |
|
| 181 |
Vix first() const |
| 182 |
{ return Vix(this, head_link); } |
| 183 |
void next(Vix& x) const |
| 184 |
{ check(x); |
| 185 |
if (x.index != 0) |
| 186 |
x.index = x.index->next; } |
| 187 |
T& operator()(const Vix& x) |
| 188 |
{ check(x); |
| 189 |
return x.index->item; } |
| 190 |
const T& operator()(const Vix& x) const |
| 191 |
{ check(x); |
| 192 |
return x.index->item; } |
| 193 |
protected: |
| 194 |
# 154 "../../templates/ListS.h" |
| 195 |
|
| 196 |
|
| 197 |
unsigned count; |
| 198 |
|
| 199 |
ListS_link<T> *head_link; // 0 for a zero-length list |
| 200 |
ListS_link<T> *tail_link; // 0 for a zero-length list |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
private: |
| 207 |
// fill may be 0 (then don't fill) |
| 208 |
void remove_head_filling(T *fill); |
| 209 |
|
| 210 |
void ensure_1() const |
| 211 |
{ if (0 == head_link) |
| 212 |
::abort(); } |
| 213 |
void check(const Vix& x) const |
| 214 |
{ if (this != x.owner) |
| 215 |
::abort(); |
| 216 |
if (0 == x.index) |
| 217 |
::abort(); } |
| 218 |
}; |
| 219 |
|
| 220 |
template<class T> |
| 221 |
ListS<T>::ListS(): |
| 222 |
count(0), |
| 223 |
head_link(0), |
| 224 |
tail_link(0) |
| 225 |
{ } |
| 226 |
|
| 227 |
template<class T> |
| 228 |
ListS<T>::ListS(const ListS<T>& other): |
| 229 |
count(0), |
| 230 |
head_link(0), |
| 231 |
tail_link(0) |
| 232 |
{ |
| 233 |
for (Vix x=other.first(); 0 != x; other.next(x)) |
| 234 |
append(other(x)); |
| 235 |
} |
| 236 |
|
| 237 |
template<class T> |
| 238 |
ListS<T>::~ListS() |
| 239 |
{ |
| 240 |
clear(); |
| 241 |
} |
| 242 |
|
| 243 |
template<class T> |
| 244 |
void |
| 245 |
ListS<T>::operator=(const ListS<T>& other) |
| 246 |
{ |
| 247 |
clear(); |
| 248 |
for (Vix x=other.first(); 0 != x; other.next(x)) |
| 249 |
append(other(x)); |
| 250 |
} |
| 251 |
|
| 252 |
template<class T> |
| 253 |
void |
| 254 |
ListS<T>::prepend(const T& item) |
| 255 |
{ |
| 256 |
|
| 257 |
head_link = new ListS_link<T>(item, head_link); |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
if (0 == tail_link) |
| 262 |
tail_link = head_link; |
| 263 |
count++; |
| 264 |
} |
| 265 |
|
| 266 |
template<class T> |
| 267 |
void |
| 268 |
ListS<T>::append(const T& item) |
| 269 |
{ |
| 270 |
|
| 271 |
ListS_link<T> *new_link = new ListS_link<T>(item); |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
if (0 == tail_link) { |
| 276 |
head_link = new_link; |
| 277 |
tail_link = new_link; |
| 278 |
} else { |
| 279 |
tail_link->next = new_link; |
| 280 |
tail_link = tail_link->next; |
| 281 |
} |
| 282 |
count++; |
| 283 |
} |
| 284 |
|
| 285 |
template<class T> |
| 286 |
void |
| 287 |
ListS<T>::clear() |
| 288 |
{ |
| 289 |
|
| 290 |
ListS_link<T> *next, *l; |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
for (l=head_link; 0 != l; l=next) { |
| 295 |
next = l->next; |
| 296 |
delete l; |
| 297 |
} |
| 298 |
|
| 299 |
count = 0; |
| 300 |
head_link = 0; |
| 301 |
tail_link = 0; |
| 302 |
} |
| 303 |
|
| 304 |
template<class T> |
| 305 |
void |
| 306 |
ListS<T>::remove_head_filling(T* fill) |
| 307 |
// fill may be 0 in which case don't assign into it |
| 308 |
{ |
| 309 |
ensure_1(); |
| 310 |
|
| 311 |
ListS_link<T> *ohead = head_link; |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
if (0 != fill) |
| 316 |
*fill = ohead->item; |
| 317 |
head_link = ohead->next; |
| 318 |
if (0 == head_link) |
| 319 |
tail_link = 0; |
| 320 |
count--; |
| 321 |
delete ohead; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
# 40 "../../templates/SetLS.h" 2 |
| 326 |
|
| 327 |
|
| 328 |
# 62 "../../templates/SetLS.h" |
| 329 |
|
| 330 |
template<class T> |
| 331 |
class SetLS { |
| 332 |
public: |
| 333 |
SetLS(); |
| 334 |
|
| 335 |
void add(const T& item); |
| 336 |
// There is no remove(const T& item) for this set |
| 337 |
bool contains(const T& item) const; |
| 338 |
|
| 339 |
unsigned length() const |
| 340 |
{ return list.length(); } |
| 341 |
|
| 342 |
void clear() |
| 343 |
{ list.clear(); } |
| 344 |
|
| 345 |
class Vix { |
| 346 |
public: |
| 347 |
Vix(): owner(0), vix() |
| 348 |
{ } |
| 349 |
|
| 350 |
// These are friend functions so that v == x is the same as x == v |
| 351 |
friend int operator==(void *v, const Vix& x) |
| 352 |
{ return v == x.vix; } |
| 353 |
friend int operator==(const Vix& x, void *v) |
| 354 |
{ return v == x.vix; } |
| 355 |
friend int operator!=(void *v, const Vix& x) |
| 356 |
{ return v != x.vix; } |
| 357 |
friend int operator!=(const Vix& x, void *v) |
| 358 |
{ return v != x.vix; } |
| 359 |
friend int operator==(const Vix& x1, const Vix& x2) |
| 360 |
{ return x1.owner == x2.owner && x1.vix == x2.vix; } |
| 361 |
friend int operator!=(const Vix& x1, const Vix& x2) |
| 362 |
{ return x1.owner != x2.owner || x1.vix != x2.vix; } |
| 363 |
private: |
| 364 |
friend class SetLS<T>; |
| 365 |
|
| 366 |
Vix(const SetLS<T> *o, const typename ListS<T>::Vix& x): owner(o), vix(x) |
| 367 |
{ } |
| 368 |
|
| 369 |
const SetLS<T> *owner; |
| 370 |
typename ListS<T>::Vix vix; |
| 371 |
}; |
| 372 |
friend class Vix; |
| 373 |
|
| 374 |
Vix first() const |
| 375 |
{ return Vix(this, list.first()); } |
| 376 |
void next(Vix& x) const |
| 377 |
{ check(x); |
| 378 |
list.next(x.vix); } |
| 379 |
const T& operator()(const Vix& x) const |
| 380 |
{ check(x); |
| 381 |
return list(x.vix); } |
| 382 |
// There is item no remove(const Vix&) for this set |
| 383 |
protected: |
| 384 |
ListS<T> list; |
| 385 |
|
| 386 |
private: |
| 387 |
void check(const Vix& x) const |
| 388 |
{ if (this != x.owner) |
| 389 |
::abort(); } |
| 390 |
}; |
| 391 |
|
| 392 |
|
| 393 |
template<class T> |
| 394 |
SetLS<T>::SetLS(): |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
list() |
| 399 |
|
| 400 |
{ } |
| 401 |
|
| 402 |
template<class T> |
| 403 |
void |
| 404 |
SetLS<T>::add(const T& item) |
| 405 |
{ |
| 406 |
if ( ! contains(item) ) { |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
list.append(item); |
| 411 |
|
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
template<class T> |
| 416 |
bool |
| 417 |
SetLS<T>::contains(const T& item) const |
| 418 |
{ |
| 419 |
for (Vix x=first(); 0 != x; next(x)) { |
| 420 |
if (operator()(x) == item) |
| 421 |
return XTRUE; |
| 422 |
} |
| 423 |
return XFALSE; |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
# 17 "SetLS.cc" 2 |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
// In (most versions of) g++ 2.X, this use of typedefs has the effect |
| 432 |
// of causing the instantiation of the templates, thereby testing the |
| 433 |
// templates |
| 434 |
|
| 435 |
class test { |
| 436 |
public: |
| 437 |
test(): value(0) |
| 438 |
{ } |
| 439 |
test(int v): value(v) |
| 440 |
{ } |
| 441 |
|
| 442 |
void print(ostream& out) const |
| 443 |
{ out << value; } |
| 444 |
|
| 445 |
friend int operator==(const test& a, const test& b); |
| 446 |
private: |
| 447 |
int value; |
| 448 |
}; |
| 449 |
|
| 450 |
int |
| 451 |
operator==(const test& a, const test& b) |
| 452 |
{ |
| 453 |
return a.value == b.value; |
| 454 |
} |
| 455 |
|
| 456 |
ostream& |
| 457 |
operator<<(ostream& o, const test& t) |
| 458 |
{ |
| 459 |
t.print(o); |
| 460 |
return o; |
| 461 |
} |
| 462 |
|
| 463 |
typedef SetLS<test> SLS; |
| 464 |
|
| 465 |
static ostream& |
| 466 |
operator<<(ostream& o, const SLS& s) |
| 467 |
{ |
| 468 |
o << "set of " << s.length() << " = {"; |
| 469 |
|
| 470 |
bool first; |
| 471 |
SetLS<test>::Vix x; |
| 472 |
for (first=XTRUE, x=s.first(); 0 != x; s.next(x), first=XFALSE) { |
| 473 |
if ( ! first ) |
| 474 |
o << ','; |
| 475 |
o << ' '; |
| 476 |
s(x).print(o); |
| 477 |
} |
| 478 |
o << '}'; |
| 479 |
|
| 480 |
return o; |
| 481 |
} |
| 482 |
|
| 483 |
SLS gsls; |
| 484 |
const SLS gcsls; |
| 485 |
|
| 486 |
void foo() |
| 487 |
{ |
| 488 |
const unsigned SIZE = 20; |
| 489 |
|
| 490 |
// |
| 491 |
// SetLS() |
| 492 |
// SetLS(const SetLS<T>&) |
| 493 |
// |
| 494 |
SLS sls; |
| 495 |
{ |
| 496 |
// Fill sls with some interesting values |
| 497 |
for (unsigned i=0; i<SIZE; i++) { |
| 498 |
test t = i; |
| 499 |
sls.add(t); |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
const SLS csls(sls); |
| 504 |
|
| 505 |
// |
| 506 |
// void operator=(const SetLS<T>&); |
| 507 |
// |
| 508 |
sls = csls; |
| 509 |
|
| 510 |
// |
| 511 |
// bool contains(const T& item) const |
| 512 |
// |
| 513 |
for (unsigned i=0; i<SIZE; i++) { |
| 514 |
test t = i; |
| 515 |
|
| 516 |
int contains = sls.contains(t); |
| 517 |
} |
| 518 |
|
| 519 |
// |
| 520 |
// void clear() |
| 521 |
// |
| 522 |
sls.clear(); |
| 523 |
if (sls.length() != 0) |
| 524 |
::abort(); |
| 525 |
|
| 526 |
sls = csls; |
| 527 |
|
| 528 |
// |
| 529 |
// Vix first() const |
| 530 |
// void next(Vix& x) const |
| 531 |
// T& operator()(const Vix& x) |
| 532 |
// const T& operator()(const Vix& x) const |
| 533 |
// |
| 534 |
SetLS<test>::Vix cx; |
| 535 |
for (cx=csls.first(); 0 != cx; sls.next(cx)) { |
| 536 |
if ( ! sls.contains(csls(cx)) ) |
| 537 |
::abort(); |
| 538 |
} |
| 539 |
|
| 540 |
cout << "gsls:\t" << gsls << '\n'; |
| 541 |
cout << "gcsls:\t" << gcsls << '\n'; |
| 542 |
cout << "sls:\t" << sls << '\n'; |
| 543 |
cout << "csls:\t" << csls << '\n'; |
| 544 |
} |
| 545 |
|
| 546 |
// Dummy function so it'll run |
| 547 |
int main() |
| 548 |
{ |
| 549 |
cout << "PASS" << endl; |
| 550 |
} |
| 551 |
|
| 552 |
template class ListS<test>; |