Open
Graph Drawing
Framework

 v.2015.05
 

ogdf::Array< E, INDEX > Class Template Reference

The parameterized class Array<E,INDEX> implements dynamic arrays of type E. More...

#include <ogdf/basic/Array.h>

+ Inheritance diagram for ogdf::Array< E, INDEX >:

Public Types

enum  { maxSizeInsertionSort = 40 }
 

Public Member Functions

 Array ()
 Creates an array with empty index set. More...
 
 Array (INDEX s)
 Creates an array with index set [0..s-1]. More...
 
 Array (INDEX a, INDEX b)
 Creates an array with index set [a..b]. More...
 
 Array (INDEX a, INDEX b, const E &x)
 Creates an array with index set [a..b] and initializes each element with x. More...
 
 Array (const Array< E, INDEX > &A)
 Creates an array that is a copy of A. More...
 
 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 capacity) of the buffer. More...
 
 ~Array ()
 
E * begin ()
 Returns a pointer to the first element. More...
 
const E * begin () const
 Returns a pointer to the first element. More...
 
int binarySearch (const E &e) const
 Performs a binary search for element x. More...
 
int binarySearch (INDEX l, INDEX r, const E &e) const
 Performs a binary search for element x within the array section [l..r] . More...
 
template<class COMPARER >
int binarySearch (const E &e, const COMPARER &comp) const
 Performs a binary search for element x with comparer comp. More...
 
template<class COMPARER >
INDEX binarySearch (INDEX l, INDEX r, const E &e, const COMPARER &comp) const
 Performs a binary search for element x within the array section [l..r] with comparer comp. More...
 
E * end ()
 Returns a pointer to one past the last element. More...
 
const E * end () const
 Returns a pointer to one past the last element. More...
 
void fill (const E &x)
 Sets all elements to x. More...
 
void fill (INDEX i, INDEX j, const E &x)
 Sets elements in the intervall [i..j] to x. More...
 
void grow (INDEX add, const E &x)
 Enlarges the array by add elements and sets new elements to x. More...
 
void grow (INDEX add)
 Enlarges the array by add elements. More...
 
INDEX high () const
 Returns the maximal array index. More...
 
void init ()
 Reinitializes the array to an array with empty index set. More...
 
void init (INDEX s)
 Reinitializes the array to an array with index set [0..s-1]. More...
 
void init (INDEX a, INDEX b)
 Reinitializes the array to an array with index set [a..b]. More...
 
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. More...
 
void leftShift (ArrayBuffer< INDEX, INDEX > &ind)
 Removes the components listed in ind by shifting the remaining components to the left. More...
 
void leftShift (ArrayBuffer< INDEX, INDEX > &ind, const E &val)
 Removes the components listed in ind by shifting the remaining components to the left. More...
 
INDEX linearSearch (const E &e) const
 Performs a linear search for element x. More...
 
template<class COMPARER >
INDEX linearSearch (const E &e, const COMPARER &comp) const
 Performs a linear search for element x with comparer comp. More...
 
INDEX low () const
 Returns the minimal array index. More...
 
Array< E, INDEX > & operator= (const Array< E, INDEX > &array2)
 Assignment operator. More...
 
const E & operator[] (INDEX i) const
 Returns a reference to the element at position i. More...
 
E & operator[] (INDEX i)
 Returns a reference to the element at position i. More...
 
void permute (INDEX l, INDEX r)
 Randomly permutes the subarray with index set [l..r]. More...
 
void permute ()
 Randomly permutes the array. More...
 
void quicksort ()
 Sorts array using Quicksort. More...
 
void quicksort (INDEX l, INDEX r)
 Sorts subarray with index set [l..r] using Quicksort. More...
 
template<class COMPARER >
void quicksort (const COMPARER &comp)
 Sorts array using Quicksort and a user-defined comparer comp. More...
 
template<class COMPARER >
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. More...
 
E * rbegin ()
 Returns a pointer to the last element. More...
 
const E * rbegin () const
 Returns a pointer to the last element. More...
 
E * rend ()
 Returns a pointer to one before the first element. More...
 
const E * rend () const
 Returns a pointer to one before the first element. More...
 
void resize (INDEX newSize, const E &x)
 Resizes (enlarges or shrinks) the array to hold newSize elements and sets new elements to x. More...
 
void resize (INDEX newSize)
 Resizes (enlarges or shrinks) the array to hold newSize elements. More...
 
INDEX size () const
 Returns the size (number of elements) of the array. More...
 
void swap (INDEX i, INDEX j)
 Swaps the elements at position i and j. More...
 

Private Member Functions

void construct (INDEX a, INDEX b)
 Allocates new array with index set [a..b]. More...
 
void copy (const Array< E, INDEX > &A)
 Constructs a new array which is a copy of A. More...
 
void deconstruct ()
 Deallocates array. More...
 
void initialize ()
 Initializes elements with default constructor. More...
 
void initialize (const E &x)
 Initializes elements with x. More...
 

Static Private Member Functions

template<class COMPARER >
static void quicksortInt (E *pL, E *pR, const COMPARER &comp)
 Internal Quicksort implementation with comparer template. More...
 

Private Attributes

INDEX m_high
 The highest index. More...
 
INDEX m_low
 The lowest index. More...
 
E * m_pStart
 The real start of the array (address of A[m_low]). More...
 
E * m_pStop
 Successor of last element (address of A[m_high+1]). More...
 
E * m_vpStart
 The virtual start of the array (address of A[0]). More...
 

Friends

template<class F , class I >
class ArrayBuffer
 

Detailed Description

template<class E, class INDEX = int>
class ogdf::Array< E, INDEX >

The parameterized class Array<E,INDEX> implements dynamic arrays of type E.

Template Parameters
Edenotes the element type.
INDEXdenotes the index type. The index type must be chosen such that it can express the whole index range of the array instance, as well as its size. The default index type is int, other possible types are short and long long (on 64-bit systems).

Definition at line 108 of file Array.h.

Member Enumeration Documentation

template<class E, class INDEX = int>
anonymous enum

Threshold used by quicksort() such that insertion sort is called for instances smaller than maxSizeInsertionSort.

Enumerator
maxSizeInsertionSort 

Definition at line 112 of file Array.h.

Constructor & Destructor Documentation

template<class E, class INDEX = int>
ogdf::Array< E, INDEX >::Array ( )
inline

Creates an array with empty index set.

Definition at line 116 of file Array.h.

template<class E, class INDEX = int>
ogdf::Array< E, INDEX >::Array ( INDEX  s)
inlineexplicit

Creates an array with index set [0..s-1].

Definition at line 119 of file Array.h.

template<class E, class INDEX = int>
ogdf::Array< E, INDEX >::Array ( INDEX  a,
INDEX  b 
)
inline

Creates an array with index set [a..b].

Definition at line 124 of file Array.h.

template<class E, class INDEX = int>
ogdf::Array< E, INDEX >::Array ( INDEX  a,
INDEX  b,
const E &  x 
)
inline

Creates an array with index set [a..b] and initializes each element with x.

Definition at line 129 of file Array.h.

template<class E, class INDEX = int>
ogdf::Array< E, INDEX >::Array ( const Array< E, INDEX > &  A)
inline

Creates an array that is a copy of A.

Definition at line 134 of file Array.h.

template<class E, class INDEX>
ogdf::Array< E, INDEX >::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 capacity) of the buffer.

Definition at line 716 of file Array.h.

template<class E, class INDEX = int>
ogdf::Array< E, INDEX >::~Array ( )
inline

Definition at line 142 of file Array.h.

Member Function Documentation

template<class E, class INDEX = int>
E* ogdf::Array< E, INDEX >::begin ( )
inline

Returns a pointer to the first element.

Definition at line 156 of file Array.h.

template<class E, class INDEX = int>
const E* ogdf::Array< E, INDEX >::begin ( ) const
inline

Returns a pointer to the first element.

Definition at line 159 of file Array.h.

template<class E, class INDEX = int>
int ogdf::Array< E, INDEX >::binarySearch ( const E &  e) const
inline

Performs a binary search for element x.

Precondition
The array must be sorted!
Returns
the index of the found element, and low()-1 if not found.

Definition at line 308 of file Array.h.

template<class E, class INDEX = int>
int ogdf::Array< E, INDEX >::binarySearch ( INDEX  l,
INDEX  r,
const E &  e 
) const
inline

Performs a binary search for element x within the array section [l..r] .

Precondition
The array must be sorted!
Returns
the index of the found element, and low()-1 if not found.

Definition at line 317 of file Array.h.

template<class E, class INDEX = int>
template<class COMPARER >
int ogdf::Array< E, INDEX >::binarySearch ( const E &  e,
const COMPARER &  comp 
) const
inline

Performs a binary search for element x with comparer comp.

Precondition
The array must be sorted according to comp!
Returns
the index of the found element, and low()-1 if not found.

Definition at line 327 of file Array.h.

template<class E, class INDEX = int>
template<class COMPARER >
INDEX ogdf::Array< E, INDEX >::binarySearch ( INDEX  l,
INDEX  r,
const E &  e,
const COMPARER &  comp 
) const
inline

Performs a binary search for element x within the array section [l..r] with comparer comp.

Precondition
The array must be sorted according to comp!
Returns
the index of the found element, and low()-1 if not found.

Definition at line 337 of file Array.h.

template<class E , class INDEX>
void ogdf::Array< E, INDEX >::construct ( INDEX  a,
INDEX  b 
)
private

Allocates new array with index set [a..b].

Definition at line 553 of file Array.h.

template<class E, class INDEX>
void ogdf::Array< E, INDEX >::copy ( const Array< E, INDEX > &  A)
private

Constructs a new array which is a copy of A.

Definition at line 615 of file Array.h.

template<class E , class INDEX >
void ogdf::Array< E, INDEX >::deconstruct ( )
private

Deallocates array.

Definition at line 604 of file Array.h.

template<class E, class INDEX = int>
E* ogdf::Array< E, INDEX >::end ( )
inline

Returns a pointer to one past the last element.

Definition at line 162 of file Array.h.

template<class E, class INDEX = int>
const E* ogdf::Array< E, INDEX >::end ( ) const
inline

Returns a pointer to one past the last element.

Definition at line 165 of file Array.h.

template<class E, class INDEX = int>
void ogdf::Array< E, INDEX >::fill ( const E &  x)
inline

Sets all elements to x.

Definition at line 237 of file Array.h.

template<class E, class INDEX = int>
void ogdf::Array< E, INDEX >::fill ( INDEX  i,
INDEX  j,
const E &  x 
)
inline

Sets elements in the intervall [i..j] to x.

Definition at line 244 of file Array.h.

template<class E, class INDEX>
void ogdf::Array< E, INDEX >::grow ( INDEX  add,
const E &  x 
)

Enlarges the array by add elements and sets new elements to x.

Note: address of array entries in memory may change!

Parameters
addis the number of additional elements; add can be negative in order to shrink the array.
xis the inital value of all new elements.

Definition at line 500 of file Array.h.

template<class E, class INDEX>
void ogdf::Array< E, INDEX >::grow ( INDEX  add)

Enlarges the array by add elements.

Note: address of array entries in memory may change!

Parameters
addis the number of additional elements; add can be negative in order to shrink the array.

Definition at line 527 of file Array.h.

template<class E, class INDEX = int>
INDEX ogdf::Array< E, INDEX >::high ( ) const
inline

Returns the maximal array index.

Definition at line 150 of file Array.h.

template<class E, class INDEX = int>
void ogdf::Array< E, INDEX >::init ( )
inline

Reinitializes the array to an array with empty index set.

Definition at line 200 of file Array.h.

template<class E, class INDEX = int>
void ogdf::Array< E, INDEX >::init ( INDEX  s)
inline

Reinitializes the array to an array with index set [0..s-1].

Notice that the elements contained in the array get discarded!

Definition at line 210 of file Array.h.

template<class E, class INDEX = int>
void ogdf::Array< E, INDEX >::init ( INDEX  a,
INDEX  b 
)
inline

Reinitializes the array to an array with index set [a..b].

Notice that the elements contained in the array get discarded!

Definition at line 216 of file Array.h.

template<class E, class INDEX = int>
void ogdf::Array< E, INDEX >::init ( INDEX  a,
INDEX  b,
const E &  x 
)
inline

Reinitializes the array to an array with index set [a..b] and sets all entries to x.

Definition at line 223 of file Array.h.

template<class E , class INDEX >
void ogdf::Array< E, INDEX >::initialize ( )
private

Initializes elements with default constructor.

Definition at line 572 of file Array.h.

template<class E, class INDEX >
void ogdf::Array< E, INDEX >::initialize ( const E &  x)
private

Initializes elements with x.

Definition at line 588 of file Array.h.

template<class E , class INDEX>
void ogdf::Array< E, INDEX >::leftShift ( ArrayBuffer< INDEX, INDEX > &  ind)

Removes the components listed in ind by shifting the remaining components to the left.

The "free" positions in the array at the end remain as they are.

This function is mainly used by Abacus. Other uses are supposed to be rare.

Memory management of the removed components must be carefully implemented by the user of this function to avoid memory leaks.

Parameters
indThe compenents being removed from the array.

shift all items up to the last element of ind to the left

copy the rest of the buffer

Definition at line 688 of file Array.h.

template<class E, class INDEX = int>
void ogdf::Array< E, INDEX >::leftShift ( ArrayBuffer< INDEX, INDEX > &  ind,
const E &  val 
)
inline

Removes the components listed in ind by shifting the remaining components to the left.

The "free" positions in the array at the end are filled with val.

Memory management of the removed components must be carefully implemented by the user of this function to avoid memory leaks.

Parameters
indspecifies the components that are removed from the array.
valis the value used to fill the positions that become free.

Definition at line 433 of file Array.h.

template<class E, class INDEX = int>
INDEX ogdf::Array< E, INDEX >::linearSearch ( const E &  e) const
inline

Performs a linear search for element x.

Warning: This method has linear running time! Note that the linear search runs from back to front.

Returns
the index of the found element, and low()-1 if not found.

Definition at line 354 of file Array.h.

template<class E, class INDEX = int>
template<class COMPARER >
INDEX ogdf::Array< E, INDEX >::linearSearch ( const E &  e,
const COMPARER &  comp 
) const
inline

Performs a linear search for element x with comparer comp.

Warning: This method has linear running time! Note that the linear search runs from back to front.

Returns
the index of the found element, and low()-1 if not found.

Definition at line 367 of file Array.h.

template<class E, class INDEX = int>
INDEX ogdf::Array< E, INDEX >::low ( ) const
inline

Returns the minimal array index.

Definition at line 147 of file Array.h.

template<class E, class INDEX = int>
Array<E,INDEX>& ogdf::Array< E, INDEX >::operator= ( const Array< E, INDEX > &  array2)
inline

Assignment operator.

Definition at line 230 of file Array.h.

template<class E, class INDEX = int>
const E& ogdf::Array< E, INDEX >::operator[] ( INDEX  i) const
inline

Returns a reference to the element at position i.

Definition at line 180 of file Array.h.

template<class E, class INDEX = int>
E& ogdf::Array< E, INDEX >::operator[] ( INDEX  i)
inline

Returns a reference to the element at position i.

Definition at line 186 of file Array.h.

template<class E , class INDEX>
void ogdf::Array< E, INDEX >::permute ( INDEX  l,
INDEX  r 
)

Randomly permutes the subarray with index set [l..r].

Definition at line 631 of file Array.h.

template<class E, class INDEX = int>
void ogdf::Array< E, INDEX >::permute ( )
inline

Randomly permutes the array.

Definition at line 287 of file Array.h.

template<class E, class INDEX = int>
void ogdf::Array< E, INDEX >::quicksort ( )
inline

Sorts array using Quicksort.

Definition at line 375 of file Array.h.

template<class E, class INDEX = int>
void ogdf::Array< E, INDEX >::quicksort ( INDEX  l,
INDEX  r 
)
inline

Sorts subarray with index set [l..r] using Quicksort.

Definition at line 380 of file Array.h.

template<class E, class INDEX = int>
template<class COMPARER >
void ogdf::Array< E, INDEX >::quicksort ( const COMPARER &  comp)
inline

Sorts array using Quicksort and a user-defined comparer comp.

Parameters
compis a user-defined comparer; C must be a class providing a less(x,y) method.

Definition at line 389 of file Array.h.

template<class E, class INDEX = int>
template<class COMPARER >
void ogdf::Array< E, INDEX >::quicksort ( INDEX  l,
INDEX  r,
const COMPARER &  comp 
)
inline

Sorts the subarray with index set [l..r] using Quicksort and a user-defined comparer comp.

Parameters
lis the left-most position in the range to be sorted.
ris the right-most position in the range to be sorted.
compis a user-defined comparer; C must be a class providing a less(x,y) method.

Definition at line 401 of file Array.h.

template<class E, class INDEX = int>
template<class COMPARER >
static void ogdf::Array< E, INDEX >::quicksortInt ( E *  pL,
E *  pR,
const COMPARER &  comp 
)
inlinestaticprivate

Internal Quicksort implementation with comparer template.

Definition at line 464 of file Array.h.

template<class E, class INDEX = int>
E* ogdf::Array< E, INDEX >::rbegin ( )
inline

Returns a pointer to the last element.

Definition at line 168 of file Array.h.

template<class E, class INDEX = int>
const E* ogdf::Array< E, INDEX >::rbegin ( ) const
inline

Returns a pointer to the last element.

Definition at line 171 of file Array.h.

template<class E, class INDEX = int>
E* ogdf::Array< E, INDEX >::rend ( )
inline

Returns a pointer to one before the first element.

Definition at line 174 of file Array.h.

template<class E, class INDEX = int>
const E* ogdf::Array< E, INDEX >::rend ( ) const
inline

Returns a pointer to one before the first element.

Definition at line 177 of file Array.h.

template<class E, class INDEX = int>
void ogdf::Array< E, INDEX >::resize ( INDEX  newSize,
const E &  x 
)
inline

Resizes (enlarges or shrinks) the array to hold newSize elements and sets new elements to x.

Note: address of array entries in memory may change!

Parameters
newSizeis new size of the array
xis the inital value of all new elements.

Definition at line 274 of file Array.h.

template<class E, class INDEX = int>
void ogdf::Array< E, INDEX >::resize ( INDEX  newSize)
inline

Resizes (enlarges or shrinks) the array to hold newSize elements.

Note: address of array entries in memory may change!

Parameters
newSizeis new size of the array

Definition at line 281 of file Array.h.

template<class E, class INDEX = int>
INDEX ogdf::Array< E, INDEX >::size ( ) const
inline

Returns the size (number of elements) of the array.

Definition at line 153 of file Array.h.

template<class E, class INDEX = int>
void ogdf::Array< E, INDEX >::swap ( INDEX  i,
INDEX  j 
)
inline

Swaps the elements at position i and j.

Definition at line 192 of file Array.h.

Friends And Related Function Documentation

template<class E, class INDEX = int>
template<class F , class I >
friend class ArrayBuffer
friend

Definition at line 438 of file Array.h.

Member Data Documentation

template<class E, class INDEX = int>
INDEX ogdf::Array< E, INDEX >::m_high
private

The highest index.

Definition at line 445 of file Array.h.

template<class E, class INDEX = int>
INDEX ogdf::Array< E, INDEX >::m_low
private

The lowest index.

Definition at line 444 of file Array.h.

template<class E, class INDEX = int>
E* ogdf::Array< E, INDEX >::m_pStart
private

The real start of the array (address of A[m_low]).

Definition at line 442 of file Array.h.

template<class E, class INDEX = int>
E* ogdf::Array< E, INDEX >::m_pStop
private

Successor of last element (address of A[m_high+1]).

Definition at line 443 of file Array.h.

template<class E, class INDEX = int>
E* ogdf::Array< E, INDEX >::m_vpStart
private

The virtual start of the array (address of A[0]).

Definition at line 441 of file Array.h.


The documentation for this class was generated from the following file: