39 #include <type_traits> 56 template<
class E,
class INDEX =
int>
class Array {
85 Array(INDEX a, INDEX b,
const E &x) {
93 Array(std::initializer_list<E> initList) {
94 construct(0, ((INDEX) initList.size()) - 1);
228 void init(INDEX a, INDEX b,
const E &x) {
242 void fill(INDEX i, INDEX j,
const E &x) {
259 void grow(INDEX add,
const E &x);
266 void grow(INDEX add);
343 void permute(INDEX l, INDEX r, RNG &rng);
385 template<
class COMPARER>
395 template<
class COMPARER>
396 INDEX
binarySearch(INDEX l, INDEX r,
const E& e,
const COMPARER &comp)
const {
397 if(r<l)
return low()-1;
415 for(i =
size(); i-- > 0; )
425 template<
class COMPARER>
428 for(i =
size(); i-- > 0; )
429 if(comp.equal(e,
m_pStart[i]))
break;
447 template<
class COMPARER>
459 template<
class COMPARER>
460 void quicksort(INDEX l, INDEX r,
const COMPARER &comp) {
520 void initialize(std::initializer_list<E> initList);
532 template<
class COMPARER>
537 if (s < maxSizeInsertionSort) {
538 for (E *pI = pL+1; pI <= pR; pI++) {
541 while (--pJ >= pL && comp.less(v,*pJ)) {
549 E *pI = pL, *pJ = pR;
553 while (comp.less(*pI,x)) pI++;
554 while (comp.less(x,*pJ)) pJ--;
555 if (pI <= pJ) std::swap(*pI++,*pJ--);
567 template<
class E,
class INDEX>
570 INDEX sOld =
size(), sNew = sOld + add;
575 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5 578 if (std::has_trivial_copy_assign<E>::value) {
580 if (std::is_trivially_copyable<E>::value) {
582 E *p =
static_cast<E *
>( realloc(
m_pStart, sNew*
sizeof(E)) );
588 E *p =
static_cast<E *
>( malloc(sNew*
sizeof(E)) );
591 for (
int i = 0; i < min(sOld,sNew); ++i) {
600 m_pStart =
static_cast<E *
>( malloc(sNew*
sizeof(E)) );
610 template<
class E,
class INDEX>
624 template<
class E,
class INDEX>
637 template<
class E,
class INDEX>
647 m_pStart =
static_cast<E *
>( malloc(s*
sizeof(E)) );
656 template<
class E,
class INDEX>
661 for (; pDest <
m_pStop; pDest++)
672 template<
class E,
class INDEX>
677 for (; pDest <
m_pStop; pDest++)
688 template<
class E,
class INDEX>
693 for (
const E &x : initList)
705 template<
class E,
class INDEX>
708 if (!std::is_trivially_destructible<E>::value) {
716 template<
class E,
class INDEX>
728 new (--pDest) E(*--pSrc);
734 template<
class E,
class INDEX>
743 std::uniform_int_distribution<int> dist(0,r-l);
747 std::swap( *pI++, *(pStart + dist(rng)) );
752 template<
class E,
class INDEX>
755 for (
int i = a.
low(); i <= a.
high(); i++) {
756 if (i > a.
low()) os << delim;
763 template<
class E,
class INDEX>
764 ostream &operator<<(ostream &os, const ogdf::Array<E,INDEX> &a)
777 template<
class E,
class INDEX>
779 const INDEX nInd = ind.
size();
780 if (nInd == 0)
return;
785 INDEX j, current = ind[0];
786 for (INDEX i = 0; i < nInd - 1; i++) {
790 const INDEX last = ind[i+1];
791 for(j = ind[i]+1; j < last; j++)
796 for (j = ind[nInd - 1]+1; j <
size(); j++)
800 template<
class E,
class INDEX>
Array(Array< E, INDEX > &&A)
Creates an array containing the elements of A (move semantics).
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.
Declaration of memory manager for allocating small pieces of memory.
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.
static const int maxSizeInsertionSort
Threshold used by quicksort() such that insertion sort is called for instances smaller than maxSizeIn...
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.
void init()
Reinitializes the array to an array with empty index set.
#define OGDF_THROW(CLASS)
Replacement for throw.
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.
INDEX m_high
The highest index.
const_iterator crend() const
Returns a const iterator to one before the first element.
Further information about the cages of vertices in UML diagrams.
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...
const_iterator crbegin() const
Returns a const iterator to the last element.
INDEX linearSearch(const E &e) const
Performs a linear search for element e.
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.
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].
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...
Standard comparer (valid as a static comparer).
const_iterator rbegin() const
Returns a const iterator to the last element.
iterator rbegin()
Returns an iterator to the last element.
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.
void copy(const Array< E, INDEX > &A)
Constructs a new array which is a copy of A.
INDEX m_low
The lowest index.
bool empty() const
Returns true iff there are no elements in the array.
E * m_vpStart
The virtual start of the array (address of A[0]).
INDEX size() const
Returns the size (number of elements) of the array.
Definition of exception classes.
void grow(INDEX add, const E &x)
Enlarges the array by add elements and sets new elements to x.
iterator rend()
Returns an iterator to one before the first element.
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.
iterator begin()
Returns an iterator to 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...
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].
void compactCopy(Array< E, INDEX > &A2) const
Generates a compact copy holding the current elements.
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.
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.
void quicksort(INDEX l, INDEX r)
Sorts subarray with index set [l, ..., r] using Quicksort.
static void quicksortInt(E *pL, E *pR, const COMPARER &comp)
Internal Quicksort implementation with comparer template.
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.
void print(ostream &os, const Array< E, INDEX > &a, char delim=' ')
Prints array a to output stream os using delimiter delim.
INDEX low() const
Returns the minimal array index.
void quicksort(const COMPARER &comp)
Sorts array using Quicksort and a user-defined comparer comp.
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.
const_iterator rend() const
Returns a const iterator to one before the first element.