Chemical Data Processing Library C++ API - Version 1.1.1
Public Types | Public Member Functions | Protected Member Functions | List of all members
CDPL::Util::Array< ValueType > Class Template Reference

A dynamic array class providing amortized constant time access to arbitrary elements. More...

#include <Array.hpp>

+ Inheritance diagram for CDPL::Util::Array< ValueType >:

Public Types

typedef std::vector< ValueType > StorageType
 
typedef std::shared_ptr< ArraySharedPointer
 A reference-counted smart pointer [SHPTR] for dynamically allocated Array instances. More...
 
typedef ValueType ElementType
 The type of objects stored by the array. More...
 
typedef std::size_t SizeType
 The type of objects stored by the array. More...
 
typedef Array BaseType
 Specifies for derived classes the type of the Array base class. More...
 
typedef StorageType::const_iterator ConstElementIterator
 A constant random access iterator used to iterate over the elements of the array. More...
 
typedef StorageType::const_reverse_iterator ConstReverseElementIterator
 A constant random access iterator used to iterate over the elements of the array in reverse order. More...
 
typedef StorageType::iterator ElementIterator
 A mutable random access iterator used to iterate over the elements of the array. More...
 
typedef StorageType::reverse_iterator ReverseElementIterator
 A mutable random access iterator used to iterate over the elements of the array in reverse order. More...
 

Public Member Functions

 Array ()
 Creates an empty array. More...
 
 Array (std::size_t num_elem, const ValueType &value=ValueType())
 Creates and initializes the array with num_elem copies of value. More...
 
template<typename InputIter >
 Array (const InputIter &first, const InputIter &last)
 Creates and initializes the array with copies of the elements in the range [first, last). More...
 
virtual ~Array ()
 Virtual destructor. More...
 
StorageTypegetData ()
 
const StorageTypegetData () const
 
BaseTypegetBase ()
 Returns a non-const reference to itself. More...
 
const BaseTypegetBase () const
 Returns a const reference to itself. More...
 
std::size_t getSize () const
 Returns the number of elements stored in the array. More...
 
std::size_t size () const
 Returns the number of elements stored in the array. More...
 
bool isEmpty () const
 Tells whether the array is empty (getSize() == 0). More...
 
void resize (std::size_t num_elem, const ValueType &value=ValueType())
 Inserts or erases elements at the end so that the size becomes num_elem. More...
 
void reserve (std::size_t num_elem)
 Preallocates memory for (at least) num_elem elements. More...
 
std::size_t getCapacity () const
 Returns the number of elements for which memory has been allocated. More...
 
void clear ()
 Erases all elements. More...
 
void swap (Array &array)
 Swaps the contents with array. More...
 
void assign (std::size_t num_elem, const ValueType &value=ValueType())
 This function fills the array with num_elem copies of the given value. More...
 
template<typename InputIter >
void assign (const InputIter &first, const InputIter &last)
 This function fills a vector with copies of the elements in the range [first, last). More...
 
void addElement (const ValueType &value=ValueType())
 Inserts a new element at the end of the array. More...
 
void insertElement (std::size_t idx, const ValueType &value=ValueType())
 Inserts a new element before the location specified by the index idx. More...
 
ElementIterator insertElement (const ElementIterator &it, const ValueType &value=ValueType())
 Inserts a new element before the location specified by the iterator it. More...
 
void insertElements (std::size_t idx, std::size_t num_elem, const ValueType &value=ValueType())
 Inserts num_elem copies of value before the location specified by the index idx. More...
 
void insertElements (const ElementIterator &it, std::size_t num_elem, const ValueType &value=ValueType())
 Inserts num_elem copies of value before the location specified by the iterator it. More...
 
template<typename InputIter >
void insertElements (std::size_t idx, const InputIter &first, const InputIter &last)
 Inserts the range of elements [first, last) before the location specified by the index idx. More...
 
template<typename InputIter >
void insertElements (const ElementIterator &it, const InputIter &first, const InputIter &last)
 Inserts the range of elements [first, last) before the location specified by the iterator it. More...
 
void popLastElement ()
 Removes the last element of the array. More...
 
void removeElement (std::size_t idx)
 Removes the element at the position specified by the index idx. More...
 
ElementIterator removeElement (const ElementIterator &it)
 Removes the element at the position specified by the iterator it. More...
 
ElementIterator removeElements (const ElementIterator &first, const ElementIterator &last)
 Removes the elements pointed to by the iterators in the range [first, last). More...
 
const ValueType & getFirstElement () const
 Returns a const reference to the first element of the array. More...
 
const ValueType & getLastElement () const
 Returns a const reference to the last element of the array. More...
 
ValueType & getFirstElement ()
 Returns a non-const reference to the first element of the array. More...
 
ValueType & getLastElement ()
 Returns a non-const reference to the last element of the array. More...
 
ConstElementIterator getElementsBegin () const
 Returns a constant iterator pointing to the beginning of the array. More...
 
ElementIterator getElementsBegin ()
 Returns a mutable iterator pointing to the beginning of the array. More...
 
ConstElementIterator getElementsEnd () const
 Returns a constant iterator pointing to the end of the array. More...
 
ElementIterator getElementsEnd ()
 Returns a mutable iterator pointing to the end of the array. More...
 
ConstElementIterator begin () const
 Returns a constant iterator pointing to the beginning of the array. More...
 
ElementIterator begin ()
 Returns a mutable iterator pointing to the beginning of the array. More...
 
ConstElementIterator end () const
 Returns a constant iterator pointing to the end of the array. More...
 
ElementIterator end ()
 Returns a mutable iterator pointing to the end of the array. More...
 
ConstReverseElementIterator getElementsReverseBegin () const
 Returns a constant iterator pointing to the beginning of the reversed array. More...
 
ReverseElementIterator getElementsReverseBegin ()
 Returns a mutable iterator pointing to the beginning of the reversed array. More...
 
ConstReverseElementIterator getElementsReverseEnd () const
 Returns a constant iterator pointing to the end of the reversed array. More...
 
ReverseElementIterator getElementsReverseEnd ()
 Returns a mutable iterator pointing to the end of the reversed array. More...
 
const ValueType & getElement (std::size_t idx) const
 Returns a const reference to the element at index idx. More...
 
ValueType & getElement (std::size_t idx)
 Returns a non-const reference to the element at index idx. More...
 
void setElement (std::size_t idx, const ValueType &value=ValueType())
 Assigns a new value to the element specified by the index idx. More...
 
const ValueType & operator[] (std::size_t idx) const
 Returns a const reference to the element at index idx. More...
 
ValueType & operator[] (std::size_t idx)
 Returns a non-const reference to the element at index idx. More...
 

Protected Member Functions

void checkIfNonEmpty () const
 
void checkIndex (std::size_t idx, bool allow_end) const
 
void checkIterator (const ElementIterator &it, bool allow_end)
 
void checkIterator (const ConstElementIterator &it, bool allow_end) const
 
virtual const char * getClassName () const
 Returns the name of the (derived) array class. More...
 

Detailed Description

template<typename ValueType>
class CDPL::Util::Array< ValueType >

A dynamic array class providing amortized constant time access to arbitrary elements.

Array is essentially a wrapper around std::vector and provides similar (but differently named) methods with the same semantics. Therefore, Array also models a sequence that supports random access to elements, constant time insertion and removal of elements at the end, and linear time insertion and removal of elements at the beginning or in the middle. The number of elements in the array may vary dynamically and memory management is done automatically. However, in contrast to a STL vector, Array provides a virtual destructor which makes it safe to use Array as a public base class. Furthermore, element indices and iterators are generally checked for validity which makes data access safer and bugs easier to detect.

Note
Memory will be reallocated automatically if the current capacity of the array is exceeded. Reallocation does not change the size nor does it change the values of any elements in the array. However, reallocation invalidates any iterators that point into the array and accessing an element via an invalid iterator results in undefined behaviour. To avoid a memory reallocation, reserve() can be used to allocate enough memory in advance.
Template Parameters
ValueTypeThe type of objects stored by the array.

Member Typedef Documentation

◆ StorageType

template<typename ValueType >
typedef std::vector<ValueType> CDPL::Util::Array< ValueType >::StorageType

◆ SharedPointer

template<typename ValueType >
typedef std::shared_ptr<Array> CDPL::Util::Array< ValueType >::SharedPointer

A reference-counted smart pointer [SHPTR] for dynamically allocated Array instances.

◆ ElementType

template<typename ValueType >
typedef ValueType CDPL::Util::Array< ValueType >::ElementType

The type of objects stored by the array.

◆ SizeType

template<typename ValueType >
typedef std::size_t CDPL::Util::Array< ValueType >::SizeType

The type of objects stored by the array.

◆ BaseType

template<typename ValueType >
typedef Array CDPL::Util::Array< ValueType >::BaseType

Specifies for derived classes the type of the Array base class.

In this case, the base class is Array itself.

See also
Util::IndirectArray

◆ ConstElementIterator

template<typename ValueType >
typedef StorageType::const_iterator CDPL::Util::Array< ValueType >::ConstElementIterator

A constant random access iterator used to iterate over the elements of the array.

◆ ConstReverseElementIterator

template<typename ValueType >
typedef StorageType::const_reverse_iterator CDPL::Util::Array< ValueType >::ConstReverseElementIterator

A constant random access iterator used to iterate over the elements of the array in reverse order.

◆ ElementIterator

template<typename ValueType >
typedef StorageType::iterator CDPL::Util::Array< ValueType >::ElementIterator

A mutable random access iterator used to iterate over the elements of the array.

◆ ReverseElementIterator

template<typename ValueType >
typedef StorageType::reverse_iterator CDPL::Util::Array< ValueType >::ReverseElementIterator

A mutable random access iterator used to iterate over the elements of the array in reverse order.

Constructor & Destructor Documentation

◆ Array() [1/3]

template<typename ValueType >
CDPL::Util::Array< ValueType >::Array ( )
inline

Creates an empty array.

◆ Array() [2/3]

template<typename ValueType >
CDPL::Util::Array< ValueType >::Array ( std::size_t  num_elem,
const ValueType &  value = ValueType() 
)
inline

Creates and initializes the array with num_elem copies of value.

Parameters
num_elemThe size of the created array.
valueThe value with which each element of the array will be initialized.

◆ Array() [3/3]

template<typename ValueType >
template<typename InputIter >
CDPL::Util::Array< ValueType >::Array ( const InputIter &  first,
const InputIter &  last 
)
inline

Creates and initializes the array with copies of the elements in the range [first, last).

Parameters
firstThe start of the range.
lastThe end of the range.

◆ ~Array()

template<typename ValueType >
virtual CDPL::Util::Array< ValueType >::~Array ( )
inlinevirtual

Virtual destructor.

Member Function Documentation

◆ getData() [1/2]

template<typename ValueType >
CDPL::Util::Array< ValueType >::StorageType & CDPL::Util::Array< ValueType >::getData

◆ getData() [2/2]

template<typename ValueType >
const CDPL::Util::Array< ValueType >::StorageType & CDPL::Util::Array< ValueType >::getData

◆ getBase() [1/2]

template<typename ValueType >
CDPL::Util::Array< ValueType > & CDPL::Util::Array< ValueType >::getBase

Returns a non-const reference to itself.

Returns
A non-const reference to itself.
See also
Util::IndirectArray

◆ getBase() [2/2]

template<typename ValueType >
const CDPL::Util::Array< ValueType > & CDPL::Util::Array< ValueType >::getBase

Returns a const reference to itself.

Returns
A const reference to itself.
See also
Util::IndirectArray

◆ getSize()

template<typename ValueType >
std::size_t CDPL::Util::Array< ValueType >::getSize

Returns the number of elements stored in the array.

Returns
The size of the array.

◆ size()

template<typename ValueType >
std::size_t CDPL::Util::Array< ValueType >::size

Returns the number of elements stored in the array.

Returns
The size of the array.

◆ isEmpty()

template<typename ValueType >
bool CDPL::Util::Array< ValueType >::isEmpty

Tells whether the array is empty (getSize() == 0).

Returns
true if the array is empty, false otherwise.

◆ resize()

template<typename ValueType >
void CDPL::Util::Array< ValueType >::resize ( std::size_t  num_elem,
const ValueType &  value = ValueType() 
)

Inserts or erases elements at the end so that the size becomes num_elem.

Parameters
num_elemThe new size.
valueThe value for newly inserted elements.

◆ reserve()

template<typename ValueType >
void CDPL::Util::Array< ValueType >::reserve ( std::size_t  num_elem)

Preallocates memory for (at least) num_elem elements.

If num_elem is less than or equal to the current capacity, this call has no effect. Otherwise, it is a request for allocation of additional memory. If the request is successful, then the capacity is greater than or equal to num_elem. Otherwise, the capacity is unchanged. In either case, the number of elements will not change.

Parameters
num_elemThe number of elements to reserve memory for.
See also
getCapacity()

◆ getCapacity()

template<typename ValueType >
std::size_t CDPL::Util::Array< ValueType >::getCapacity

Returns the number of elements for which memory has been allocated.

The capacity is always greater than or equal to the number of currently stored elements.

Returns
The current capacity.

◆ clear()

template<typename ValueType >
void CDPL::Util::Array< ValueType >::clear

Erases all elements.

◆ swap()

template<typename ValueType >
void CDPL::Util::Array< ValueType >::swap ( Array< ValueType > &  array)

Swaps the contents with array.

Parameters
arrayThe other array with which the contents will be exchanged.

◆ assign() [1/2]

template<typename ValueType >
void CDPL::Util::Array< ValueType >::assign ( std::size_t  num_elem,
const ValueType &  value = ValueType() 
)

This function fills the array with num_elem copies of the given value.

Note that the assignment completely changes the array and the new size is the same as the number of elements assigned. Old data will be lost.

Parameters
num_elemThe number of elements to be assigned.
valueThe value to be assigned.

◆ assign() [2/2]

template<typename ValueType >
template<typename InputIter >
void CDPL::Util::Array< ValueType >::assign ( const InputIter &  first,
const InputIter &  last 
)

This function fills a vector with copies of the elements in the range [first, last).

Note that the assignment completely changes the array and the new size is the same as the number of elements assigned. Old data will be lost.

Parameters
firstThe start of the range.
lastThe end of the range.

◆ addElement()

template<typename ValueType >
void CDPL::Util::Array< ValueType >::addElement ( const ValueType &  value = ValueType())

Inserts a new element at the end of the array.

Parameters
valueThe value of the new element.

◆ insertElement() [1/2]

template<typename ValueType >
void CDPL::Util::Array< ValueType >::insertElement ( std::size_t  idx,
const ValueType &  value = ValueType() 
)

Inserts a new element before the location specified by the index idx.

Parameters
idxThe location where to insert the new element.
valueThe value of the element to insert.
Exceptions
Base::IndexErrorif the array is empty or idx is not in the range [0, getSize()].

◆ insertElement() [2/2]

template<typename ValueType >
CDPL::Util::Array< ValueType >::ElementIterator CDPL::Util::Array< ValueType >::insertElement ( const ElementIterator it,
const ValueType &  value = ValueType() 
)

Inserts a new element before the location specified by the iterator it.

Parameters
itThe location where to insert the new element.
valueThe value of the element to insert.
Exceptions
Base::RangeErrorif it is not in the range [getElementsBegin(), getElementsEnd()].

◆ insertElements() [1/4]

template<typename ValueType >
void CDPL::Util::Array< ValueType >::insertElements ( std::size_t  idx,
std::size_t  num_elem,
const ValueType &  value = ValueType() 
)

Inserts num_elem copies of value before the location specified by the index idx.

Parameters
idxThe location where to insert the new elements.
num_elemThe number of elements to insert.
valueThe value of the elements to insert.
Exceptions
Base::IndexErrorif idx is not in the range [0, getSize()].

◆ insertElements() [2/4]

template<typename ValueType >
void CDPL::Util::Array< ValueType >::insertElements ( const ElementIterator it,
std::size_t  num_elem,
const ValueType &  value = ValueType() 
)

Inserts num_elem copies of value before the location specified by the iterator it.

Parameters
itThe location where to insert the new elements.
num_elemThe number of elements to insert.
valueThe value of the elements to insert.
Exceptions
Base::RangeErrorif it is not in the range [getElementsBegin(), getElementsEnd()].

◆ insertElements() [3/4]

template<typename ValueType >
template<typename InputIter >
void CDPL::Util::Array< ValueType >::insertElements ( std::size_t  idx,
const InputIter &  first,
const InputIter &  last 
)

Inserts the range of elements [first, last) before the location specified by the index idx.

Parameters
idxThe location where to insert the new elements.
firstThe start of the range.
lastThe end of the range.
Exceptions
Base::IndexErrorif idx is not in the range [0, getSize()].

◆ insertElements() [4/4]

template<typename ValueType >
template<typename InputIter >
void CDPL::Util::Array< ValueType >::insertElements ( const ElementIterator it,
const InputIter &  first,
const InputIter &  last 
)

Inserts the range of elements [first, last) before the location specified by the iterator it.

Parameters
itThe location where to insert the new elements.
firstThe start of the range.
lastThe end of the range.
Exceptions
Base::RangeErrorif it is not in the range [getElementsBegin(), getElementsEnd()].

◆ popLastElement()

template<typename ValueType >
void CDPL::Util::Array< ValueType >::popLastElement

Removes the last element of the array.

Exceptions
Base::OperationFailedif the array is empty.

◆ removeElement() [1/2]

template<typename ValueType >
void CDPL::Util::Array< ValueType >::removeElement ( std::size_t  idx)

Removes the element at the position specified by the index idx.

Parameters
idxThe zero-based index of the element to remove.
Exceptions
Base::IndexErrorif the array is empty or idx is not in the range [0, getSize() - 1].

◆ removeElement() [2/2]

template<typename ValueType >
CDPL::Util::Array< ValueType >::ElementIterator CDPL::Util::Array< ValueType >::removeElement ( const ElementIterator it)

Removes the element at the position specified by the iterator it.

Parameters
itThe iterator specifying the element to remove.
Returns
An iterator pointing to the position of the element immediately following the one that was removed.
Exceptions
Base::RangeErrorif the array is empty or it is not in the range [getElementsBegin(), getElementsEnd() - 1].

◆ removeElements()

template<typename ValueType >
CDPL::Util::Array< ValueType >::ElementIterator CDPL::Util::Array< ValueType >::removeElements ( const ElementIterator first,
const ElementIterator last 
)

Removes the elements pointed to by the iterators in the range [first, last).

Parameters
firstThe start of the range.
lastThe end of the range.
Returns
An iterator pointing to the position of the element immediately following the ones that were removed.
Exceptions
Base::RangeErrorif first or last is not in the range [getElementsBegin(), getElementsEnd()] or last < first.

◆ getFirstElement() [1/2]

template<typename ValueType >
const ValueType & CDPL::Util::Array< ValueType >::getFirstElement

Returns a const reference to the first element of the array.

Returns
A const reference to the first element.
Exceptions
Base::OperationFailedif the array is empty.

◆ getLastElement() [1/2]

template<typename ValueType >
const ValueType & CDPL::Util::Array< ValueType >::getLastElement

Returns a const reference to the last element of the array.

Returns
A const reference to the last element.
Exceptions
Base::OperationFailedif the array is empty.

◆ getFirstElement() [2/2]

template<typename ValueType >
ValueType & CDPL::Util::Array< ValueType >::getFirstElement

Returns a non-const reference to the first element of the array.

Returns
A non-const reference to the first element.
Exceptions
Base::OperationFailedif the array is empty.

◆ getLastElement() [2/2]

template<typename ValueType >
ValueType & CDPL::Util::Array< ValueType >::getLastElement

Returns a non-const reference to the last element of the array.

Returns
A non-const reference to the last element.
Exceptions
Base::OperationFailedif the array is empty.

◆ getElementsBegin() [1/2]

template<typename ValueType >
CDPL::Util::Array< ValueType >::ConstElementIterator CDPL::Util::Array< ValueType >::getElementsBegin

Returns a constant iterator pointing to the beginning of the array.

Returns
A constant iterator pointing to the beginning of the array.

◆ getElementsBegin() [2/2]

template<typename ValueType >
CDPL::Util::Array< ValueType >::ElementIterator CDPL::Util::Array< ValueType >::getElementsBegin

Returns a mutable iterator pointing to the beginning of the array.

Returns
A mutable iterator pointing to the beginning of the array.

◆ getElementsEnd() [1/2]

template<typename ValueType >
CDPL::Util::Array< ValueType >::ConstElementIterator CDPL::Util::Array< ValueType >::getElementsEnd

Returns a constant iterator pointing to the end of the array.

Returns
A constant iterator pointing to the end of the array.

◆ getElementsEnd() [2/2]

template<typename ValueType >
CDPL::Util::Array< ValueType >::ElementIterator CDPL::Util::Array< ValueType >::getElementsEnd

Returns a mutable iterator pointing to the end of the array.

Returns
A mutable iterator pointing to the end of the array.

◆ begin() [1/2]

template<typename ValueType >
CDPL::Util::Array< ValueType >::ConstElementIterator CDPL::Util::Array< ValueType >::begin

Returns a constant iterator pointing to the beginning of the array.

Returns
A constant iterator pointing to the beginning of the array.

◆ begin() [2/2]

template<typename ValueType >
CDPL::Util::Array< ValueType >::ElementIterator CDPL::Util::Array< ValueType >::begin

Returns a mutable iterator pointing to the beginning of the array.

Returns
A mutable iterator pointing to the beginning of the array.

◆ end() [1/2]

template<typename ValueType >
CDPL::Util::Array< ValueType >::ConstElementIterator CDPL::Util::Array< ValueType >::end

Returns a constant iterator pointing to the end of the array.

Returns
A constant iterator pointing to the end of the array.

◆ end() [2/2]

template<typename ValueType >
CDPL::Util::Array< ValueType >::ElementIterator CDPL::Util::Array< ValueType >::end

Returns a mutable iterator pointing to the end of the array.

Returns
A mutable iterator pointing to the end of the array.

◆ getElementsReverseBegin() [1/2]

template<typename ValueType >
CDPL::Util::Array< ValueType >::ConstReverseElementIterator CDPL::Util::Array< ValueType >::getElementsReverseBegin

Returns a constant iterator pointing to the beginning of the reversed array.

Returns
A constant iterator pointing to the beginning of the reversed array.

◆ getElementsReverseBegin() [2/2]

template<typename ValueType >
CDPL::Util::Array< ValueType >::ReverseElementIterator CDPL::Util::Array< ValueType >::getElementsReverseBegin

Returns a mutable iterator pointing to the beginning of the reversed array.

Returns
A mutable iterator pointing to the beginning of the reversed array.

◆ getElementsReverseEnd() [1/2]

template<typename ValueType >
CDPL::Util::Array< ValueType >::ConstReverseElementIterator CDPL::Util::Array< ValueType >::getElementsReverseEnd

Returns a constant iterator pointing to the end of the reversed array.

Returns
A constant iterator pointing to the end of the reversed array.

◆ getElementsReverseEnd() [2/2]

template<typename ValueType >
CDPL::Util::Array< ValueType >::ReverseElementIterator CDPL::Util::Array< ValueType >::getElementsReverseEnd

Returns a mutable iterator pointing to the end of the reversed array.

Returns
A mutable iterator pointing to the end of the reversed array.

◆ getElement() [1/2]

template<typename ValueType >
const ValueType & CDPL::Util::Array< ValueType >::getElement ( std::size_t  idx) const

Returns a const reference to the element at index idx.

The method is equivalent to operator[](std::size_t) const.

Parameters
idxThe zero-based index of the element.
Returns
A const reference to the element.
Exceptions
Base::IndexErrorif the array is empty or idx is not in the range [0, getSize() - 1].

◆ getElement() [2/2]

template<typename ValueType >
ValueType & CDPL::Util::Array< ValueType >::getElement ( std::size_t  idx)

Returns a non-const reference to the element at index idx.

The method is equivalent to operator[](std::size_t).

Parameters
idxThe zero-based index of the element.
Returns
A non-const reference to the element.
Exceptions
Base::IndexErrorif the array is empty or idx is not in the range [0, getSize() - 1].

◆ setElement()

template<typename ValueType >
void CDPL::Util::Array< ValueType >::setElement ( std::size_t  idx,
const ValueType &  value = ValueType() 
)

Assigns a new value to the element specified by the index idx.

Parameters
idxThe zero-based index of the element for which to assign a new value.
valueThe new value of the element after assignment.
Exceptions
Base::IndexErrorif the array is empty or idx is not in the range [0, getSize() - 1].

◆ operator[]() [1/2]

template<typename ValueType >
const ValueType & CDPL::Util::Array< ValueType >::operator[] ( std::size_t  idx) const

Returns a const reference to the element at index idx.

Parameters
idxThe zero-based index of the element.
Returns
A const reference to the element.
Exceptions
Base::IndexErrorif the array is empty or idx is not in the range [0, getSize() - 1].

◆ operator[]() [2/2]

template<typename ValueType >
ValueType & CDPL::Util::Array< ValueType >::operator[] ( std::size_t  idx)

Returns a non-const reference to the element at index idx.

Parameters
idxThe zero-based index of the element.
Returns
A non-const reference to the element.
Exceptions
Base::IndexErrorif the array is empty or idx is not in the range [0, getSize() - 1].

◆ checkIfNonEmpty()

template<typename ValueType >
void CDPL::Util::Array< ValueType >::checkIfNonEmpty
protected

◆ checkIndex()

template<typename ValueType >
void CDPL::Util::Array< ValueType >::checkIndex ( std::size_t  idx,
bool  allow_end 
) const
protected

◆ checkIterator() [1/2]

template<typename ValueType >
void CDPL::Util::Array< ValueType >::checkIterator ( const ElementIterator it,
bool  allow_end 
)
protected

◆ checkIterator() [2/2]

template<typename ValueType >
void CDPL::Util::Array< ValueType >::checkIterator ( const ConstElementIterator it,
bool  allow_end 
) const
protected

◆ getClassName()

template<typename ValueType >
const char * CDPL::Util::Array< ValueType >::getClassName
protectedvirtual

Returns the name of the (derived) array class.

Overriding this method provides an easy way to customize exception messages with the name of a derived class.

Consider the class MyArray which inherits publicly from Array<int> and overrides getClassName():

class MyArray : public CDPL::Util::Array<int>
{
.....
private:
const char* getClassName() const {
return "MyArray";
}
};

If, for example, the index in an element access is out of bounds, the error message of the thrown Base::IndexError will now start with "MyArray: ...." instead of the default "Array: ....". This small difference already makes the message much more informative and the bug easier to locate.

Returns
The name of the class as a C-string.

The documentation for this class was generated from the following file:
CDPL::Util::Array
A dynamic array class providing amortized constant time access to arbitrary elements.
Definition: Array.hpp:92
CDPL::Util::Array::getClassName
virtual const char * getClassName() const
Returns the name of the (derived) array class.
Definition: Array.hpp:1042