40 #include <type_traits> 87 operator std::conditional<isConst, const E, E> *()
const {
return m_pX; }
91 return m_pX == it.
m_pX;
96 return m_pX != it.
m_pX;
164 template<
bool isArgConst>
170 bool operator< (ArrayReverseIteratorBase<E, isConst> &it)
const {
return m_pX > it.m_pX; }
176 bool operator<=(ArrayReverseIteratorBase<E, isConst> &it)
const {
return m_pX >= it.m_pX; }
200 template<
class E,
class INDEX =
int>
class Array {
204 static const int maxSizeInsertionSort = 40;
229 construct(a,b); initialize();
233 Array(INDEX a, INDEX b,
const E &x) {
234 construct(a,b); initialize(x);
241 Array(std::initializer_list<E> initList) {
242 construct(0, ((INDEX) initList.size()) - 1);
243 initialize(initList);
256 : m_vpStart(
A.m_vpStart), m_pStart(
A.m_pStart), m_pStop(
A.m_pStop), m_low(
A.m_low), m_high(
A.m_high)
276 INDEX
low()
const {
return m_low; }
279 INDEX
high()
const {
return m_high; }
282 INDEX
size()
const {
return m_high - m_low + 1; }
285 bool empty()
const {
return size() == 0; }
376 void init(INDEX a, INDEX b,
const E &x) {
385 while(pDest > m_pStart)
390 void fill(INDEX i, INDEX j,
const E &x) {
396 E *pI = m_vpStart + i, *pJ = m_vpStart + j+1;
407 void grow(INDEX add,
const E &x);
414 void grow(INDEX add);
422 void resize(INDEX newSize,
const E &x) { grow(newSize - size(), x); }
429 void resize(INDEX newSize) { grow(newSize - size()); }
445 m_vpStart = A.m_vpStart;
446 m_pStart = A.m_pStart;
461 if (size() != L.
size()) {
465 auto thisIterator = begin();
466 auto thatIterator = L.
begin();
468 while (thisIterator != end() && thatIterator != L.
end()) {
469 if (*thisIterator != *thatIterator) {
499 std::swap(m_vpStart[i], m_vpStart[j]);
510 permute(low(), high());
520 void permute(INDEX l, INDEX r, RNG &rng);
529 permute(low(), high(), rng);
564 template<
class COMPARER>
566 return binarySearch(low(), high(), e, comp);
574 template<
class COMPARER>
575 INDEX
binarySearch(INDEX l, INDEX r,
const E& e,
const COMPARER &comp)
const {
576 if(r<l)
return low()-1;
579 if(comp.greater(e, m_vpStart[m]))
584 return comp.equal(e, m_vpStart[l]) ? l : low()-1;
594 for(i = size(); i-- > 0; )
595 if(e == m_pStart[i])
break;
604 template<
class COMPARER>
607 for(i = size(); i-- > 0; )
608 if(comp.equal(e, m_pStart[i]))
break;
626 template<
class COMPARER>
629 quicksortInt(m_pStart,m_pStop-1,comp);
638 template<
class COMPARER>
639 void quicksort(INDEX l, INDEX r,
const COMPARER &comp) {
645 quicksortInt(m_vpStart+l,m_vpStart+r,comp);
675 fill(high()-ind.
size(),high(),val);
690 void construct(INDEX a, INDEX b);
696 void initialize(
const E &x);
699 void initialize(std::initializer_list<E> initList);
708 void expandArray(INDEX add);
711 template<
class COMPARER>
716 if (s < maxSizeInsertionSort) {
717 for (E *pI = pL+1; pI <= pR; pI++) {
720 while (--pJ >= pL && comp.less(v,*pJ)) {
728 E *pI = pL, *pJ = pR;
732 while (comp.less(*pI,x)) pI++;
733 while (comp.less(x,*pJ)) pJ--;
734 if (pI <= pJ) std::swap(*pI++,*pJ--);
737 if (pL < pJ) quicksortInt(pL,pJ,comp);
738 if (pI < pR) quicksortInt(pI,pR,comp);
745 template<
class E,
class INDEX>
748 INDEX sOld = size(), sNew = sOld + add;
751 if (m_pStart !=
nullptr) {
753 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5 756 if (std::has_trivial_copy_assign<E>::value) {
758 if (std::is_trivially_copyable<E>::value) {
760 E *p =
static_cast<E *
>( realloc(m_pStart, sNew*
sizeof(E)) );
766 E *p =
static_cast<E *
>( malloc(sNew*
sizeof(E)) );
769 for (
int i = 0; i < min(sOld,sNew); ++i) {
778 m_pStart =
static_cast<E *
>( malloc(sNew*
sizeof(E)) );
782 m_vpStart = m_pStart - m_low;
783 m_pStop = m_pStart + sNew;
788 template<
class E,
class INDEX>
797 for (E *pDest = m_pStart+sOld; pDest < m_pStop; pDest++)
802 template<
class E,
class INDEX>
811 for (E *pDest = m_pStart+sOld; pDest < m_pStop; pDest++)
815 template<
class E,
class INDEX>
818 m_low = a; m_high = b;
822 m_pStart = m_vpStart = m_pStop =
nullptr;
825 m_pStart =
static_cast<E *
>( malloc(s*
sizeof(E)) );
828 m_vpStart = m_pStart - a;
829 m_pStop = m_pStart + s;
834 template<
class E,
class INDEX>
839 for (; pDest < m_pStop; pDest++)
842 while(--pDest >= m_pStart)
850 template<
class E,
class INDEX>
855 for (; pDest < m_pStop; pDest++)
858 while(--pDest >= m_pStart)
866 template<
class E,
class INDEX>
871 for (
const E &x : initList)
875 while (--pDest >= m_pStart)
883 template<
class E,
class INDEX>
886 if (!std::is_trivially_destructible<E>::value) {
887 for (E *pDest = m_pStart; pDest < m_pStop; pDest++)
894 template<
class E,
class INDEX>
899 if (m_pStart !=
nullptr) {
902 while(pDest > m_pStart)
906 new (--pDest) E(*--pSrc);
912 template<
class E,
class INDEX>
921 std::uniform_int_distribution<int> dist(0,r-l);
923 E *pI = m_vpStart+l, *pStart = m_vpStart+l, *pStop = m_vpStart+r;
925 std::swap( *pI++, *(pStart + dist(rng)) );
930 template<
class E,
class INDEX>
933 for (
int i = a.
low(); i <= a.
high(); i++) {
934 if (i > a.
low()) os << delim;
941 template<
class E,
class INDEX>
942 std::ostream &operator<<(std::ostream &os, const ogdf::Array<E,INDEX> &a)
955 template<
class E,
class INDEX>
957 const INDEX nInd = ind.
size();
958 if (nInd == 0)
return;
963 INDEX j, current = ind[0];
964 for (INDEX i = 0; i < nInd - 1; i++) {
968 const INDEX last = ind[i+1];
969 for(j = ind[i]+1; j < last; j++)
970 m_vpStart[current++] = m_vpStart[j];
974 for (j = ind[nInd - 1]+1; j < size(); j++)
975 m_vpStart[current++] = m_vpStart[j];
978 template<
class E,
class INDEX>
Array(Array< E, INDEX > &&A)
Creates an array containing the elements of A (move semantics).
ArrayReverseIteratorBase< E, isConst > & operator=(const ArrayReverseIteratorBase< E, isConst > &it)
Assignment operator.
Declarations for Comparer objects.
Array()
Creates an array with empty index set.
Array(INDEX a, INDEX b, const E &x)
Creates an array with index set [a..b] and initializes each element with x.
The parameterized class Array implements dynamic arrays of type E.
bool operator>(ArrayReverseIteratorBase< E, isConst > &it) const
Greater-than operator.
Declaration of memory manager for allocating small pieces of memory.
int operator-(ArrayReverseIteratorBase< E, isArgConst > &rhs)
Subtraction operator.
ArrayReverseIteratorBase< E, isConst > operator--(int)
Decrement operator (postfix).
INDEX binarySearch(INDEX l, INDEX r, const E &e, const COMPARER &comp) const
Performs a binary search for element e within the array section [l, ..., r] with comparer comp...
int binarySearch(INDEX l, INDEX r, const E &e) const
Performs a binary search for element e within the array section [l, ..., r] .
void swap(INDEX i, INDEX j)
Swaps the elements at position i and j.
void deconstruct()
Deallocates array.
void leftShift(ArrayBuffer< INDEX, INDEX > &ind)
Removes the components listed in ind by shifting the remaining components to the left.
Declaration and implementation of ArrayBuffer class.
void permute()
Randomly permutes the array.
bool operator>=(ArrayReverseIteratorBase< E, isConst > &it) const
Greater-than-or-equals operator.
void init()
Reinitializes the array to an array with empty index set.
#define OGDF_THROW(CLASS)
Replacement for throw.
typename std::conditional< isConst, const E, E >::type Elem
The underlying element, depending on isConst.
INDEX high() const
Returns the maximal array index.
void fill(INDEX i, INDEX j, const E &x)
Sets elements in the intervall [i..j] to x.
const_reverse_iterator rend() const
Returns a const reverse iterator to one before the first element.
INDEX m_high
The highest index.
Elem * m_pX
The pointer to the array element.
Further information about the cages of vertices in UML diagrams.
ArrayReverseIteratorBase< E, isConst > & operator-=(const int &rhs)
Compound assignment operator (-).
void quicksort(INDEX l, INDEX r, const COMPARER &comp)
Sorts the subarray with index set [l, ..., r] using Quicksort and a user-defined comparer comp...
Array(INDEX a, INDEX b)
Creates an array with index set [a..b].
void init(INDEX s)
Reinitializes the array to an array with index set [0..s-1].
An array that keeps track of the number of inserted elements; also usable as an efficient stack...
INDEX linearSearch(const E &e) const
Performs a linear search for element e.
friend ArrayReverseIteratorBase< E, isConst > operator+(const int &lhs, ArrayReverseIteratorBase< E, isConst > rhs)
Addition operator with int on the left-hand side. Returns the same result as addition with int on the...
const_iterator cend() const
Returns a const iterator to one past the last element.
int binarySearch(const E &e, const COMPARER &comp) const
Performs a binary search for element e with comparer comp.
reverse_iterator rbegin()
Returns an reverse iterator to the last element.
Array< E, INDEX > & operator=(Array< E, INDEX > &&A)
Assignment operator (move semantics).
void permute(INDEX l, INDEX r)
Randomly permutes the subarray with index set [l..r].
const_reverse_iterator crend() const
Returns a const reverse iterator to one before the first element.
void init(INDEX a, INDEX b)
Reinitializes the array to an array with index set [a..b].
void init(INDEX a, INDEX b, const E &x)
Reinitializes the array to an array with index set [a..b] and sets all entries to x...
ArrayReverseIteratorBase(const ArrayReverseIteratorBase< E, isArgConst > &it)
Constructs an iterator that is a copy of it.
Standard comparer (valid as a static comparer).
ArrayReverseIteratorBase()
Constructs an invalid iterator.
bool operator==(const Array< E, INDEX > &L) const
Equality operator.
long unsigned int randomSeed()
Returns a random value suitable as initial seed for a random number engine.
void leftShift(ArrayBuffer< INDEX, INDEX > &ind, const E &val)
Removes the components listed in ind by shifting the remaining components to the left.
Implementation of the Reverse class for the reverse iteration of containers.
void copy(const Array< E, INDEX > &A)
Constructs a new array which is a copy of A.
INDEX m_low
The lowest index.
Elem & operator[](std::size_t idx)
Member access operator.
bool empty() const
Returns true iff there are no elements in the array.
ArrayReverseIteratorBase< E, isConst > & operator+=(const int &rhs)
Compound assignment operator (+).
E * m_vpStart
The virtual start of the array (address of A[0]).
ArrayReverseIteratorBase< E, isConst > & operator++()
Increment operator (prefix).
Random-access reverse iterator based on a pointer to an array element.
INDEX size() const
Returns the size (number of elements) of the array.
static void copy(const T &from, T &to)
Definition of exception classes.
void grow(INDEX add, const E &x)
Enlarges the array by add elements and sets new elements to x.
ArrayReverseIteratorBase(E *pX)
Constructs an iterator that points to E* pX.
ArrayIterator< ogdf::OrthoRep::VertexInfoUML * > iterator
Provides a random-access iterator that can read or modify any element in an array.
Exception thrown when not enough memory is available to execute an algorithm.
const_iterator end() const
Returns a const iterator to one past the last element.
#define OGDF_NEW_DELETE
Makes the class use OGDF's memory allocator.
const T & move(const T &v)
INDEX size() const
Returns number of elements in the buffer.
int binarySearch(const E &e) const
Performs a binary search for element e.
bool operator!=(const Array< E, INDEX > &L) const
Inequality operator.
iterator begin()
Returns an iterator to the first element.
reverse_iterator rend()
Returns an reverse iterator to one before the first element.
reference operator[](INDEX i)
Returns a reference to the element at position i.
void resize(INDEX newSize, const E &x)
Resizes (enlarges or shrinks) the array to hold newSize elements and sets new elements to x...
ArrayReverseIteratorBase(const E *pX)
Constructs an iterator that points to const E* pX.
iterator end()
Returns an iterator to one past the last element.
void quicksort()
Sorts array using Quicksort.
void construct(INDEX a, INDEX b)
Allocates new array with index set [a, ..., b].
ArrayReverseIteratorBase< E, isConst > operator+(const int &rhs)
Addition operator with int on the right-hand side.
ArrayConstIterator< ogdf::OrthoRep::VertexInfoUML * > const_iterator
Provides a random-access iterator that can read a const element in an array.
void compactCopy(Array< E, INDEX > &A2) const
Generates a compact copy holding the current elements.
const_reverse_iterator crbegin() const
Returns a const reverse iterator to the last element.
const_reverse_iterator rbegin() const
Returns a const reverse iterator to the last element.
Array< E, INDEX > & operator=(const Array< E, INDEX > &A)
Assignment operator.
const_reference operator[](INDEX i) const
Returns a reference to the element at position i.
const E * ArrayConstIterator
E * m_pStart
The real start of the array (address of A[m_low]).
void fill(const E &x)
Sets all elements to x.
The namespace for all OGDF objects.
E * m_pStop
Successor of last element (address of A[m_high+1]).
void permute(RNG &rng)
Randomly permutes the array using random number generator rng.
const_iterator begin() const
Returns a const iterator to the first element.
ArrayReverseIteratorBase< E, isConst > operator-(const int &rhs)
Subtraction operator with int on the right-hand side.
void print(std::ostream &os, const Array< E, INDEX > &a, char delim=' ')
Prints array a to output stream os using delimiter delim.
void quicksort(INDEX l, INDEX r)
Sorts subarray with index set [l, ..., r] using Quicksort.
bool operator!=(const ArrayReverseIteratorBase< E, isConst > &it) const
Inequality operator.
const Elem & operator[](std::size_t idx) const
Const member access operator.
static void quicksortInt(E *pL, E *pR, const COMPARER &comp)
Internal Quicksort implementation with comparer template.
ArrayReverseIteratorBase< E, isConst > & operator--()
Decrement operator (prefix).
bool operator==(const ArrayReverseIteratorBase< E, isConst > &it) const
Equality operator.
void resize(INDEX newSize)
Resizes (enlarges or shrinks) the array to hold newSize elements.
Array(std::initializer_list< E > initList)
Creates an array containing the elements in the initializer list initList.
INDEX linearSearch(const E &e, const COMPARER &comp) const
Performs a linear search for element e with comparer comp.
Elem & operator*() const
Returns the element this iterator points to.
INDEX low() const
Returns the minimal array index.
void quicksort(const COMPARER &comp)
Sorts array using Quicksort and a user-defined comparer comp.
ArrayReverseIteratorBase< E, isConst > operator++(int)
Increment operator (postfix).
Array(INDEX s)
Creates an array with index set [0..s-1].
void initialize()
Initializes elements with default constructor.
Array(const Array< E, INDEX > &A)
Creates an array that is a copy of A.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
void expandArray(INDEX add)
Used by grow() to enlarge the array.
const_iterator cbegin() const
Returns a const iterator to the first element.