39 template<
class E>
class List;
62 : m_next(next), m_prev(prev), m_x(x) {
70 template<
class ... Args>
72 :
ListElement(list, E(std::forward<Args>(args)...), next, prev) { }
98 operator ListElem *() {
return m_pX; }
112 bool valid()
const {
return m_pX !=
nullptr; }
124 return m_pX == it.
m_pX;
129 return m_pX != it.
m_pX;
207 ListPure(std::initializer_list<E> init) : m_head(nullptr), m_tail(nullptr) {
208 for (
const E &x : init)
222 L.m_head = L.m_tail =
nullptr;
236 bool empty()
const {
return m_head ==
nullptr; }
292 for(pX = m_head; pX !=
nullptr; pX = pX->
m_next)
293 if (pos-- == 0)
break;
303 for(pX = m_head; pX !=
nullptr; pX = pX->
m_next)
304 if (pos-- == 0)
break;
407 return (pX && pX->m_next) ? pX->m_next : m_head;
417 return (pX && pX->m_next) ? pX->m_next : m_head;
427 return (pX && pX->m_prev) ? pX->m_prev : m_tail;
437 return (pX && pX->m_prev) ? pX->m_prev : m_tail;
462 L.m_head = L.m_tail =
nullptr;
470 while(pX !=
nullptr && pY !=
nullptr) {
471 if(pX->
m_x != pY->m_x)
476 return (pX ==
nullptr && pY ==
nullptr);
495 m_head = m_head->
m_prev = pX;
497 m_head = m_tail = pX;
505 template<
class ... Args>
509 m_head = m_head->
m_prev = pX;
511 m_head = m_tail = pX;
519 m_tail = m_tail->
m_next = pX;
521 m_tail = m_head = pX;
529 template<
class ... Args>
533 m_tail = m_tail->
m_next = pX;
535 m_tail = m_head = pX;
553 if (pYnext) pYnext->
m_prev = pX;
558 if (pYprev) pYprev->
m_next = pX;
573 if (pYprev) pYprev->
m_next = pX;
587 if (pYnext) pYnext->
m_prev = pX;
608 if (m_head) m_head->
m_prev =
nullptr;
609 else m_tail =
nullptr;
631 if (m_tail) m_tail->
m_next =
nullptr;
632 else m_head =
nullptr;
651 ListElement<E> *pX = it, *pPrev = pX->m_prev, *pNext = pX->m_next;
653 if (pPrev) pPrev->m_next = pNext;
655 if (pNext) pNext->
m_prev = pPrev;
669 del(pX);
return true;
676 if (m_head ==
nullptr)
return;
678 if (!std::is_trivially_destructible<E>::value) {
682 OGDF_ALLOCATOR::deallocateList(
sizeof(
ListElement<E>),m_head,m_tail);
684 m_head = m_tail =
nullptr;
704 std::swap(pX->
m_next,pY->m_next);
705 std::swap(pX->
m_prev,pY->m_prev);
707 std::swap(pX->m_list,pY->m_list);
711 pX->
m_next = pY; pY->m_prev = pX;
714 pX->
m_prev = pY; pY->m_next = pX;
720 if(pY->m_prev) pY->
m_prev->m_next = pY;
726 if(pY->m_next) pY->
m_next->m_prev = pY;
737 ListElement<E> *pX = it, *pPrev = pX->m_prev, *pNext = pX->m_next;
742 if (pPrev) pPrev->m_next = pNext;
743 if (pNext) pNext->m_prev = pPrev;
748 m_head = m_head->
m_prev = pX;
758 ListElement<E> *pX = it, *pPrev = pX->m_prev, *pNext = pX->m_next;
763 if (pPrev) pPrev->m_next = pNext;
765 if (pNext) pNext->
m_prev = pPrev;
769 m_tail = m_tail->
m_next = pX;
780 ListElement<E> *pX = it, *pPrev = pX->m_prev, *pNext = pX->m_next;
783 if(pX == pY || pPrev == pY)
return;
786 if (pPrev) pPrev->m_next = pNext;
788 if (pNext) pNext->
m_prev = pPrev;
792 (pX->m_prev = pY)->
m_next = pX;
793 if (pYnext) pYnext->
m_prev = pX;
805 ListElement<E> *pX = it, *pPrev = pX->m_prev, *pNext = pX->m_next;
808 if(pX == pY || pNext == pY)
return;
811 if (pPrev) pPrev->m_next = pNext;
813 if (pNext) pNext->
m_prev = pPrev;
817 (pX->m_next = pY)->
m_prev = pX;
818 if (pYprev) pYprev->
m_next = pX;
831 if (pPrev) pPrev->m_next = pNext;
833 if (pNext) pNext->
m_prev = pPrev;
856 if (pPrev) pPrev->m_next = pNext;
858 if (pNext) pNext->
m_prev = pPrev;
883 if (pPrev) pPrev->m_next = pNext;
885 if (pNext) pNext->
m_prev = pPrev;
891 if (pYnext) pYnext->
m_prev = pX;
910 if (pPrev) pPrev->m_next = pNext;
912 if (pNext) pNext->
m_prev = pPrev;
918 if (pYprev) pYprev->
m_next = pX;
934 L2.
m_head->m_prev = m_tail;
938 reassignListRefs(L2.
m_head);
951 L2.
m_tail->m_next = m_head;
955 reassignListRefs(L2.
m_head);
962 std::swap(m_head, other.
m_head);
963 std::swap(m_tail, other.
m_tail);
981 if (&L1 !=
this) L1.
clear();
982 if (&L2 !=
this) L2.
clear();
1003 if (
this != &L1 &&
this != &L2) {
1004 m_head = m_tail =
nullptr;
1034 if ((m_tail = pX->
m_prev) ==
nullptr)
1037 m_tail->
m_next =
nullptr;
1065 for (i = begin(); i.
valid(); ++i)
1066 if (*i == e)
return i;
1073 for (i = begin(); i.
valid(); ++i)
1074 if (*i == e)
return i;
1079 template<
class COMPARER>
1082 for (i = begin(); i.
valid(); ++i)
1083 if (comp.equal(*i, e))
return i;
1088 template<
class COMPARER>
1091 for (i = begin(); i.
valid(); ++i)
1092 if (comp.equal(*i, e))
return i;
1102 template<
class COMPARER>
1133 std::function<
bool(
const E&)> includeElement = [](
const E&) {
return true; },
1134 bool isFastTest =
true)
const {
1140 std::function<
bool(
const E&)> includeElement = [](
const E&) {
return true; },
1141 bool isFastTest =
true) {
1154 std::function<
bool(
const E&)> includeElement = [](
const E&) {
return true; },
1155 bool isFastTest =
true)
const {
1156 const_iterator result = chooseIterator(includeElement, isFastTest);
1163 std::function<
bool(
const E&)> includeElement = [](
const E&) {
return true; },
1164 bool isFastTest =
true) {
1165 iterator result = chooseIterator(includeElement, isFastTest);
1173 permute(size(), rng);
1179 permute(size(), rng);
1193 void permute(
const int n, RNG &rng);
1198 for(
auto e = start ==
nullptr ? m_head : start; e !=
nullptr; e = e->
m_next) {
1232 List(std::initializer_list<E> init) :
ListPure<E>(init), m_count((int)init.size()) { }
1255 int size()
const {
return m_count; }
1309 template<
class ... Args>
1322 template<
class ... Args>
1447 std::swap(m_count, other.
m_count);
1453 int countL = m_count, countL1 = 0;
1458 L2.
m_count = countL - countL1;
1459 if (this->m_head ==
nullptr) m_count = 0;
1496 if (m_head == m_tail)
return;
1501 for (pX = m_head; pX; pX = pX->
m_next) {
1506 head[i] = tail[i] = pX;
1510 for (
int i = l; i <= h; i++) {
1516 (m_head = pX)->
m_prev =
nullptr;
1530 if (n == 0) {
return; }
1533 A[0] = A[n+1] =
nullptr;
1537 for (pX = m_head; pX; pX = pX->
m_next)
1542 for (i = 1; i <= n; i++) {
1559 for(++pX; pX.valid(); ++pX)
1573 ostream &operator<<(ostream &os, const ListPure<E> &L)
1581 ostream &operator<<(ostream &os, const List<E> &L)
1583 return os << L.getListPure();
1586 template<
class E,
class Master>
void concFront(ListPure< E > &L2)
Prepends L2 to this list and makes L2 empty.
void clear()
Removes all elements from the list.
const_iterator cend() const
Returns a const iterator to one-past-last element of the list.
E & reference
Provides a reference to an element stored in a list.
ListConstIterator< E > search(const E &e, const COMPARER &comp) const
Scans the list for the specified element (using the user-defined comparer) and returns an iterator to...
ListElement< E > * m_tail
Pointer to last element.
bool operator!=(const ListIteratorBase< E, isConst > &it) const
Inequality operator.
void moveToBack(iterator it)
Moves it to the end of the list.
iterator pushFront(const E &x)
Adds element x at the beginning of the list.
ListIteratorBase()
Constructs an invalid iterator.
ListPure(ListPure< E > &&L)
Constructs a doubly linked list containing the elements of L (move semantics).
The parameterized class Array implements dynamic arrays of type E.
ListElement< E > * m_prev
Pointer to predecessor element.
bool operator==(const ListPure< E > &L) const
Equality operator.
ListIteratorBase< E, isConst > operator--(int)
Decrement operator (postfix).
void quicksortTemplate(LIST &L)
iterator pushBack(const E &x)
Adds element x at the end of the list.
void moveToPrec(iterator it, List< E > &L2, iterator itAfter)
Moves it before itAfter.
iterator cyclicSucc(iterator it)
Returns an iterator to the cyclic successor of it.
ListIteratorBase(ListElem *pX)
Constructs an iterator that points to pX.
void conc(List< E > &L2)
Appends L2 to this list and makes L2 empty.
bool removeFirst(const E &x)
Removes the first occurrence of x (if any) from the list.
void splitAfter(iterator it, ListPure< E > &L2)
Splits the list after it.
iterator pushBack(const E &x)
Adds element x at the end of the list.
iterator emplaceBack(Args &&... args)
Adds a new element at the end of the list.
void popFront()
Removes the first element from the list.
const E & const_reference
Provides a reference to a const element stored in a list for reading and performing const operations...
void split(iterator it, ListPure< E > &L1, ListPure< E > &L2, Direction dir=Direction::before)
Splits the list at element it into lists L1 and L2.
iterator begin()
Returns an iterator to the first element of the list.
iterator begin() const
Returns an iterator to the first element in the container.
void split(iterator it, List< E > &L1, List< E > &L2, Direction dir=Direction::before)
Splits the list at element it into lists L1 and L2.
iterator insertAfter(const E &x, iterator it)
Inserts element x after it.
List< E > & operator=(List< E > &&L)
Assignment operator (move semantics).
const_iterator cyclicSucc(const_iterator it) const
Returns a const iterator to the cyclic successor of it.
ListIteratorBase< E, isConst > operator++(int)
Increment operator (postfix).
E popBackRet()
Removes the last element from the list and returns it.
void moveToFront(iterator it)
Moves it to the begin of the list.
iterator chooseIterator(std::function< bool(const E &)> includeElement=[](const E &) { return true;}, bool isFastTest=true)
Returns an iterator to a random element.
void moveToSucc(iterator it, List< E > &L2, iterator itBefore)
Moves it after itBefore.
CONTAINER::iterator chooseIteratorFrom(CONTAINER &container, std::function< bool(const TYPE &)> includeElement=[](const TYPE &) { return true;}, bool isFastTest=true)
Returns an iterator to a random element in the container.
void conc(ListPure< E > &L2)
Appends L2 to this list and makes L2 empty.
void moveToSucc(iterator it, ListPure< E > &L2, iterator itBefore)
Moves it to list L2 and inserts it after itBefore.
const_iterator cyclicPred(const_iterator it) const
Returns a const iterator to the cyclic predecessor of it.
ListIteratorBase< E, isConst > pred() const
Returns predecessor iterator.
virtual int size() const
Returns the number of elements in the list.
ListIterator< E > search(const E &e)
Scans the list for the specified element and returns an iterator to the first occurrence in the list...
ListElement(ListPure< E > *list, const E &x)
Constructs a ListElement.
bool operator==(const List< E > &L) const
Equality operator.
void permute(INDEX l, INDEX r)
Randomly permutes the subarray with index set [l..r].
iterator cyclicPred(iterator it)
Returns an iterator to the cyclic predecessor of it.
void del(iterator it)
Removes it from the list.
void popBack()
Removes the last element from the list.
ListIteratorBase< E, isConst > succ() const
Returns successor iterator.
void bucketSort(int l, int h, BucketFunc< E > &f)
Sorts the list using bucket sort.
List(std::initializer_list< E > init)
Constructs a doubly linked list containing the elements in init.
ListElem * m_pX
pointer to list element
void del(iterator it)
Removes it from the list.
ListIteratorBase< E, isConst > & operator=(const ListIteratorBase< E, isConst > &it)
Assignment operator.
ListPure(std::initializer_list< E > init)
Constructs a doubly linked list containing the elements in init.
const_reference chooseElement(std::function< bool(const E &)> includeElement=[](const E &) { return true;}, bool isFastTest=true) const
Returns a random element.
void moveToBack(iterator it, List< E > &L2)
Moves it to the end of the list.
long unsigned int randomSeed()
Returns a random value suitable as initial seed for a random number engine.
reference front()
Returns a reference to the first element.
ListElement< E > * m_next
Pointer to successor element.
void reverse()
Reverses the order of the list elements.
ListIteratorBase< E, isConst > & operator--()
Decrement operator (prefix).
void quicksort(const COMPARER &comp)
Sorts the list using Quicksort and comparer comp.
void quicksort()
Sorts the list using Quicksort.
const ListPure< E > & getListPure() const
Conversion to const ListPure.
reference chooseElement(std::function< bool(const E &)> includeElement=[](const E &) { return true;}, bool isFastTest=true)
Returns a random element.
ListIterator< E > search(const E &e, const COMPARER &comp)
Scans the list for the specified element (using the user-defined comparer) and returns an iterator to...
iterator pushFront(const E &x)
Adds element x at the beginning of the list.
bool operator!=(const ListPure< E > &L) const
Inequality operator.
iterator emplaceBack(Args &&... args)
Adds a new element at the end of the list.
reference back()
Returns a reference to the last element.
iterator rend() const
Returns an iterator to the one-before-first element in the container.
static void copy(const T &from, T &to)
const_reference front() const
Returns a const reference to the first element.
iterator emplaceFront(Args &&... args)
Adds a new element at the beginning of the list.
iterator end() const
Returns an iterator to the one-past-last element in the container.
void swap(List< E > &other)
Exchanges the contents of this list and other in constant time.
void moveToPrec(iterator it, iterator itAfter)
Moves it before itAfter.
List(List< E > &&L)
Constructs a doubly linked list containing the elements of L (move semantics).
void swap(ListPure< E > &other)
Exchanges the contents of this list and other in constant time.
ListElement(ListPure< E > *list, const E &x, ListElement< E > *next, ListElement< E > *prev)
Constructs a ListElement.
E popBackRet()
Removes the last element from the list and returns it.
Doubly linked lists (maintaining the length of the list).
ListPure(const ListPure< E > &L)
Constructs a doubly linked list that is a copy of L.
Structure for elements of doubly linked lists.
iterator end()
Returns an iterator to one-past-last element of the list.
void clear()
Removes all elements from the list.
const_iterator cbegin() const
Returns a const iterator to the first element of the list.
ListIteratorBase(const ListIteratorBase< E, isArgConst > &it)
Constructs an iterator that is a copy of it.
E popFrontRet()
Removes the first element from the list and returns it.
void moveToBack(iterator it, ListPure< E > &L2)
Moves it to the end of L2.
void exchange(iterator it1, iterator it2)
Exchanges the positions of it1 and it2 in the list.
E popFrontRet()
Removes the first element from the list and returns it.
const_iterator begin() const
Returns a const iterator to the first element of the list.
Encapsulates a pointer to a list element.
#define OGDF_NEW_DELETE
Makes the class use OGDF's memory allocator.
const T & move(const T &v)
void reassignListRefs(ListElement< E > *start=nullptr)
Sets the debug reference of all list elements starting at start to this.
Implementation of algorithms as templates working with different list types.
iterator insert(const E &x, iterator it, Direction dir=Direction::after)
Inserts element x before or after it.
Abstract base class for bucket functions.
iterator insert(const E &x, iterator it, Direction dir=Direction::after)
Inserts element x before or after it.
void bucketSort(Array< E > &a, int min, int max, BucketFunc< E > &f)
Bucket-sort array a using bucket assignment f; the values of f must be in the interval [min...
iterator rbegin() const
Returns an iterator to the last element in the container.
void moveToFront(iterator it, List< E > &L2)
Moves it to the begin of the list.
iterator insertBefore(const E &x, iterator it)
Inserts element x before it.
Elem & operator*() const
Returns a reference to the element content.
typename std::conditional< isConst, const ogdf::ClusterGraphObserver *, ogdf::ClusterGraphObserver * >::type Elem
The underlying type, depending on isConst.
int pos(const_iterator it) const
Returns the position (starting with 0) of iterator it in the list.
bool removeFirst(const E &x)
Removes the first occurrence of x (if any) from the list.
void moveToFront(iterator it, ListPure< E > &L2)
Moves it to the begin of L2.
void moveToSucc(iterator it, iterator itBefore)
Moves it after itBefore.
iterator rbegin()
Returns an iterator to the last element of the list.
int size() const
Returns the number of elements in the list.
void splitBefore(iterator it, ListPure< E > &L2)
Splits the list before it.
const_iterator rend() const
Returns a const iterator to one-before-first element of the list.
E value_type
Represents the data type stored in a list element.
int m_count
The length of the list.
List(const List< E > &L)
Constructs a doubly linked list that is a copy of L.
void popFront()
Removes the first element from the list.
virtual int getBucket(const E &x)=0
Returns the bucket of x.
ListIteratorBase< E, isConst > & operator++()
Increment operator (prefix).
iterator emplaceFront(Args &&... args)
Adds a new element at the beginning of the list.
virtual ~ListPure()
Destructor.
iterator insertBefore(const E &x, iterator it)
Inserts element x before it.
const_iterator crend() const
Returns a const iterator to one-before-first element of the list.
const_iterator chooseIterator(std::function< bool(const E &)> includeElement=[](const E &) { return true;}, bool isFastTest=true) const
Returns an iterator to a random element.
const_iterator end() const
Returns a const iterator to one-past-last element of the list.
void popBack()
Removes the last element from the list.
void concFront(List< E > &L2)
Prepends L2 to this list and makes L2 empty.
List()
Constructs an empty doubly linked list.
The namespace for all OGDF objects.
ListPure()
Constructs an empty doubly linked list.
void permute(RNG &rng)
Randomly permutes the elements in the list using random number generator rng.
iterator insertAfter(const E &x, iterator it)
Inserts element x after it.
void copy(const ListPure< E > &L)
List< E > & operator=(const List< E > &L)
Assignment operator.
bool operator!=(const List< E > &L) const
Inequality operator.
ListElement(ListPure< E > *list, ListElement< E > *next, ListElement< E > *prev, Args &&... args)
Constructs a ListElement with given arguments args for constructor of element type.
const_iterator crbegin() const
Returns a const iterator to the last element of the list.
ListConstIterator< E > search(const E &e) const
Scans the list for the specified element and returns an iterator to the first occurrence in the list...
ListPure< E > & operator=(ListPure< E > &&L)
Assignment operator (move semantics).
ListElement< E > * m_head
Pointer to first element.
void print(ostream &os, const Array< E, INDEX > &a, char delim=' ')
Prints array a to output stream os using delimiter delim.
bool operator==(const Tuple2< E1, E2 > &t1, const Tuple2< E1, E2 > &t2)
Equality operator for 2-tuples.
bool empty() const
Returns true iff the list is empty.
bool valid() const
Returns true iff the iterator points to an element.
int size() const
Returns the number of elements in the container.
iterator rend()
Returns an iterator to one-before-first element of the list.
void moveToPrec(iterator it, ListPure< E > &L2, iterator itAfter)
Moves it to list L2 and inserts it before itAfter.
void permute()
Randomly permutes the elements in the list.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
const_iterator rbegin() const
Returns a const iterator to the last element of the list.
ListPure< E > & operator=(const ListPure< E > &L)
Assignment operator.
const_reference back() const
Returns a const reference to the last element.