Chemical Data Processing Library C++ API - Version 1.1.1
|
A dynamic array class providing amortized constant time access to arbitrary elements. More...
#include <Array.hpp>
Public Types | |
typedef std::vector< ValueType > | StorageType |
typedef std::shared_ptr< Array > | SharedPointer |
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... | |
StorageType & | getData () |
const StorageType & | getData () const |
BaseType & | getBase () |
Returns a non-const reference to itself. More... | |
const BaseType & | getBase () 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... | |
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.
ValueType | The type of objects stored by the array. |
typedef std::vector<ValueType> CDPL::Util::Array< ValueType >::StorageType |
typedef std::shared_ptr<Array> CDPL::Util::Array< ValueType >::SharedPointer |
A reference-counted smart pointer [SHPTR] for dynamically allocated Array
instances.
typedef ValueType CDPL::Util::Array< ValueType >::ElementType |
The type of objects stored by the array.
typedef std::size_t CDPL::Util::Array< ValueType >::SizeType |
The type of objects stored by the array.
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.
typedef StorageType::const_iterator CDPL::Util::Array< ValueType >::ConstElementIterator |
A constant random access iterator used to iterate over the elements of the array.
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.
typedef StorageType::iterator CDPL::Util::Array< ValueType >::ElementIterator |
A mutable random access iterator used to iterate over the elements of the array.
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.
|
inline |
Creates an empty array.
|
inline |
Creates and initializes the array with num_elem copies of value.
num_elem | The size of the created array. |
value | The value with which each element of the array will be initialized. |
|
inline |
Creates and initializes the array with copies of the elements in the range [first, last).
first | The start of the range. |
last | The end of the range. |
|
inlinevirtual |
Virtual destructor.
CDPL::Util::Array< ValueType >::StorageType & CDPL::Util::Array< ValueType >::getData |
const CDPL::Util::Array< ValueType >::StorageType & CDPL::Util::Array< ValueType >::getData |
CDPL::Util::Array< ValueType > & CDPL::Util::Array< ValueType >::getBase |
Returns a non-const
reference to itself.
const
reference to itself. const CDPL::Util::Array< ValueType > & CDPL::Util::Array< ValueType >::getBase |
Returns a const
reference to itself.
const
reference to itself. std::size_t CDPL::Util::Array< ValueType >::getSize |
Returns the number of elements stored in the array.
std::size_t CDPL::Util::Array< ValueType >::size |
Returns the number of elements stored in the array.
bool CDPL::Util::Array< ValueType >::isEmpty |
Tells whether the array is empty (getSize() == 0).
true
if the array is empty, false
otherwise. 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.
num_elem | The new size. |
value | The value for newly inserted elements. |
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.
num_elem | The number of elements to reserve memory for. |
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.
void CDPL::Util::Array< ValueType >::clear |
Erases all elements.
void CDPL::Util::Array< ValueType >::swap | ( | Array< ValueType > & | array | ) |
Swaps the contents with array.
array | The other array with which the contents will be exchanged. |
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.
num_elem | The number of elements to be assigned. |
value | The value to be assigned. |
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.
first | The start of the range. |
last | The end of the range. |
void CDPL::Util::Array< ValueType >::addElement | ( | const ValueType & | value = ValueType() | ) |
Inserts a new element at the end of the array.
value | The value of the new element. |
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.
idx | The location where to insert the new element. |
value | The value of the element to insert. |
Base::IndexError | if the array is empty or idx is not in the range [0, getSize()]. |
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.
it | The location where to insert the new element. |
value | The value of the element to insert. |
Base::RangeError | if it is not in the range [getElementsBegin(), getElementsEnd()]. |
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.
idx | The location where to insert the new elements. |
num_elem | The number of elements to insert. |
value | The value of the elements to insert. |
Base::IndexError | if idx is not in the range [0, getSize()]. |
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.
it | The location where to insert the new elements. |
num_elem | The number of elements to insert. |
value | The value of the elements to insert. |
Base::RangeError | if it is not in the range [getElementsBegin(), getElementsEnd()]. |
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.
idx | The location where to insert the new elements. |
first | The start of the range. |
last | The end of the range. |
Base::IndexError | if idx is not in the range [0, getSize()]. |
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.
it | The location where to insert the new elements. |
first | The start of the range. |
last | The end of the range. |
Base::RangeError | if it is not in the range [getElementsBegin(), getElementsEnd()]. |
void CDPL::Util::Array< ValueType >::popLastElement |
Removes the last element of the array.
Base::OperationFailed | if the array is empty. |
void CDPL::Util::Array< ValueType >::removeElement | ( | std::size_t | idx | ) |
Removes the element at the position specified by the index idx.
idx | The zero-based index of the element to remove. |
Base::IndexError | if the array is empty or idx is not in the range [0, getSize() - 1]. |
CDPL::Util::Array< ValueType >::ElementIterator CDPL::Util::Array< ValueType >::removeElement | ( | const ElementIterator & | it | ) |
Removes the element at the position specified by the iterator it.
it | The iterator specifying the element to remove. |
Base::RangeError | if the array is empty or it is not in the range [getElementsBegin(), getElementsEnd() - 1]. |
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).
first | The start of the range. |
last | The end of the range. |
Base::RangeError | if first or last is not in the range [getElementsBegin(), getElementsEnd()] or last < first. |
const ValueType & CDPL::Util::Array< ValueType >::getFirstElement |
Returns a const
reference to the first element of the array.
const
reference to the first element. Base::OperationFailed | if the array is empty. |
const ValueType & CDPL::Util::Array< ValueType >::getLastElement |
Returns a const
reference to the last element of the array.
const
reference to the last element. Base::OperationFailed | if the array is empty. |
ValueType & CDPL::Util::Array< ValueType >::getFirstElement |
Returns a non-const
reference to the first element of the array.
const
reference to the first element. Base::OperationFailed | if the array is empty. |
ValueType & CDPL::Util::Array< ValueType >::getLastElement |
Returns a non-const
reference to the last element of the array.
const
reference to the last element. Base::OperationFailed | if the array is empty. |
CDPL::Util::Array< ValueType >::ConstElementIterator CDPL::Util::Array< ValueType >::getElementsBegin |
Returns a constant iterator pointing to the beginning of the array.
CDPL::Util::Array< ValueType >::ElementIterator CDPL::Util::Array< ValueType >::getElementsBegin |
Returns a mutable iterator pointing to the beginning of the array.
CDPL::Util::Array< ValueType >::ConstElementIterator CDPL::Util::Array< ValueType >::getElementsEnd |
Returns a constant iterator pointing to the end of the array.
CDPL::Util::Array< ValueType >::ElementIterator CDPL::Util::Array< ValueType >::getElementsEnd |
Returns a mutable iterator pointing to the end of the array.
CDPL::Util::Array< ValueType >::ConstElementIterator CDPL::Util::Array< ValueType >::begin |
Returns a constant iterator pointing to the beginning of the array.
CDPL::Util::Array< ValueType >::ElementIterator CDPL::Util::Array< ValueType >::begin |
Returns a mutable iterator pointing to the beginning of the array.
CDPL::Util::Array< ValueType >::ConstElementIterator CDPL::Util::Array< ValueType >::end |
Returns a constant iterator pointing to the end of the array.
CDPL::Util::Array< ValueType >::ElementIterator CDPL::Util::Array< ValueType >::end |
Returns a mutable iterator pointing to the end of the array.
CDPL::Util::Array< ValueType >::ConstReverseElementIterator CDPL::Util::Array< ValueType >::getElementsReverseBegin |
Returns a constant iterator pointing to the beginning of the reversed array.
CDPL::Util::Array< ValueType >::ReverseElementIterator CDPL::Util::Array< ValueType >::getElementsReverseBegin |
Returns a mutable iterator pointing to the beginning of the reversed array.
CDPL::Util::Array< ValueType >::ConstReverseElementIterator CDPL::Util::Array< ValueType >::getElementsReverseEnd |
Returns a constant iterator pointing to the end of the reversed array.
CDPL::Util::Array< ValueType >::ReverseElementIterator CDPL::Util::Array< ValueType >::getElementsReverseEnd |
Returns a mutable iterator pointing to the end of the reversed array.
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.
idx | The zero-based index of the element. |
const
reference to the element. Base::IndexError | if the array is empty or idx is not in the range [0, getSize() - 1]. |
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).
idx | The zero-based index of the element. |
const
reference to the element. Base::IndexError | if the array is empty or idx is not in the range [0, getSize() - 1]. |
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.
idx | The zero-based index of the element for which to assign a new value. |
value | The new value of the element after assignment. |
Base::IndexError | if the array is empty or idx is not in the range [0, getSize() - 1]. |
const ValueType & CDPL::Util::Array< ValueType >::operator[] | ( | std::size_t | idx | ) | const |
Returns a const
reference to the element at index idx.
idx | The zero-based index of the element. |
const
reference to the element. Base::IndexError | if the array is empty or idx is not in the range [0, getSize() - 1]. |
ValueType & CDPL::Util::Array< ValueType >::operator[] | ( | std::size_t | idx | ) |
Returns a non-const
reference to the element at index idx.
idx | The zero-based index of the element. |
const
reference to the element. Base::IndexError | if the array is empty or idx is not in the range [0, getSize() - 1]. |
|
protected |
|
protected |
|
protected |
|
protected |
|
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():
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.