42#include <initializer_list>
50template<
class E,
bool isConst>
51class ArrayReverseIteratorBase;
52template<
class E,
class INDEX>
73template<
class E,
bool isConst>
78 using Elem =
typename std::conditional<isConst, const E, E>::type;
88 template<bool isConstSFINAE = isConst, typename std::enable_if<isConstSFINAE, int>::type = 0>
95 template<bool isArgConst, typename std::enable_if<isConst || !isArgConst, int>::type = 0>
105 operator std::conditional<isConst, const E, E>*()
const {
return m_pX; }
182 template<
bool isArgConst>
218template<
class E,
class INDEX =
int>
253 Array(INDEX a, INDEX b,
const E& x) {
262 Array(std::initializer_list<E> initList) {
263 construct(0, ((INDEX)initList.size()) - 1);
394 void init(INDEX a, INDEX b,
const E& x) {
409 void fill(INDEX i, INDEX j,
const E& x) {
427 void grow(INDEX add,
const E& x);
485 auto thisIterator =
begin();
486 auto thatIterator = L.
begin();
488 while (thisIterator !=
end() && thatIterator != L.
end()) {
489 if (*thisIterator != *thatIterator) {
579 template<
class COMPARER>
589 template<
class COMPARER>
590 INDEX
binarySearch(INDEX l, INDEX
r,
const E& e,
const COMPARER& comp)
const {
595 INDEX m = (
r + l) / 2;
613 for (i =
size(); i-- > 0;) {
627 template<
class COMPARER>
630 for (i =
size(); i-- > 0;) {
648 template<
class COMPARER>
661 template<
class COMPARER>
704 template<
class F,
class I>
736 template<typename EE = E, typename std::enable_if<OGDF_TRIVIALLY_COPYABLE<EE>::value,
int>::type = 0>
739 E* p =
static_cast<E*
>(realloc(
m_pStart, sNew *
sizeof(E)));
747 template<typename EE = E, typename std::enable_if<!OGDF_TRIVIALLY_COPYABLE<EE>::value,
int>::type = 0>
751 E* p =
static_cast<E*
>(malloc(sNew *
sizeof(E)));
756 for (
int i = 0; i < min(sOld, sNew); ++i) {
757 new (&p[i]) E(std::move(
m_pStart[i]));
765 template<
class COMPARER>
771 for (E* pI = pL + 1; pI <= pR; pI++) {
774 while (--pJ >= pL && comp.less(v, *pJ)) {
782 E *pI = pL, *pJ = pR;
783 E x = *(pL + (s >> 1));
786 while (comp.less(*pI, x)) {
789 while (comp.less(x, *pJ)) {
793 std::swap(*pI++, *pJ--);
809template<
class E,
class INDEX>
811 INDEX sOld = size(), sNew = sOld + add;
814 if (m_pStart !=
nullptr) {
815 expandArrayHelper(sOld, sNew);
817 m_pStart =
static_cast<E*
>(malloc(sNew *
sizeof(E)));
818 if (m_pStart ==
nullptr) {
823 m_vpStart = m_pStart - m_low;
824 m_pStop = m_pStart + sNew;
829template<
class E,
class INDEX>
839 for (E* pDest = m_pStart + sOld; pDest < m_pStop; pDest++) {
845template<
class E,
class INDEX>
855 for (E* pDest = m_pStart + sOld; pDest < m_pStop; pDest++) {
860template<
class E,
class INDEX>
867 m_pStart = m_vpStart = m_pStop =
nullptr;
870 m_pStart =
static_cast<E*
>(malloc(s *
sizeof(E)));
871 if (m_pStart ==
nullptr) {
875 m_vpStart = m_pStart - a;
876 m_pStop = m_pStart + s;
880template<
class E,
class INDEX>
884 for (; pDest < m_pStop; pDest++) {
888 while (--pDest >= m_pStart) {
896template<
class E,
class INDEX>
900 for (; pDest < m_pStop; pDest++) {
904 while (--pDest >= m_pStart) {
912template<
class E,
class INDEX>
916 for (
const E& x : initList) {
920 while (--pDest >= m_pStart) {
928template<
class E,
class INDEX>
930 if (!std::is_trivially_destructible<E>::value) {
931 for (E* pDest = m_pStart; pDest < m_pStop; pDest++) {
938template<
class E,
class INDEX>
942 if (m_pStart !=
nullptr) {
945 while (pDest > m_pStart)
949 new (--pDest) E(*--pSrc);
954template<
class E,
class INDEX>
962 std::uniform_int_distribution<int> dist(0,
r - l);
964 E *pI = m_vpStart + l, *pStart = m_vpStart + l, *pStop = m_vpStart +
r;
965 while (pI <= pStop) {
966 std::swap(*pI++, *(pStart + dist(rng)));
971template<
class E,
class INDEX>
973 for (
int i = a.
low(); i <= a.
high(); i++) {
982template<
class E,
class INDEX>
993template<
class E,
class INDEX>
995 const INDEX nInd = ind.
size();
1003 INDEX j, current = ind[0];
1004 for (INDEX i = 0; i < nInd - 1; i++) {
1008 const INDEX last = ind[i + 1];
1009 for (j = ind[i] + 1; j < last; j++) {
1010 m_vpStart[current++] = m_vpStart[j];
1015 for (j = ind[nInd - 1] + 1; j < size(); j++) {
1016 m_vpStart[current++] = m_vpStart[j];
1020template<
class E,
class INDEX>
1023 A.compactCopy(*
this);
Basic declarations, included by all source files.
An array that keeps track of the number of inserted elements; also usable as an efficient stack.
INDEX size() const
Returns number of elements in the buffer.
The parameterized class Array implements dynamic arrays of type E.
iterator begin()
Returns an iterator to the first element.
const_reference operator[](INDEX i) const
Returns a reference to the element at position i.
void quicksort(const COMPARER &comp)
Sorts array using Quicksort and a user-defined comparer comp.
void resize(INDEX newSize, const E &x)
Resizes (enlarges or shrinks) the array to hold newSize elements and sets new elements to x.
reference operator[](INDEX i)
Returns a reference to the element at position i.
void grow(INDEX add, const E &x)
Enlarges the array by add elements and sets new elements to x.
void deconstruct()
Deallocates array.
void swap(INDEX i, INDEX j)
Swaps the elements at position i and j.
const_reverse_iterator rend() const
Returns a const reverse iterator to one before the first element.
E * m_pStop
Successor of last element (address of A[m_high+1]).
static const int maxSizeInsertionSort
Threshold used by quicksort() such that insertion sort is called for instances smaller than maxSizeIn...
INDEX high() const
Returns the maximal array index.
void permute(INDEX l, INDEX r)
Randomly permutes the subarray with index set [l..r].
reverse_iterator rbegin()
Returns an reverse iterator to the last element.
INDEX m_low
The lowest index.
reverse_iterator rend()
Returns an reverse iterator to one before the first element.
INDEX linearSearch(const E &e) const
Performs a linear search for element e.
void leftShift(ArrayBuffer< INDEX, INDEX > &ind)
Removes the components listed in ind by shifting the remaining components to the left.
Array(const ArrayBuffer< E, INDEX > &A)
Creates an array that is a copy of A. The array-size is set to be the number of elements (not the cap...
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.
const_iterator cend() const
Returns a const iterator to one past the last element.
static void quicksortInt(E *pL, E *pR, const COMPARER &comp)
Internal Quicksort implementation with comparer template.
const_iterator begin() const
Returns a const iterator to the first element.
void grow(INDEX add)
Enlarges the array by add elements.
int binarySearch(const E &e, const COMPARER &comp) const
Performs a binary search for element e with comparer comp.
void permute(RNG &rng)
Randomly permutes the array using random number generator rng.
void construct(INDEX a, INDEX b)
Allocates new array with index set [a, ..., b].
iterator end()
Returns an iterator to one past the last element.
bool empty() const
Returns true iff there are no elements in the array.
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.
void init()
Reinitializes the array to an array with empty index set.
void fill(const E &x)
Sets all elements to x.
void initialize(const E &x)
Initializes elements with x.
void quicksort()
Sorts array using Quicksort.
void expandArray(INDEX add)
Used by grow() to enlarge the array.
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 expandArrayHelper(INDEX sOld, INDEX sNew)
Used by expandArray() to reallocate the array elements.
Array(INDEX a, INDEX b, const E &x)
Creates an array with index set [a..b] and initializes each element with x.
Array(Array< E, INDEX > &&A) noexcept
Creates an array containing the elements of A (move semantics).
bool operator!=(const Array< E, INDEX > &L) const
Inequality operator.
E & reference
Provides a reference to an element stored in an array.
int binarySearch(const E &e) const
Performs a binary search for element e.
ArrayConstIterator< E > const_iterator
Provides a random-access iterator that can read a const element in an array.
Array< E, INDEX > & operator=(const Array< E, INDEX > &A)
Assignment operator.
INDEX low() const
Returns the minimal array index.
void initialize()
Initializes elements with default constructor.
void permute()
Randomly permutes the array.
void quicksort(INDEX l, INDEX r)
Sorts subarray with index set [l, ..., r] using Quicksort.
INDEX size() const
Returns the size (number of elements) of the array.
int binarySearch(INDEX l, INDEX r, const E &e) const
Performs a binary search for element e within the array section [l, ..., r] .
const_reverse_iterator crbegin() const
Returns a const reverse iterator to the last element.
void leftShift(ArrayBuffer< INDEX, INDEX > &ind, const E &val)
Removes the components listed in ind by shifting the remaining components to the left.
void copy(const Array< E, INDEX > &A)
Constructs a new array which is a copy of A.
void resize(INDEX newSize)
Resizes (enlarges or shrinks) the array to hold newSize elements.
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.
const_iterator end() const
Returns a const iterator to one past the last element.
void fill(INDEX i, INDEX j, const E &x)
Sets elements in the intervall [i..j] to x.
INDEX m_high
The highest index.
void initialize(std::initializer_list< E > initList)
Initializes elements from given initializer list initList.
ArrayIterator< E > iterator
Provides a random-access iterator that can read or modify any element in an array.
bool operator==(const Array< E, INDEX > &L) const
Equality operator.
Array(INDEX a, INDEX b)
Creates an array with index set [a..b].
E * m_vpStart
The virtual start of the array (address of A[0]).
E * m_pStart
The real start of the array (address of A[m_low]).
Array()
Creates an array with empty index set.
E value_type
Represents the data type stored in an array element.
Array(const Array< E, INDEX > &A)
Creates an array that is a copy of A.
Array(INDEX s)
Creates an array with index set [0..s-1].
const_iterator cbegin() const
Returns a const iterator to the first element.
void init(INDEX s)
Reinitializes the array to an array with index set [0..s-1].
void permute(INDEX l, INDEX r, RNG &rng)
Randomly permutes the subarray with index set [l..r] using random number generator rng.
INDEX linearSearch(const E &e, const COMPARER &comp) const
Performs a linear search for element e with comparer comp.
const E & const_reference
Provides a reference to a const element stored in an array for reading and performing const operation...
Array< E, INDEX > & operator=(Array< E, INDEX > &&A)
Assignment operator (move semantics).
const_reverse_iterator rbegin() const
Returns a const reverse iterator to the last element.
Array(std::initializer_list< E > initList)
Creates an array containing the elements in the initializer list initList.
Random-access reverse iterator based on a pointer to an array element.
bool operator<=(ArrayReverseIteratorBase< E, isConst > &it) const
Less-than-or-equals operator.
ArrayReverseIteratorBase< E, isConst > operator-(const int &rhs)
Subtraction operator with int on the right-hand side.
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 Elem & operator[](std::size_t idx) const
Const member access operator.
bool operator!=(const ArrayReverseIteratorBase< E, isConst > &it) const
Inequality operator.
ArrayReverseIteratorBase()
Constructs an invalid iterator.
int operator-(ArrayReverseIteratorBase< E, isArgConst > &rhs)
Subtraction operator.
ArrayReverseIteratorBase(E *pX)
Constructs an iterator that points to E* pX.
ArrayReverseIteratorBase(const E *pX)
Constructs an iterator that points to const E* pX.
ArrayReverseIteratorBase< E, isConst > & operator++()
Increment operator (prefix).
bool operator>=(ArrayReverseIteratorBase< E, isConst > &it) const
Greater-than-or-equals operator.
ArrayReverseIteratorBase< E, isConst > operator++(int)
Increment operator (postfix).
ArrayReverseIteratorBase(const ArrayReverseIteratorBase< E, isConst > &it)
Copy constructor. clang10 does not see the above templated one match this case and requires it explic...
bool operator==(const ArrayReverseIteratorBase< E, isConst > &it) const
Equality operator.
ArrayReverseIteratorBase< E, isConst > & operator=(const ArrayReverseIteratorBase< E, isConst > &it)
Assignment operator.
ArrayReverseIteratorBase< E, isConst > operator+(const int &rhs)
Addition operator with int on the right-hand side.
Elem * m_pX
The pointer to the array element.
bool operator>(ArrayReverseIteratorBase< E, isConst > &it) const
Greater-than operator.
ArrayReverseIteratorBase< E, isConst > & operator--()
Decrement operator (prefix).
Elem & operator*() const
Returns the element this iterator points to.
ArrayReverseIteratorBase< E, isConst > operator--(int)
Decrement operator (postfix).
Elem & operator[](std::size_t idx)
Member access operator.
ArrayReverseIteratorBase(const ArrayReverseIteratorBase< E, isArgConst > &it)
Constructs an iterator that is a copy of it.
ArrayReverseIteratorBase< E, isConst > & operator+=(const int &rhs)
Compound assignment operator (+).
ArrayReverseIteratorBase< E, isConst > & operator-=(const int &rhs)
Compound assignment operator (-).
bool operator<(ArrayReverseIteratorBase< E, isConst > &it) const
Less-than operator.
typename std::conditional< isConst, const E, E >::type Elem
The underlying element, depending on isConst.
Exception thrown when not enough memory is available to execute an algorithm.
Standard comparer (valid as a static comparer).
Declarations for Comparer objects.
Definition of exception classes.
#define OGDF_THROW(CLASS)
Replacement for throw.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
#define OGDF_NEW_DELETE
Makes the class use OGDF's memory allocator.
long unsigned int randomSeed()
Returns a random value suitable as initial seed for a random number engine.
Declaration of memory manager for allocating small pieces of memory.
The namespace for all OGDF objects.
std::ostream & operator<<(std::ostream &os, const ogdf::Array< E, INDEX > &a)
Prints array a to output stream os.
const E * ArrayConstIterator
void print(std::ostream &os, const Array< E, INDEX > &a, char delim=' ')
Prints array a to output stream os using delimiter delim.