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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
|
/*
* $Revision: 2615 $
*
* last checkin:
* $Author: gutwenger $
* $Date: 2012-07-16 14:23:36 +0200 (Mo, 16. Jul 2012) $
***************************************************************/
/** \file
* \brief Declaration and implementation of singly linked lists
* (SListPure<E> and SList<E>) and iterators (SListConstIterator<E>
* and SListIterator<E>).
*
* \author Carsten Gutwenger
*
* \par License:
* This file is part of the Open Graph Drawing Framework (OGDF).
*
* \par
* Copyright (C)<br>
* See README.txt in the root directory of the OGDF installation for details.
*
* \par
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* Version 2 or 3 as published by the Free Software Foundation;
* see the file LICENSE.txt included in the packaging of this file
* for details.
*
* \par
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* \par
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* \see http://www.gnu.org/copyleft/gpl.html
***************************************************************/
#ifdef _MSC_VER
#pragma once
#endif
#ifndef OGDF_SLIST_H
#define OGDF_SLIST_H
#include "../internal/basic/list_templates.h"
namespace ogdf {
template<class E> class SListPure;
template<class E> class StackPure;
template<class E> class SListIterator;
template<class E> class SListConstIterator;
//! The parameterized class \a SListElement<E> represents the structure for elements of singly linked lists.
template<class E>
class SListElement {
friend class SListPure<E>;
friend class StackPure<E>;
friend class SListIterator<E>;
friend class SListConstIterator<E>;
SListElement<E> *m_next; //!< Pointer to successor element.
E m_x; //!< Stores the content.
//! Constructs an SListElement.
SListElement() : m_next(0) { }
//! Constructs an SListElement.
SListElement(const E &x) : m_next(0), m_x(x) { }
//! Constructs an SListElement.
SListElement(const E &x, SListElement<E> *next) :
m_next(next), m_x(x) { }
OGDF_NEW_DELETE
}; // class SListElement
//! The parameterized class \a SListIterator<E> encapsulates a pointer to an slist element.
/**
* It is used in order to iterate over singly linked lists,
* and to specify a position in a singly linked list. It is possible that
* an iterator encapsulates a null pointer.
*/
template<class E> class SListIterator {
SListElement<E> *m_pX; //!< Pointer to slist element.
friend class SListConstIterator<E>;
friend class SListPure<E>;
//! Conversion to pointer to slist element.
operator SListElement<E> *() { return m_pX; }
//! Conversion to pointer to slist element.
operator const SListElement<E> *() const { return m_pX; }
public:
//! Constructs an iterator pointing to no element.
SListIterator() : m_pX(0) { }
//! Constructs an iterator pointing to \a pX.
SListIterator(SListElement<E> *pX) : m_pX(pX) { }
//! Constructs an iterator that is a copy of \a it.
SListIterator(const SListIterator<E> &it) : m_pX(it.m_pX) { }
//! Returns true iff the iterator points to an element.
bool valid() const { return m_pX != 0; }
//! Equality operator.
bool operator==(const SListIterator<E> &it) const {
return m_pX == it.m_pX;
}
//! Inequality operator.
bool operator!=(const SListIterator<E> &it) const {
return m_pX != it.m_pX;
}
//! Returns successor iterator.
SListIterator<E> succ() const { return m_pX->m_next; }
//! Returns a reference to the element content.
E &operator*() const { return m_pX->m_x; }
//! Assignment operator.
SListIterator<E> &operator=(const SListIterator<E> &it) {
m_pX = it.m_pX;
return *this;
}
//! Increment operator (prefix).
SListIterator<E> &operator++() {
m_pX = m_pX->m_next;
return *this;
}
//! Increment operator (postfix).
SListIterator<E> operator++(int) {
SListIterator<E> it = *this;
m_pX = m_pX->m_next;
return it;
}
OGDF_NEW_DELETE
}; // class SListIterator
//! The parameterized class \a SListIterator<E> encapsulates a constant pointer to an slist element.
/**
* It is used in order to iterate over singly linked lists,
* and to specify a position in a singly linked list. It is possible that
* an iterator encapsulates a null pointer. In contrast to SListIterator,
* it is not possible to change the slist element pointed to.
*/
template<class E> class SListConstIterator {
const SListElement<E> *m_pX; //!< Pointer to slist element.
friend class SListPure<E>;
//! Conversion to pointer to slist element.
operator const SListElement<E> *() { return m_pX; }
public:
//! Constructs an iterator pointing to no element.
SListConstIterator() : m_pX(0) { }
//! Constructs an iterator pointing to \a pX.
SListConstIterator(const SListElement<E> *pX) : m_pX(pX) { }
//! Constructs an iterator that is a copy of \a it.
SListConstIterator(const SListIterator<E> &it) : m_pX((const SListElement<E> *)it) { }
//! Constructs an iterator that is a copy of \a it.
SListConstIterator(const SListConstIterator &it) : m_pX(it.m_pX) { }
//! Returns true iff the iterator points to an element.
bool valid() const { return m_pX != 0; }
//! Equality operator.
bool operator==(const SListConstIterator<E> &it) const {
return m_pX == it.m_pX;
}
//! Inequality operator.
bool operator!=(const SListConstIterator<E> &it) const {
return m_pX != it.m_pX;
}
//! Returns successor iterator.
SListConstIterator<E> succ() const { return m_pX->m_next; }
//! Returns a reference to the element content.
const E &operator*() const { return m_pX->m_x; }
//! Assignment operator.
SListConstIterator<E> &operator=(const SListConstIterator<E> &it) {
m_pX = it.m_pX;
return *this;
}
//! Increment operator (prefix).
SListConstIterator<E> &operator++() {
m_pX = m_pX->m_next;
return *this;
}
//! Increment operator (postfix).
SListConstIterator<E> operator++(int) {
SListConstIterator<E> it = *this;
m_pX = m_pX->m_next;
return it;
}
OGDF_NEW_DELETE
}; // class SListConstIterator
//! The parameterized class \a SListPure<E> represents singly linked lists with content type \a E.
/**
* Elements of the list are instances of type SListElement<E>.
* Use SListConstIterator<E> or SListIterator<E> in order to iterate over the list.
*
* In contrast to SList<E>, instances of \a SListPure<E> do not store the length of the list.
*
* @tparam E is the data type stored in list elements.
*/
template<class E> class SListPure {
SListElement<E> *m_head; //!< Pointer to first element.
SListElement<E> *m_tail; //!< Pointer to last element.
public:
//! Constructs an empty singly linked list.
SListPure() : m_head(0), m_tail(0) { }
//! Constructs a singly linked list that is a copy of \a L.
SListPure(const SListPure<E> &L) : m_head(0), m_tail(0) {
copy(L);
}
// destruction
~SListPure() { clear(); }
typedef E value_type;
typedef SListElement<E> element_type;
typedef SListConstIterator<E> const_iterator;
typedef SListIterator<E> iterator;
//! Returns true iff the list is empty.
bool empty() const { return m_head == 0; }
//! Returns the length of the list
/**
* Notice that this method requires to run through the whole list and takes linear running time!
*/
int size() const {
int count = 0;
for (SListElement<E> *pX = m_head; pX; pX = pX->m_next)
++count;
return count;
}
//! Returns an iterator to the first element of the list.
/**
* If the list is empty, a null pointer iterator is returned.
*/
SListConstIterator<E> begin() const { return m_head; }
//! Returns an iterator to the first element of the list.
/**
* If the list is empty, a null pointer iterator is returned.
*/
SListIterator<E> begin() { return m_head; }
//! Returns an iterator to one-past-last element of the list.
/**
* This is always a null pointer iterator.
*/
SListConstIterator<E> end() const { return SListConstIterator<E>(); }
//! Returns an iterator to one-past-last element of the list.
/**
* This is always a null pointer iterator.
*/
SListIterator<E> end() { return SListIterator<E>(); }
//! Returns an iterator to the last element of the list.
/**
* If the list is empty, a null pointer iterator is returned.
*/
SListConstIterator<E> rbegin() const { return m_tail; }
//! Returns an iterator to the last element of the list.
/**
* If the list is empty, a null pointer iterator is returned.
*/
SListIterator<E> rbegin() { return m_tail; }
//! Returns an iterator pointing to the element at position \a pos.
/**
* The running time of this method is linear in \a pos.
*/
SListConstIterator<E> get(int pos) const {
SListElement<E> *pX;
for(pX = m_head; pX != 0; pX = pX->m_next)
if (pos-- == 0) break;
return pX;
}
//! Returns an iterator pointing to the element at position \a pos.
/**
* The running time of this method is linear in \a pos.
*/
SListIterator<E> get(int pos) {
SListElement<E> *pX;
for(pX = m_head; pX != 0; pX = pX->m_next)
if (pos-- == 0) break;
return pX;
}
//! Returns the position (starting with 0) of \a it in the list.
/**
* Positions are numbered 0,1,...
* \pre \a it is an iterator pointing to an element in this list.
*/
int pos(SListConstIterator<E> it) const {
OGDF_ASSERT(it.valid())
int p = 0;
for(SListElement<E> *pX = m_head; pX != 0; pX = pX->m_next, ++p)
if (pX == it) break;
return p;
}
//! Returns a reference to the first element.
/**
* \pre The list is not empty!
*/
const E &front() const {
OGDF_ASSERT(m_head != 0)
return m_head->m_x;
}
//! Returns a reference to the first element.
/**
* \pre The list is not empty!
*/
E &front() {
OGDF_ASSERT(m_head != 0)
return m_head->m_x;
}
//! Returns a reference to the last element.
/**
* \pre The list is not empty!
*/
const E &back() const {
OGDF_ASSERT(m_tail != 0)
return m_tail->m_x;
}
//! Returns a reference to the last element.
/**
* \pre The list is not empty!
*/
E &back() {
OGDF_ASSERT(m_tail != 0)
return m_tail->m_x;
}
//! Returns an iterator to the cyclic successor of \a it.
/**
* \pre \a it points to an element in this list!
*/
SListConstIterator<E> cyclicSucc(SListConstIterator<E> it) const {
const SListElement<E> *pX = it;
return (pX->m_next) ? pX->m_next : m_head;
}
//! Returns an iterator to the cyclic successor of \a it.
/**
* \pre \a it points to an element in this list!
*/
SListIterator<E> cyclicSucc(SListIterator<E> it) {
SListElement<E> *pX = it;
return (pX->m_next) ? pX->m_next : m_head;
}
//! Assignment operator.
SListPure<E> &operator=(const SListPure<E> &L) {
clear(); copy(L);
return *this;
}
//! Adds element \a x at the begin of the list.
SListIterator<E> pushFront(const E &x) {
m_head = OGDF_NEW SListElement<E>(x,m_head);
if (m_tail == 0) m_tail = m_head;
return m_head;
}
//! Adds element \a x at the end of the list.
SListIterator<E> pushBack(const E &x) {
SListElement<E> *pNew = OGDF_NEW SListElement<E>(x);
if (m_head == 0)
m_head = m_tail = pNew;
else
m_tail = m_tail->m_next = pNew;
return m_tail;
}
//! Inserts element \a x after \a pBefore.
/**
* \pre \a pBefore points to an element in this list.
*/
SListIterator<E> insertAfter(const E &x, SListIterator<E> itBefore) {
SListElement<E> *pBefore = itBefore;
OGDF_ASSERT(pBefore != 0)
SListElement<E> *pNew = OGDF_NEW SListElement<E>(x,pBefore->m_next);
if (pBefore == m_tail) m_tail = pNew;
return (pBefore->m_next = pNew);
}
//! Removes the first element from the list.
/**
* \pre The list is not empty!
*/
void popFront() {
OGDF_ASSERT(m_head != 0)
SListElement<E> *pX = m_head;
if ((m_head = m_head->m_next) == 0) m_tail = 0;
delete pX;
}
//! Removes the first element from the list and returns it.
/**
* \pre The list is not empty!
*/
E popFrontRet() {
E el = front();
popFront();
return el;
}
//! Removes the succesor of \a pBefore.
/**
* \pre \a pBefore points to an element in this list.
*/
void delSucc(SListIterator<E> itBefore) {
SListElement<E> *pBefore = itBefore;
OGDF_ASSERT(pBefore != 0)
SListElement<E> *pDel = pBefore->m_next;
OGDF_ASSERT(pDel != 0)
if ((pBefore->m_next = pDel->m_next) == 0) m_tail = pBefore;
delete pDel;
}
//! Moves the first element of this list to the begin of list \a L2.
void moveFrontToFront(SListPure<E> &L2) {
OGDF_ASSERT(m_head != 0)
OGDF_ASSERT(this != &L2)
SListElement<E> *pX = m_head;
if ((m_head = m_head->m_next) == 0) m_tail = 0;
pX->m_next = L2.m_head;
L2.m_head = pX;
if (L2.m_tail == 0) L2.m_tail = L2.m_head;
}
//! Moves the first element of this list to the end of list \a L2.
void moveFrontToBack(SListPure<E> &L2) {
OGDF_ASSERT(m_head != 0)
OGDF_ASSERT(this != &L2)
SListElement<E> *pX = m_head;
if ((m_head = m_head->m_next) == 0) m_tail = 0;
pX->m_next = 0;
if (L2.m_head == 0)
L2.m_head = L2.m_tail = pX;
else
L2.m_tail = L2.m_tail->m_next = pX;
}
//! Moves the first element of this list to list \a L2 inserted after \a itBefore.
/**
* \pre \a itBefore points to an element in \a L2.
*/
void moveFrontToSucc(SListPure<E> &L2, SListIterator<E> itBefore) {
OGDF_ASSERT(m_head != 0)
OGDF_ASSERT(this != &L2)
SListElement<E> *pBefore = itBefore;
SListElement<E> *pX = m_head;
if ((m_head = m_head->m_next) == 0) m_tail = 0;
pX->m_next = pBefore->m_next;
pBefore->m_next = pX;
if (pBefore == L2.m_tail) L2.m_tail = pX;
}
//! Removes all elements from the list.
void clear() {
if (m_head == 0) return;
#if (_MSC_VER == 1100)
// workaround for bug in Visual Studio 5.0
while (!empty())
popFront();
#else
if (doDestruction((E*)0)) {
for(SListElement<E> *pX = m_head; pX != 0; pX = pX->m_next)
pX->m_x.~E();
}
OGDF_ALLOCATOR::deallocateList(sizeof(SListElement<E>),m_head,m_tail);
#endif
m_head = m_tail = 0;
}
//! Appends \a L2 to this list and makes \a L2 empty.
void conc(SListPure<E> &L2) {
if (m_head)
m_tail->m_next = L2.m_head;
else
m_head = L2.m_head;
if (L2.m_tail != 0) m_tail = L2.m_tail;
L2.m_head = L2.m_tail = 0;
}
//! Reverses the order of the list elements.
void reverse() {
SListElement<E> *p, *pNext, *pPred = 0;
for(p = m_head; p; p = pNext) {
pNext = p->m_next;
p->m_next = pPred;
pPred = p;
}
swap(m_head,m_tail);
}
//! Conversion to const SListPure.
const SListPure<E> &getListPure() const { return *this; }
//! Sorts the list using Quicksort.
void quicksort() {
ogdf::quicksortTemplate(*this);
}
//! Sorts the list using Quicksort and comparer \a comp.
template<class COMPARER>
void quicksort(const COMPARER &comp) {
ogdf::quicksortTemplate(*this,comp);
}
//! Sorts the list using bucket sort.
/**
* @param l is the lowest bucket that will occur.
* @param h is the highest bucket that will occur.
* @param f returns the bucket for each element.
* \pre The bucket function \a f will only return bucket values between \a l
* and \a h for this list.
*/
void bucketSort(int l, int h, BucketFunc<E> &f);
//! Sorts the list using bucket sort.
void bucketSort(BucketFunc<E> &f);
//! Randomly permutes the elements in the list.
void permute() {
permute(size());
}
//! Scans the list for the specified element and returns its position in the list, or -1 if not found.
int search (const E& e) const {
int x = 0;
for(SListConstIterator<E> i = begin(); i.valid(); ++i, ++x)
if(*i == e) return x;
return -1;
}
//! Scans the list for the specified element (using the user-defined comparer) and returns its position in the list, or -1 if not found.
template<class COMPARER>
int search (const E& e, const COMPARER &comp) const {
int x = 0;
for(SListConstIterator<E> i = begin(); i.valid(); ++i, ++x)
if(comp.equal(*i,e)) return x;
return -1;
}
protected:
void copy(const SListPure<E> &L) {
for(SListElement<E> *pX = L.m_head; pX != 0; pX = pX->m_next)
pushBack(pX->m_x);
}
void permute(const int n);
OGDF_NEW_DELETE
}; // class SListPure
//! The parameterized class \a SList<E> represents singly linked lists with content type \a E.
/**
* Elements of the list are instances of type SListElement<E>.
* Use SListConstIterator<E> or SListIterator<E> in order to iterate over the list.
* In contrast to SListPure<E>, instances of \a SList<E> store the length of the list
* and thus allow constant time access to the length.
*
* @tparam E is the data type stored in list elements.
*/
template<class E>
class SList : private SListPure<E> {
int m_count; //!< The length of the list.
public:
//! Constructs an empty singly linked list.
SList() : m_count(0) { }
//! Constructs a singly linked list that is a copy of \a L.
SList(const SList<E> &L) : SListPure<E>(L), m_count(L.m_count) { }
// destruction
~SList() { }
typedef E value_type;
typedef SListElement<E> element_type;
typedef SListConstIterator<E> const_iterator;
typedef SListIterator<E> iterator;
//! Returns true iff the list is empty.
bool empty() const { return SListPure<E>::empty(); }
//! Returns the length of the list.
int size() const { return m_count; }
//! Returns an iterator to the first element of the list.
/**
* If the list is empty, a null pointer iterator is returned.
*/
const SListConstIterator<E> begin() const { return SListPure<E>::begin(); }
//! Returns an iterator to the first element of the list.
/**
* If the list is empty, a null pointer iterator is returned.
*/
SListIterator<E> begin() { return SListPure<E>::begin(); }
//! Returns an iterator to one-past-last element of the list.
/**
* This is always a null pointer iterator.
*/
SListConstIterator<E> end() const { return SListConstIterator<E>(); }
//! Returns an iterator to one-past-last element of the list.
/**
* This is always a null pointer iterator.
*/
SListIterator<E> end() { return SListIterator<E>(); }
//! Returns an iterator to the last element of the list.
/**
* If the list is empty, a null pointer iterator is returned.
*/
const SListConstIterator<E> rbegin() const { return SListPure<E>::rbegin(); }
//! Returns an iterator to the last element of the list.
/**
* If the list is empty, a null pointer iterator is returned.
*/
SListIterator<E> rbegin() { return SListPure<E>::rbegin(); }
//! Returns an iterator pointing to the element at position \a pos.
/**
* The running time of this method is linear in \a pos.
*/
SListConstIterator<E> get(int pos) const {
return SListPure<E>::get(pos);
}
//! Returns an iterator pointing to the element at position \a pos.
/**
* The running time of this method is linear in \a pos.
*/
SListIterator<E> get(int pos) {
return SListPure<E>::get(pos);
}
//! Returns the position (starting with 0) of \a it in the list.
/**
* Positions are numbered 0,1,...
* \pre \a it is an iterator pointing to an element in this list.
*/
int pos(SListConstIterator<E> it) const {
return SListPure<E>::pos(it);;
}
//! Returns a reference to the first element.
/**
* \pre The list is not empty!
*/
const E &front() const { return SListPure<E>::front(); }
//! Returns a reference to the first element.
/**
* \pre The list is not empty!
*/
E &front() { return SListPure<E>::front(); }
//! Returns a reference to the last element.
/**
* \pre The list is not empty!
*/
const E &back() const { return SListPure<E>::back(); }
//! Returns a reference to the last element.
/**
* \pre The list is not empty!
*/
E &back() { return SListPure<E>::back(); }
//! Returns an iterator to the cyclic successor of \a it.
/**
* \pre \a it points to an element in this list!
*/
SListConstIterator<E> cyclicSucc(SListConstIterator<E> it) const {
return SListPure<E>::cyclicSucc(it);
}
//! Returns an iterator to the cyclic successor of \a it.
/**
* \pre \a it points to an element in this list!
*/
SListIterator<E> cyclicSucc(SListIterator<E> it) {
return SListPure<E>::cyclicSucc(it);
}
//! Assignment operator.
SList<E> &operator=(const SList<E> &L) {
SListPure<E>::operator=(L);
m_count = L.m_count;
return *this;
}
//! Adds element \a x at the begin of the list.
SListIterator<E> pushFront(const E &x) {
++m_count;
return SListPure<E>::pushFront(x);
}
//! Adds element \a x at the end of the list.
SListIterator<E> pushBack(const E &x) {
++m_count;
return SListPure<E>::pushBack(x);
}
//! Inserts element \a x after \a pBefore.
/**
* \pre \a pBefore points to an element in this list.
*/
SListIterator<E> insertAfter(const E &x, SListIterator<E> itBefore) {
++m_count;
return SListPure<E>::insertAfter(x, itBefore);
}
//! Removes the first element from the list.
/**
* \pre The list is not empty!
*/
void popFront() {
--m_count;
SListPure<E>::popFront();
}
//! Removes the first element from the list and returns it.
/**
* \pre The list is not empty!
*/
E popFrontRet() {
E el = front();
popFront();
return el;
}
//! Removes the succesor of \a pBefore.
/**
* \pre \a pBefore points to an element in this list.
*/
void delSucc(SListIterator<E> itBefore) {
--m_count;
SListPure<E>::delSucc(itBefore);
}
//! Moves the first element of this list to the begin of list \a L2.
void moveFrontToFront(SList<E> &L2) {
SListPure<E>::moveFrontToFront(L2);
--m_count; ++L2.m_count;
}
//! Moves the first element of this list to the end of list \a L2.
void moveFrontToBack(SList<E> &L2) {
SListPure<E>::moveFrontToBack(L2);
--m_count; ++L2.m_count;
}
//! Moves the first element of this list to list \a L2 inserted after \a itBefore.
/**
* \pre \a itBefore points to an element in \a L2.
*/
void moveFrontToSucc(SList<E> &L2, SListIterator<E> itBefore) {
SListPure<E>::moveFrontToSucc(L2,itBefore);
--m_count; ++L2.m_count;
}
//! Removes all elements from the list.
void clear() {
m_count = 0;
SListPure<E>::clear();
}
//! Appends \a L2 to this list and makes \a L2 empty.
void conc(SList<E> &L2) {
SListPure<E>::conc(L2);
m_count += L2.m_count;
L2.m_count = 0;
}
//! Reverses the order of the list elements.
void reverse() {
SListPure<E>::reverse();
}
//! Conversion to const SListPure.
const SListPure<E> &getListPure() const { return *this; }
//! Sorts the list using Quicksort.
void quicksort() {
ogdf::quicksortTemplate(*this);
}
//! Sorts the list using Quicksort and comparer \a comp.
template<class COMPARER>
void quicksort(const COMPARER &comp) {
ogdf::quicksortTemplate(*this,comp);
}
//! Sorts the list using bucket sort.
/**
* @param l is the lowest bucket that will occur.
* @param h is the highest bucket that will occur.
* @param f returns the bucket for each element.
* \pre The bucket function \a f will only return bucket values between \a l
* and \a h for this list.
*/
void bucketSort(int l, int h, BucketFunc<E> &f) {
SListPure<E>::bucketSort(l,h,f);
}
//! Sorts the list using bucket sort.
void bucketSort(BucketFunc<E> &f) {
SListPure<E>::bucketSort(f);
}
//! Randomly permutes the elements in the list.
void permute() {
SListPure<E>::permute(m_count);
}
//! Scans the list for the specified element and returns its position in the list, or -1 if not found.
int search (const E& e) const {
return SListPure<E>::search(e);
}
//! Scans the list for the specified element (using the user-defined comparer) and returns its position in the list, or -1 if not found.
template<class COMPARER>
int search (const E& e, const COMPARER &comp) const {
return SListPure<E>::search(e, comp);
}
OGDF_NEW_DELETE
}; // class SList
// sorts list L using bucket sort
// computes l and h value
template<class E>
void SListPure<E>::bucketSort(BucketFunc<E> &f)
{
// if less than two elements, nothing to do
if (m_head == m_tail) return;
int l, h;
l = h = f.getBucket(m_head->m_x);
SListElement<E> *pX;
for(pX = m_head->m_next; pX; pX = pX->m_next)
{
int i = f.getBucket(pX->m_x);
if (i < l) l = i;
if (i > h) h = i;
}
bucketSort(l,h,f);
}
// sorts list L using bucket sort
template<class E>
void SListPure<E>::bucketSort(int l, int h, BucketFunc<E> &f)
{
// if less than two elements, nothing to do
if (m_head == m_tail) return;
Array<SListElement<E> *> head(l,h,0), tail(l,h);
SListElement<E> *pX;
for (pX = m_head; pX; pX = pX->m_next) {
int i = f.getBucket(pX->m_x);
if (head[i])
tail[i] = (tail[i]->m_next = pX);
else
head[i] = tail[i] = pX;
}
SListElement<E> *pY = 0;
for (int i = l; i <= h; i++) {
pX = head[i];
if (pX) {
if (pY)
pY->m_next = pX;
else
m_head = pX;
pY = tail[i];
}
}
m_tail = pY;
pY->m_next = 0;
}
// permutes elements in list randomly; n is the length of the list
template<class E>
void SListPure<E>::permute(const int n)
{
Array<SListElement<E> *> A(n+1);
A[n] = 0;
int i = 0;
SListElement<E> *pX;
for (pX = m_head; pX; pX = pX->m_next)
A[i++] = pX;
A.permute(0,n-1);
for (i = 0; i < n; i++) {
A[i]->m_next = A[i+1];
}
m_head = A[0];
m_tail = A[n-1];
}
// prints list to output stream os using delimiter delim
template<class E>
void print(ostream &os, const SListPure<E> &L, char delim = ' ')
{
SListConstIterator<E> pX = L.begin();
if (pX.valid()) {
os << *pX;
for(++pX; pX.valid(); ++pX)
os << delim << *pX;
}
}
// prints list to output stream os using delimiter delim
template<class E>
void print(ostream &/*os*/, const SList<E> &L, char delim = ' ')
{
print(L.getListPure(), delim);
}
// output operator
template<class E>
ostream &operator<<(ostream &os, const SListPure<E> &L)
{
print(os,L);
return os;
}
template<class E>
ostream &operator<<(ostream &os, const SList<E> &L)
{
return operator<<(os,L.getListPure());
}
// sort array using bucket sort and bucket object f;
// the values of f must be in the interval [min,max]
template<class E>
void bucketSort(Array<E> &a, int min, int max, BucketFunc<E> &f)
{
if (a.low() >= a.high()) return;
Array<SListPure<E> > bucket(min,max);
int i;
for(i = a.low(); i <= a.high(); ++i)
bucket[f.getBucket(a[i])].pushBack(a[i]);
i = a.low();
for(int j = min; j <= max; ++j) {
SListConstIterator<E> it = bucket[j].begin();
for(; it.valid(); ++it)
a[i++] = *it;
}
}
} // namespace ogdf
#endif
|