[6ba162] | 1 | // list.cc |
---|
| 2 | |
---|
| 3 | // implementation of class list and class list iterator |
---|
| 4 | |
---|
| 5 | #ifndef LIST_CC |
---|
| 6 | #define LIST_CC |
---|
| 7 | |
---|
| 8 | #include "list.h" |
---|
| 9 | |
---|
| 10 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 11 | ///////////////////////// class list //////////////////////////////////////// |
---|
| 12 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 13 | |
---|
| 14 | /////////////////// constructors and destructor ///////////////////////////// |
---|
| 15 | |
---|
| 16 | list::list() |
---|
| 17 | { |
---|
| 18 | |
---|
| 19 | #ifdef DL_LIST |
---|
| 20 | |
---|
| 21 | element* begin_dummy=new element; |
---|
| 22 | |
---|
| 23 | #endif // DL_LIST |
---|
| 24 | |
---|
| 25 | element* end_dummy=new element; |
---|
| 26 | |
---|
| 27 | #ifdef DL_LIST |
---|
| 28 | |
---|
| 29 | begin_dummy->previous=NULL; |
---|
| 30 | begin_dummy->next=end_dummy; |
---|
| 31 | begin_dummy->entry=NULL; |
---|
| 32 | begin_dummy->done=FALSE; |
---|
| 33 | begin_dummy->head_reduced=FALSE; |
---|
| 34 | end_dummy->previous=begin_dummy; |
---|
| 35 | |
---|
| 36 | #endif // DL_LIST |
---|
| 37 | |
---|
| 38 | end_dummy->next=NULL; |
---|
| 39 | end_dummy->entry=NULL; |
---|
| 40 | end_dummy->done=TRUE; |
---|
| 41 | end_dummy->head_reduced=TRUE; |
---|
| 42 | |
---|
| 43 | #ifdef DL_LIST |
---|
| 44 | |
---|
| 45 | start=begin_dummy; |
---|
| 46 | |
---|
| 47 | #endif // DL_LIST |
---|
| 48 | |
---|
| 49 | #ifdef SL_LIST |
---|
| 50 | |
---|
| 51 | start=end_dummy; |
---|
| 52 | |
---|
| 53 | #endif // SL_LIST |
---|
| 54 | |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | // The dummy elements have the following functions: |
---|
| 58 | // - The end_dummy guarantees that the deletion method of the simply linked |
---|
| 59 | // list works: Deletion is done by copying the next element to the actual |
---|
| 60 | // position and then deleting the original, see below for an explaination. |
---|
| 61 | // This would cause problems when deleting the last element; then the |
---|
| 62 | // next-pointer of the preceeding element would reference freed memory |
---|
| 63 | // (it cannot be manipulated, is unknown). So the end_dummy as an |
---|
| 64 | // artificial last element avoids this problem. |
---|
| 65 | // - With the described deletion method for a simply linked list, the start |
---|
| 66 | // pointer of a list has never to be changed by the list_iterator class |
---|
| 67 | // (as long as the list is not empty; but with the end_dummy, the list |
---|
| 68 | // never becomes empty). So a list iterator must never know on which list |
---|
| 69 | // he operates on. |
---|
| 70 | // Doubly linked lists use another, more secure deletion method (which |
---|
| 71 | // really deletes the actual element). Therefore, the comfort of a start |
---|
| 72 | // pointer which has never to be manipulated by the iterators must be |
---|
| 73 | // reached in another way: This is the purpose of the begin_dummy, an |
---|
| 74 | // artificial first element which is referenced by the start pointer and |
---|
| 75 | // never changed. |
---|
| 76 | // - With the dummy elements, we do need to distinguish the cases of inserting |
---|
| 77 | // or deleting at the beginning or the end of a list. |
---|
| 78 | // - Furthermore, the end_dummy is a stopper for Buchberger's algorithm: |
---|
| 79 | // By setting all flags to TRUE, we can eliminate many calls of the |
---|
| 80 | // is_at_end()-function of the list iterator class. |
---|
| 81 | // The dummy elements should never be deleted (the dletion method is not |
---|
| 82 | // secure)! |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | list::list(const list& l) |
---|
| 88 | { |
---|
| 89 | |
---|
| 90 | #ifdef DL_LIST |
---|
| 91 | |
---|
| 92 | element* begin_dummy=new element; |
---|
| 93 | |
---|
| 94 | #endif // DL_LIST |
---|
| 95 | |
---|
| 96 | element* end_dummy=new element; |
---|
| 97 | |
---|
| 98 | #ifdef DL_LIST |
---|
| 99 | |
---|
| 100 | begin_dummy->previous=NULL; |
---|
| 101 | begin_dummy->next=end_dummy; |
---|
| 102 | begin_dummy->entry=NULL; |
---|
| 103 | begin_dummy->done=FALSE; |
---|
| 104 | begin_dummy->head_reduced=FALSE; |
---|
| 105 | |
---|
| 106 | end_dummy->previous=NULL; |
---|
| 107 | |
---|
| 108 | #endif // DL_LIST |
---|
| 109 | |
---|
| 110 | end_dummy->next=NULL; |
---|
| 111 | end_dummy->entry=NULL; |
---|
| 112 | end_dummy->done=TRUE; |
---|
| 113 | end_dummy->head_reduced=TRUE; |
---|
| 114 | |
---|
| 115 | #ifdef DL_LIST |
---|
| 116 | |
---|
| 117 | start=begin_dummy; |
---|
| 118 | element *iter=(l.start)->next; |
---|
| 119 | |
---|
| 120 | #endif // DL_LIST |
---|
| 121 | |
---|
| 122 | #ifdef SL_LIST |
---|
| 123 | |
---|
| 124 | element* iter=l.start; |
---|
| 125 | |
---|
| 126 | #endif // SL_LIST |
---|
| 127 | |
---|
| 128 | if(iter==NULL) |
---|
| 129 | { |
---|
| 130 | cerr<<"\nWARNING: list::list(const list&):\ntry to construct a list from" |
---|
| 131 | " a corrupt one; empty list created"<<endl; |
---|
| 132 | return; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | while((iter->next)!=NULL) |
---|
| 136 | // end_dummy not reached |
---|
| 137 | { |
---|
| 138 | copy_insert(*(iter->entry)); |
---|
| 139 | iter=iter->next; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | list::~list() |
---|
| 148 | { |
---|
| 149 | |
---|
| 150 | #ifdef DL_LIST |
---|
| 151 | |
---|
| 152 | element *iter=start->next; |
---|
| 153 | |
---|
| 154 | #endif // DL_LIST |
---|
| 155 | |
---|
| 156 | #ifdef SL_LIST |
---|
| 157 | |
---|
| 158 | element *iter=start; |
---|
| 159 | |
---|
| 160 | #endif // SL_LIST |
---|
| 161 | |
---|
| 162 | while(iter->next!=NULL) |
---|
| 163 | // delete non-dummy elements |
---|
| 164 | { |
---|
| 165 | element* aux=iter; |
---|
| 166 | iter=iter->next; |
---|
| 167 | delete aux->entry; |
---|
| 168 | delete aux; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | // end_dummy reached, delete it |
---|
| 172 | delete iter; |
---|
| 173 | |
---|
| 174 | #ifdef DL_LIST |
---|
| 175 | |
---|
| 176 | // delete begin_dummy |
---|
| 177 | delete start; |
---|
| 178 | |
---|
| 179 | #endif // DL_LIST |
---|
| 180 | |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | |
---|
| 184 | |
---|
| 185 | |
---|
| 186 | ///////////////////// inserting //////////////////////////////////////////// |
---|
| 187 | |
---|
| 188 | // For a better overview, the code for the simply and for the doubly linked |
---|
| 189 | // list is separated (except from the "copy-insert"-routines). |
---|
| 190 | |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | #ifdef DL_LIST |
---|
| 195 | |
---|
| 196 | |
---|
| 197 | |
---|
| 198 | |
---|
| 199 | list& list::insert(binomial& bin) |
---|
| 200 | { |
---|
| 201 | // insert at the beginning (after the begin_dummy) |
---|
| 202 | |
---|
| 203 | // initialize element |
---|
| 204 | element* aux=new element; |
---|
| 205 | aux->entry=&bin; |
---|
| 206 | aux->done=FALSE; |
---|
| 207 | aux->head_reduced=FALSE; |
---|
| 208 | |
---|
| 209 | // set pointers |
---|
| 210 | aux->next=start->next; |
---|
| 211 | aux->next->previous=aux; |
---|
| 212 | aux->previous=start; |
---|
| 213 | start->next=aux; |
---|
| 214 | |
---|
| 215 | return(*this); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | |
---|
| 219 | |
---|
| 220 | |
---|
| 221 | list& list::_insert(binomial& bin) |
---|
| 222 | { |
---|
| 223 | // insert at the beginning |
---|
| 224 | |
---|
| 225 | // initialize element |
---|
| 226 | element* aux=new element; |
---|
| 227 | aux->entry=&bin; |
---|
| 228 | |
---|
| 229 | // set pointers |
---|
| 230 | aux->next=start->next; |
---|
| 231 | aux->next->previous=aux; |
---|
| 232 | aux->previous=start; |
---|
| 233 | start->next=aux; |
---|
| 234 | |
---|
| 235 | return(*this); |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | |
---|
| 239 | |
---|
| 240 | |
---|
| 241 | list& list::ordered_insert(binomial& bin, const term_ordering& w) |
---|
| 242 | { |
---|
| 243 | |
---|
| 244 | // search for the place to insert |
---|
| 245 | element* iter=start->next; |
---|
| 246 | while(iter->entry!=NULL) |
---|
| 247 | // end_dummy not reached |
---|
| 248 | { |
---|
| 249 | if(w.compare(*(iter->entry),bin)<=0) |
---|
| 250 | // bin is bigger in term ordering then the referenced binomial |
---|
| 251 | iter=iter->next; |
---|
| 252 | else |
---|
| 253 | break; |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | // now bin is smaller in term ordering then the referenced binomial |
---|
| 257 | // or the referenced binomial is the end_dummy |
---|
| 258 | |
---|
| 259 | // initialize element |
---|
| 260 | element*aux=new element; |
---|
| 261 | aux->entry=&bin; |
---|
| 262 | aux->done=FALSE; |
---|
| 263 | aux->head_reduced=FALSE; |
---|
| 264 | |
---|
| 265 | // set pointers |
---|
| 266 | aux->previous=iter->previous; |
---|
| 267 | aux->previous->next=aux; |
---|
| 268 | aux->next=iter; |
---|
| 269 | iter->previous=aux; |
---|
| 270 | |
---|
| 271 | return *this; |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | |
---|
| 275 | |
---|
| 276 | |
---|
| 277 | list& list::_ordered_insert(binomial& bin, const term_ordering& w) |
---|
| 278 | { |
---|
| 279 | // search for the place to insert |
---|
| 280 | element* iter=start->next; |
---|
| 281 | while(iter->entry!=NULL) |
---|
| 282 | // end_dummy not reached |
---|
| 283 | { |
---|
| 284 | if(w.compare(*(iter->entry),bin)<=0) |
---|
| 285 | // bin is bigger in term ordering then the referenced binomial |
---|
| 286 | iter=iter->next; |
---|
| 287 | else |
---|
| 288 | break; |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | // now bin is smaller in term ordering then the referenced binomial |
---|
| 292 | // or the referenced binomial is the end_dummy |
---|
| 293 | |
---|
| 294 | // initialize element |
---|
| 295 | element*aux=new element; |
---|
| 296 | aux->entry=&bin; |
---|
| 297 | |
---|
| 298 | // set pointers |
---|
| 299 | aux->previous=iter->previous; |
---|
| 300 | aux->previous->next=aux; |
---|
| 301 | aux->next=iter; |
---|
| 302 | iter->previous=aux; |
---|
| 303 | |
---|
| 304 | return *this; |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | |
---|
| 308 | |
---|
| 309 | |
---|
| 310 | #endif // DL_LIST |
---|
| 311 | |
---|
| 312 | |
---|
| 313 | |
---|
| 314 | |
---|
| 315 | #ifdef SL_LIST |
---|
| 316 | |
---|
| 317 | |
---|
| 318 | |
---|
| 319 | |
---|
| 320 | list& list::insert(binomial& bin) |
---|
| 321 | { |
---|
| 322 | // insert at the beginning |
---|
| 323 | |
---|
| 324 | // initialize element |
---|
| 325 | element* aux=new element; |
---|
| 326 | aux->entry=&bin; |
---|
| 327 | aux->done=FALSE; |
---|
| 328 | aux->head_reduced=FALSE; |
---|
| 329 | |
---|
| 330 | // set pointers |
---|
| 331 | aux->next=start; |
---|
| 332 | start=aux; |
---|
| 333 | |
---|
| 334 | return(*this); |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | |
---|
| 338 | |
---|
| 339 | |
---|
| 340 | list& list::_insert(binomial& bin) |
---|
| 341 | { |
---|
| 342 | // insert at the beginning |
---|
| 343 | |
---|
| 344 | // initialize element |
---|
| 345 | element* aux=new element; |
---|
| 346 | aux->entry=&bin; |
---|
| 347 | |
---|
| 348 | // set pointers |
---|
| 349 | aux->next=start; |
---|
| 350 | start=aux; |
---|
| 351 | |
---|
| 352 | return(*this); |
---|
| 353 | } |
---|
| 354 | |
---|
| 355 | |
---|
| 356 | |
---|
| 357 | |
---|
| 358 | list& list::ordered_insert(binomial& bin, const term_ordering& w) |
---|
| 359 | { |
---|
| 360 | // search for the place to insert |
---|
| 361 | element* iter=start; |
---|
| 362 | while(iter->entry!=NULL) |
---|
| 363 | // end_dummy not reached |
---|
| 364 | { |
---|
| 365 | if(w.compare(*(iter->entry),bin)<=0) |
---|
| 366 | // bin is bigger in term ordering then the referenced binomial |
---|
| 367 | iter=iter->next; |
---|
| 368 | else |
---|
| 369 | break; |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | // now bin is smaller in term ordering then the referenced binomial |
---|
| 373 | // or the referenced binomial is the end_dummy |
---|
| 374 | |
---|
| 375 | // here we have to consider a special case |
---|
| 376 | if(iter==start) |
---|
| 377 | return insert(bin); |
---|
| 378 | |
---|
| 379 | // insert new element by first allocating a new list place behind the |
---|
| 380 | // referenced element, then moving the referenced element to that |
---|
| 381 | // new place... |
---|
| 382 | element*aux=new element; |
---|
| 383 | aux->entry=iter->entry; |
---|
| 384 | aux->done=iter->done; |
---|
| 385 | aux->head_reduced=iter->head_reduced; |
---|
| 386 | aux->next=iter->next; |
---|
| 387 | |
---|
| 388 | // .. and finally inserting bin at the old place |
---|
| 389 | // Remember that we cannot insert a new element between the referenced |
---|
| 390 | // element and its preceeding (we do not know the preceeding element) |
---|
| 391 | iter->entry=&bin; |
---|
| 392 | iter->done=FALSE; |
---|
| 393 | iter->head_reduced=FALSE; |
---|
| 394 | iter->next=aux; |
---|
| 395 | |
---|
| 396 | return *this; |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | |
---|
| 400 | |
---|
| 401 | |
---|
| 402 | list& list::_ordered_insert(binomial& bin, const term_ordering& w) |
---|
| 403 | { |
---|
| 404 | // search for the place to insert |
---|
| 405 | element* iter=start; |
---|
| 406 | while(iter->entry!=NULL) |
---|
| 407 | // end_dummy not reached |
---|
| 408 | { |
---|
| 409 | if(w.compare(*(iter->entry),bin)<=0) |
---|
| 410 | // bin is bigger in term ordering then the referenced binomial |
---|
| 411 | iter=iter->next; |
---|
| 412 | else |
---|
| 413 | break; |
---|
| 414 | } |
---|
| 415 | |
---|
| 416 | // now bin is smaller in term ordering then the referenced binomial |
---|
| 417 | // or the referenced binomial is the end_dummy |
---|
| 418 | |
---|
| 419 | // here we have to consider a special case |
---|
| 420 | if(iter==start) |
---|
| 421 | return _insert(bin); |
---|
| 422 | |
---|
| 423 | // insert new element by first allocating a new list place behind the |
---|
| 424 | // referenced element, then moving the referenced element to that |
---|
| 425 | // new place... |
---|
| 426 | element*aux=new element; |
---|
| 427 | aux->entry=iter->entry; |
---|
| 428 | aux->next=iter->next; |
---|
| 429 | |
---|
| 430 | // .. and finally inserting bin at the old place |
---|
| 431 | // Remember that we cannot insert a new element between the referenced |
---|
| 432 | // element and its preceeding (we do not know the preceeding element) |
---|
| 433 | iter->entry=&bin; |
---|
| 434 | iter->next=aux; |
---|
| 435 | |
---|
| 436 | return *this; |
---|
| 437 | } |
---|
| 438 | |
---|
| 439 | |
---|
| 440 | |
---|
| 441 | |
---|
| 442 | #endif // SL_LIST |
---|
| 443 | |
---|
| 444 | |
---|
| 445 | |
---|
| 446 | |
---|
| 447 | // copy-insert routines |
---|
| 448 | |
---|
| 449 | list& list::copy_insert(const binomial& bin) |
---|
| 450 | { |
---|
| 451 | binomial* _bin=new binomial(bin); |
---|
| 452 | return insert(*_bin); |
---|
| 453 | } |
---|
| 454 | |
---|
| 455 | |
---|
| 456 | list& list::_copy_insert(const binomial& bin) |
---|
| 457 | { |
---|
| 458 | binomial *_bin=new binomial(bin); |
---|
| 459 | return _insert(*_bin); |
---|
| 460 | } |
---|
| 461 | |
---|
| 462 | |
---|
| 463 | list& list::ordered_copy_insert(const binomial& bin, const term_ordering& w) |
---|
| 464 | { |
---|
| 465 | binomial *_bin=new binomial(bin); |
---|
| 466 | return ordered_insert(*_bin,w); |
---|
| 467 | } |
---|
| 468 | |
---|
| 469 | |
---|
| 470 | list& list::_ordered_copy_insert(const binomial& bin, const term_ordering& w) |
---|
| 471 | { |
---|
| 472 | binomial *_bin=new binomial(bin); |
---|
| 473 | return _ordered_insert(*_bin,w); |
---|
| 474 | } |
---|
| 475 | |
---|
| 476 | |
---|
| 477 | |
---|
| 478 | |
---|
| 479 | /////////////////////////// output ////////////////////////////////////////// |
---|
| 480 | |
---|
| 481 | |
---|
| 482 | |
---|
| 483 | |
---|
| 484 | void list::print() const |
---|
| 485 | { |
---|
| 486 | |
---|
| 487 | #ifdef DL_LIST |
---|
| 488 | |
---|
| 489 | element* iter=start->next; |
---|
| 490 | |
---|
| 491 | #endif // DL_LIST |
---|
| 492 | |
---|
| 493 | #ifdef SL_LIST |
---|
| 494 | |
---|
| 495 | element *iter=start; |
---|
| 496 | |
---|
| 497 | #endif // SL_LIST |
---|
| 498 | |
---|
| 499 | if(iter==NULL) |
---|
| 500 | { |
---|
| 501 | cerr<<"\nWARNING: void list::print() const:\ncannot print corrupt list" |
---|
| 502 | <<endl; |
---|
| 503 | return; |
---|
| 504 | } |
---|
| 505 | |
---|
| 506 | while(iter->next!=NULL) |
---|
| 507 | { |
---|
| 508 | iter->entry->print(); |
---|
| 509 | iter=iter->next; |
---|
| 510 | } |
---|
| 511 | } |
---|
| 512 | |
---|
| 513 | |
---|
| 514 | |
---|
| 515 | |
---|
| 516 | void list::ordered_print(const term_ordering& w) const |
---|
| 517 | { |
---|
| 518 | |
---|
| 519 | #ifdef DL_LIST |
---|
| 520 | |
---|
| 521 | element* iter=start->next; |
---|
| 522 | |
---|
| 523 | #endif // DL_LIST |
---|
| 524 | |
---|
| 525 | #ifdef SL_LIST |
---|
| 526 | |
---|
| 527 | element *iter=start; |
---|
| 528 | |
---|
| 529 | #endif // SL_LIST |
---|
| 530 | |
---|
| 531 | if(iter==NULL) |
---|
| 532 | { |
---|
| 533 | cerr<<"\nWARNING: void list::print(const term_ordering&) const:\n" |
---|
| 534 | "cannot print corrupt list"<<endl; |
---|
| 535 | return; |
---|
| 536 | } |
---|
| 537 | |
---|
| 538 | list aux; |
---|
| 539 | |
---|
| 540 | while(iter->next!=NULL) |
---|
| 541 | { |
---|
| 542 | aux._ordered_insert(*(iter->entry),w); |
---|
| 543 | iter=iter->next; |
---|
| 544 | } |
---|
| 545 | |
---|
| 546 | aux.print(); |
---|
| 547 | |
---|
| 548 | // delete aux, but only the element structs, not the binomials |
---|
| 549 | // (these are still stored in the actual list!) |
---|
| 550 | |
---|
| 551 | #ifdef DL_LIST |
---|
| 552 | |
---|
| 553 | iter=aux.start->next; |
---|
| 554 | |
---|
| 555 | #endif // DL_LIST |
---|
| 556 | |
---|
| 557 | #ifdef SL_LIST |
---|
| 558 | |
---|
| 559 | iter=aux.start; |
---|
| 560 | |
---|
| 561 | #endif |
---|
| 562 | |
---|
| 563 | while(iter->next!=NULL) |
---|
| 564 | // end_dummy not reached |
---|
| 565 | { |
---|
| 566 | element* aux2=iter->next; |
---|
| 567 | iter->next=iter->next->next; |
---|
| 568 | delete aux2; |
---|
| 569 | } |
---|
| 570 | |
---|
| 571 | // the dummy elements are deleted by the destructor |
---|
| 572 | |
---|
| 573 | } |
---|
| 574 | |
---|
| 575 | |
---|
| 576 | |
---|
| 577 | |
---|
| 578 | void list::print(FILE* output) const |
---|
| 579 | { |
---|
| 580 | |
---|
| 581 | #ifdef DL_LIST |
---|
| 582 | |
---|
| 583 | element* iter=start->next; |
---|
| 584 | |
---|
| 585 | #endif // DL_LIST |
---|
| 586 | |
---|
| 587 | #ifdef SL_LIST |
---|
| 588 | |
---|
| 589 | element *iter=start; |
---|
| 590 | |
---|
| 591 | #endif // SL_LIST |
---|
| 592 | |
---|
| 593 | if(iter==NULL) |
---|
| 594 | { |
---|
| 595 | cerr<<"\nWARNING: void list::print(FILE*) const:\ncannot print corrupt " |
---|
| 596 | "list"<<endl; |
---|
| 597 | fprintf(output,"\nWARNING: void list::print(FILE*) const:\ncannot print " |
---|
| 598 | "corrupt list\n"); |
---|
| 599 | return; |
---|
| 600 | } |
---|
| 601 | |
---|
| 602 | while(iter->next!=NULL) |
---|
| 603 | { |
---|
| 604 | iter->entry->print(output); |
---|
| 605 | iter=iter->next; |
---|
| 606 | } |
---|
| 607 | } |
---|
| 608 | |
---|
| 609 | |
---|
| 610 | |
---|
| 611 | |
---|
| 612 | void list::ordered_print(FILE* output, const term_ordering& w) const |
---|
| 613 | { |
---|
| 614 | |
---|
| 615 | #ifdef DL_LIST |
---|
| 616 | |
---|
| 617 | element* iter=start->next; |
---|
| 618 | |
---|
| 619 | #endif // DL_LIST |
---|
| 620 | |
---|
| 621 | #ifdef SL_LIST |
---|
| 622 | |
---|
| 623 | element *iter=start; |
---|
| 624 | |
---|
| 625 | #endif // SL_LIST |
---|
| 626 | |
---|
| 627 | if(iter==NULL) |
---|
| 628 | { |
---|
| 629 | cerr<<"\nWARNING: void list::print(const term_ordering&) const:\n" |
---|
| 630 | "cannot print corrupt list"<<endl; |
---|
| 631 | fprintf(output,"\nWARNING: void list::print(const term_ordering&) const:\n" |
---|
| 632 | "cannot print corrupt list\n"); |
---|
| 633 | return; |
---|
| 634 | } |
---|
| 635 | |
---|
| 636 | list aux; |
---|
| 637 | |
---|
| 638 | while(iter->next!=NULL) |
---|
| 639 | { |
---|
| 640 | aux._ordered_insert(*(iter->entry),w); |
---|
| 641 | iter=iter->next; |
---|
| 642 | } |
---|
| 643 | |
---|
| 644 | aux.print(output); |
---|
| 645 | |
---|
| 646 | // delete aux, but only the element structs, not the binomials |
---|
| 647 | // (these are still stored in the actual list!) |
---|
| 648 | |
---|
| 649 | #ifdef DL_LIST |
---|
| 650 | |
---|
| 651 | iter=aux.start->next; |
---|
| 652 | |
---|
| 653 | #endif // DL_LIST |
---|
| 654 | |
---|
| 655 | #ifdef SL_LIST |
---|
| 656 | |
---|
| 657 | iter=aux.start; |
---|
| 658 | |
---|
| 659 | #endif |
---|
| 660 | |
---|
| 661 | while(iter->next!=NULL) |
---|
| 662 | // end_dummy not reached |
---|
| 663 | { |
---|
| 664 | element* aux2=iter->next; |
---|
| 665 | iter->next=iter->next->next; |
---|
| 666 | delete aux2; |
---|
| 667 | } |
---|
| 668 | |
---|
| 669 | // the dummy elements are deleted by the destructor |
---|
| 670 | } |
---|
| 671 | |
---|
| 672 | |
---|
| 673 | |
---|
| 674 | |
---|
| 675 | void list::print(ofstream& output) const |
---|
| 676 | { |
---|
| 677 | |
---|
| 678 | #ifdef DL_LIST |
---|
| 679 | |
---|
| 680 | element* iter=start->next; |
---|
| 681 | |
---|
| 682 | #endif // DL_LIST |
---|
| 683 | |
---|
| 684 | #ifdef SL_LIST |
---|
| 685 | |
---|
| 686 | element *iter=start; |
---|
| 687 | |
---|
| 688 | #endif // SL_LIST |
---|
| 689 | |
---|
| 690 | if(iter==NULL) |
---|
| 691 | { |
---|
| 692 | cerr<<"\nWARNING: void list::print(ofstream&) const:\n" |
---|
| 693 | "cannot print corrupt list"<<endl; |
---|
| 694 | output<<"\nWARNING: void list::print(oftream&) const:\ncannot print " |
---|
| 695 | "corrupt list"<<endl; |
---|
| 696 | return; |
---|
| 697 | } |
---|
| 698 | |
---|
| 699 | while(iter->next!=NULL) |
---|
| 700 | { |
---|
| 701 | iter->entry->print(output); |
---|
| 702 | iter=iter->next; |
---|
| 703 | } |
---|
| 704 | } |
---|
| 705 | |
---|
| 706 | |
---|
| 707 | |
---|
| 708 | |
---|
| 709 | void list::ordered_print(ofstream& output, const term_ordering& w) const |
---|
| 710 | { |
---|
| 711 | |
---|
| 712 | #ifdef DL_LIST |
---|
| 713 | |
---|
| 714 | element* iter=start->next; |
---|
| 715 | |
---|
| 716 | #endif // DL_LIST |
---|
| 717 | |
---|
| 718 | #ifdef SL_LIST |
---|
| 719 | |
---|
| 720 | element *iter=start; |
---|
| 721 | |
---|
| 722 | #endif // SL_LIST |
---|
| 723 | |
---|
| 724 | if(iter==NULL) |
---|
| 725 | { |
---|
| 726 | cerr<<"\nWARNING: void list::ordered_print(const term_ordering&) const:\n" |
---|
| 727 | "cannot print corrupt list"<<endl; |
---|
| 728 | output<<"\nWARNING: void list::ordered_print(const term_ordering&) const:" |
---|
| 729 | "\n" |
---|
| 730 | "cannot print corrupt list\n"<<endl; |
---|
| 731 | return; |
---|
| 732 | } |
---|
| 733 | |
---|
| 734 | list aux; |
---|
| 735 | |
---|
| 736 | while(iter->next!=NULL) |
---|
| 737 | { |
---|
| 738 | aux._ordered_insert(*(iter->entry),w); |
---|
| 739 | iter=iter->next; |
---|
| 740 | } |
---|
| 741 | |
---|
| 742 | aux.print(output); |
---|
| 743 | |
---|
| 744 | // delete aux, but only the element structs, not the binomials |
---|
| 745 | // (these are still stored in the actual list!) |
---|
| 746 | |
---|
| 747 | #ifdef DL_LIST |
---|
| 748 | |
---|
| 749 | iter=(aux.start)->next; |
---|
| 750 | |
---|
| 751 | #endif // DL_LIST |
---|
| 752 | |
---|
| 753 | #ifdef SL_LIST |
---|
| 754 | |
---|
| 755 | |
---|
| 756 | iter=aux.start; |
---|
| 757 | |
---|
| 758 | #endif |
---|
| 759 | |
---|
| 760 | |
---|
| 761 | while((iter->next)!=NULL) |
---|
| 762 | { |
---|
| 763 | element* aux1=iter->next; |
---|
| 764 | iter->next=iter->next->next; |
---|
| 765 | delete aux1; |
---|
| 766 | } |
---|
| 767 | |
---|
| 768 | // the dummy elements are deleted by the destructor |
---|
| 769 | } |
---|
| 770 | |
---|
| 771 | |
---|
| 772 | |
---|
| 773 | |
---|
| 774 | void list::format_print(ofstream& output) const |
---|
| 775 | { |
---|
| 776 | |
---|
| 777 | #ifdef DL_LIST |
---|
| 778 | |
---|
| 779 | element* iter=start->next; |
---|
| 780 | |
---|
| 781 | #endif // DL_LIST |
---|
| 782 | |
---|
| 783 | #ifdef SL_LIST |
---|
| 784 | |
---|
| 785 | element *iter=start; |
---|
| 786 | |
---|
| 787 | #endif // SL_LIST |
---|
| 788 | |
---|
| 789 | if(iter==NULL) |
---|
| 790 | { |
---|
| 791 | cerr<<"\nWARNING: void list::print(ofstream&) const:\n" |
---|
| 792 | "cannot print corrupt list"<<endl; |
---|
| 793 | output<<"\nWARNING: void list::print(oftream&) const:\ncannot print " |
---|
| 794 | "corrupt list"<<endl; |
---|
| 795 | return; |
---|
| 796 | } |
---|
| 797 | |
---|
| 798 | while(iter->next!=NULL) |
---|
| 799 | { |
---|
| 800 | iter->entry->format_print(output); |
---|
| 801 | iter=iter->next; |
---|
| 802 | } |
---|
| 803 | } |
---|
| 804 | |
---|
| 805 | |
---|
| 806 | |
---|
| 807 | |
---|
| 808 | void list::ordered_format_print(ofstream& output, const term_ordering& w) const |
---|
| 809 | { |
---|
| 810 | |
---|
| 811 | #ifdef DL_LIST |
---|
| 812 | |
---|
| 813 | element* iter=start->next; |
---|
| 814 | |
---|
| 815 | #endif // DL_LIST |
---|
| 816 | |
---|
| 817 | #ifdef SL_LIST |
---|
| 818 | |
---|
| 819 | element *iter=start; |
---|
| 820 | |
---|
| 821 | #endif // SL_LIST |
---|
| 822 | |
---|
| 823 | if(iter==NULL) |
---|
| 824 | { |
---|
| 825 | cerr<<"\nWARNING: void list::ordered_format_print(const term_ordering&) " |
---|
| 826 | "const:\n" |
---|
| 827 | "cannot print corrupt list"<<endl; |
---|
| 828 | output<<"\nWARNING: void list::ordered_format_print(const term_ordering&) " |
---|
| 829 | "const:\n" |
---|
| 830 | "cannot print corrupt list\n"<<endl; |
---|
| 831 | return; |
---|
| 832 | } |
---|
| 833 | |
---|
| 834 | list aux; |
---|
| 835 | |
---|
| 836 | while(iter->next!=NULL) |
---|
| 837 | { |
---|
| 838 | aux._ordered_insert(*(iter->entry),w); |
---|
| 839 | iter=iter->next; |
---|
| 840 | } |
---|
| 841 | |
---|
| 842 | aux.format_print(output); |
---|
| 843 | |
---|
| 844 | // delete aux, but only the element structs, not the binomials |
---|
| 845 | // (these are still stored in the actual list!) |
---|
| 846 | |
---|
| 847 | #ifdef DL_LIST |
---|
| 848 | |
---|
| 849 | iter=(aux.start)->next; |
---|
| 850 | |
---|
| 851 | #endif // DL_LIST |
---|
| 852 | |
---|
| 853 | #ifdef SL_LIST |
---|
| 854 | |
---|
| 855 | |
---|
| 856 | iter=aux.start; |
---|
| 857 | |
---|
| 858 | #endif |
---|
| 859 | |
---|
| 860 | |
---|
| 861 | while((iter->next)!=NULL) |
---|
| 862 | { |
---|
| 863 | element* aux1=iter->next; |
---|
| 864 | iter->next=iter->next->next; |
---|
| 865 | delete aux1; |
---|
| 866 | } |
---|
| 867 | |
---|
| 868 | // the dummy elements are deleted by the destructor |
---|
| 869 | } |
---|
| 870 | |
---|
| 871 | |
---|
| 872 | |
---|
| 873 | |
---|
| 874 | |
---|
| 875 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 876 | ////////////////////// class list_iterator ////////////////////////////////// |
---|
| 877 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 878 | |
---|
| 879 | // implementation of class list_iterator |
---|
| 880 | // Most of these function can be inlined. I have tried this and not improved |
---|
| 881 | // the performance. Perhaps the compiler does this automatically... |
---|
| 882 | |
---|
| 883 | |
---|
| 884 | |
---|
| 885 | |
---|
| 886 | ///////////////////////// constructors and destructor /////////////////////// |
---|
| 887 | |
---|
| 888 | |
---|
| 889 | |
---|
| 890 | |
---|
| 891 | list_iterator::list_iterator() |
---|
| 892 | { |
---|
| 893 | actual=NULL; |
---|
| 894 | } |
---|
| 895 | |
---|
| 896 | |
---|
| 897 | |
---|
| 898 | list_iterator::list_iterator(list& l) |
---|
| 899 | { |
---|
| 900 | |
---|
| 901 | #ifdef DL_LIST |
---|
| 902 | |
---|
| 903 | actual=(l.start)->next; |
---|
| 904 | |
---|
| 905 | #endif // DL_LIST |
---|
| 906 | |
---|
| 907 | #ifdef SL_LIST |
---|
| 908 | |
---|
| 909 | actual=l.start; |
---|
| 910 | |
---|
| 911 | #endif // SL_LIST |
---|
| 912 | |
---|
| 913 | } |
---|
| 914 | |
---|
| 915 | |
---|
| 916 | |
---|
| 917 | list_iterator::list_iterator(list_iterator& iter) |
---|
| 918 | { |
---|
| 919 | actual=iter.actual; |
---|
| 920 | } |
---|
| 921 | |
---|
| 922 | |
---|
| 923 | |
---|
| 924 | list_iterator::~list_iterator() |
---|
| 925 | { |
---|
| 926 | } |
---|
| 927 | |
---|
| 928 | |
---|
| 929 | |
---|
| 930 | |
---|
| 931 | /////////////////// object information ///////////////////////////////////// |
---|
| 932 | |
---|
| 933 | |
---|
| 934 | |
---|
| 935 | |
---|
| 936 | BOOLEAN list_iterator::element_is_marked_done() const |
---|
| 937 | { |
---|
| 938 | return(actual->done); |
---|
| 939 | } |
---|
| 940 | |
---|
| 941 | |
---|
| 942 | BOOLEAN list_iterator::element_is_marked_head_reduced() const |
---|
| 943 | { |
---|
| 944 | return(actual->head_reduced); |
---|
| 945 | } |
---|
| 946 | |
---|
| 947 | |
---|
| 948 | BOOLEAN list_iterator::is_at_end() const |
---|
| 949 | { |
---|
| 950 | if(actual==NULL) |
---|
| 951 | // actual references no list |
---|
| 952 | return(TRUE); |
---|
| 953 | |
---|
| 954 | if(actual->next==NULL) |
---|
| 955 | // actual references dummy element |
---|
| 956 | return(TRUE); |
---|
| 957 | |
---|
| 958 | // actual references a real element |
---|
| 959 | return(FALSE); |
---|
| 960 | } |
---|
| 961 | |
---|
| 962 | |
---|
| 963 | |
---|
| 964 | |
---|
| 965 | ////////////////////////// assigment /////////////////////////////////////// |
---|
| 966 | |
---|
| 967 | |
---|
| 968 | |
---|
| 969 | |
---|
| 970 | list_iterator& list_iterator::set_to_list(const list& l) |
---|
| 971 | { |
---|
| 972 | |
---|
| 973 | #ifdef DL_LIST |
---|
| 974 | |
---|
| 975 | actual=(l.start)->next; |
---|
| 976 | |
---|
| 977 | #endif // DL_LIST |
---|
| 978 | |
---|
| 979 | #ifdef SL_LIST |
---|
| 980 | |
---|
| 981 | actual=l.start; |
---|
| 982 | |
---|
| 983 | #endif // SL_LIST |
---|
| 984 | |
---|
| 985 | return *this; |
---|
| 986 | } |
---|
| 987 | |
---|
| 988 | |
---|
| 989 | |
---|
| 990 | list_iterator& list_iterator::operator=(const list_iterator& iter) |
---|
| 991 | { |
---|
| 992 | if((&iter)!=this) |
---|
| 993 | actual=iter.actual; |
---|
| 994 | return *this; |
---|
| 995 | } |
---|
| 996 | |
---|
| 997 | |
---|
| 998 | |
---|
| 999 | list_iterator& list_iterator::next() |
---|
| 1000 | { |
---|
| 1001 | actual=actual->next; |
---|
| 1002 | return *this; |
---|
| 1003 | } |
---|
| 1004 | |
---|
| 1005 | |
---|
| 1006 | |
---|
| 1007 | |
---|
| 1008 | /////////////////// comparison //////////////////////////////////////////// |
---|
| 1009 | |
---|
| 1010 | |
---|
| 1011 | |
---|
| 1012 | |
---|
| 1013 | int list_iterator::operator==(const list_iterator& iter) const |
---|
| 1014 | { |
---|
| 1015 | return(actual==iter.actual); |
---|
| 1016 | } |
---|
| 1017 | |
---|
| 1018 | |
---|
| 1019 | int list_iterator::operator!=(const list_iterator& iter) const |
---|
| 1020 | { |
---|
| 1021 | return(!(actual==iter.actual)); |
---|
| 1022 | } |
---|
| 1023 | |
---|
| 1024 | |
---|
| 1025 | int list_iterator::next_is(const list_iterator& iter) const |
---|
| 1026 | { |
---|
| 1027 | return ((actual->next)==iter.actual); |
---|
| 1028 | } |
---|
| 1029 | |
---|
| 1030 | |
---|
| 1031 | |
---|
| 1032 | |
---|
| 1033 | ////////////// manipulation of list elements ////////////////////////////// |
---|
| 1034 | |
---|
| 1035 | // For a better overview, the code of the delete- and extract-routine is |
---|
| 1036 | // separated for simply and doubly linked lists. |
---|
| 1037 | |
---|
| 1038 | |
---|
| 1039 | |
---|
| 1040 | |
---|
| 1041 | binomial& list_iterator::get_element() |
---|
| 1042 | { |
---|
| 1043 | return(*(actual->entry)); |
---|
| 1044 | } |
---|
| 1045 | |
---|
| 1046 | |
---|
| 1047 | |
---|
| 1048 | |
---|
| 1049 | #ifdef DL_LIST |
---|
| 1050 | |
---|
| 1051 | |
---|
| 1052 | |
---|
| 1053 | |
---|
| 1054 | list_iterator& list_iterator::delete_element() |
---|
| 1055 | { |
---|
| 1056 | binomial* aux1=actual->entry; |
---|
| 1057 | element* aux2=actual; |
---|
| 1058 | |
---|
| 1059 | actual->previous->next=actual->next; |
---|
| 1060 | actual->next->previous=actual->previous; |
---|
| 1061 | |
---|
| 1062 | actual=actual->next; |
---|
| 1063 | |
---|
| 1064 | delete aux1; |
---|
| 1065 | delete aux2; |
---|
| 1066 | return *this; |
---|
| 1067 | } |
---|
| 1068 | |
---|
| 1069 | |
---|
| 1070 | |
---|
| 1071 | |
---|
| 1072 | list_iterator& list_iterator::extract_element() |
---|
| 1073 | { |
---|
| 1074 | element* aux=actual; |
---|
| 1075 | |
---|
| 1076 | actual->previous->next=actual->next; |
---|
| 1077 | actual->next->previous=actual->previous; |
---|
| 1078 | |
---|
| 1079 | actual=actual->next; |
---|
| 1080 | |
---|
| 1081 | delete aux; |
---|
| 1082 | return *this; |
---|
| 1083 | } |
---|
| 1084 | |
---|
| 1085 | |
---|
| 1086 | |
---|
| 1087 | |
---|
| 1088 | #endif // DL_LIST |
---|
| 1089 | |
---|
| 1090 | |
---|
| 1091 | |
---|
| 1092 | |
---|
| 1093 | #ifdef SL_LIST |
---|
| 1094 | |
---|
| 1095 | // When deleting or extracting an element of a simply linked list, the |
---|
| 1096 | // next-pointer of the previous element cannot be manipulated (is unkonwn!). |
---|
| 1097 | // So deletion must be done by copying the next element to the actual position |
---|
| 1098 | // and then deleting the original. Notice that only pointers are copies, never |
---|
| 1099 | // binomials. |
---|
| 1100 | |
---|
| 1101 | |
---|
| 1102 | |
---|
| 1103 | |
---|
| 1104 | list_iterator& list_iterator::delete_element() |
---|
| 1105 | { |
---|
| 1106 | binomial* aux1=actual->entry; |
---|
| 1107 | element* aux2=actual->next; |
---|
| 1108 | |
---|
| 1109 | actual->done=actual->next->done; |
---|
| 1110 | actual->head_reduced=actual->next->head_reduced; |
---|
| 1111 | actual->entry=actual->next->entry; |
---|
| 1112 | actual->next=actual->next->next; |
---|
| 1113 | |
---|
| 1114 | delete aux1; |
---|
| 1115 | delete aux2; |
---|
| 1116 | |
---|
| 1117 | return *this; |
---|
| 1118 | } |
---|
| 1119 | |
---|
| 1120 | |
---|
| 1121 | |
---|
| 1122 | |
---|
| 1123 | list_iterator& list_iterator::extract_element() |
---|
| 1124 | { |
---|
| 1125 | element* aux=actual->next; |
---|
| 1126 | |
---|
| 1127 | actual->done=actual->next->done; |
---|
| 1128 | actual->head_reduced=actual->next->head_reduced; |
---|
| 1129 | actual->entry=actual->next->entry; |
---|
| 1130 | actual->next=actual->next->next; |
---|
| 1131 | |
---|
| 1132 | delete aux; |
---|
| 1133 | return *this; |
---|
| 1134 | } |
---|
| 1135 | #endif // SL_LIST |
---|
| 1136 | |
---|
| 1137 | list_iterator& list_iterator::mark_element_done() |
---|
| 1138 | { |
---|
| 1139 | actual->done=TRUE; |
---|
[ce3dda] | 1140 | return *this; |
---|
[6ba162] | 1141 | } |
---|
| 1142 | |
---|
| 1143 | |
---|
| 1144 | |
---|
| 1145 | list_iterator& list_iterator::mark_element_undone() |
---|
| 1146 | { |
---|
| 1147 | actual->done=FALSE; |
---|
| 1148 | return *this; |
---|
| 1149 | } |
---|
| 1150 | |
---|
| 1151 | |
---|
| 1152 | |
---|
| 1153 | list_iterator& list_iterator::mark_element_head_reduced() |
---|
| 1154 | { |
---|
| 1155 | actual->head_reduced=TRUE; |
---|
| 1156 | return *this; |
---|
| 1157 | } |
---|
| 1158 | |
---|
| 1159 | |
---|
| 1160 | |
---|
| 1161 | list_iterator& list_iterator::mark_element_head_unreduced() |
---|
| 1162 | { |
---|
| 1163 | actual->head_reduced=FALSE; |
---|
| 1164 | return *this; |
---|
| 1165 | } |
---|
| 1166 | #endif // LIST_CC |
---|