29 #ifndef CDPL_UTIL_ARRAY_HPP
30 #define CDPL_UTIL_ARRAY_HPP
41 #ifndef CDPL_UTIL_ARRAY_CHECK_INDEX
42 # ifdef CDPL_UTIL_ARRAY_CHECKS_DISABLE
43 # define CDPL_UTIL_ARRAY_CHECK_INDEX(idx, allow_end)
45 # define CDPL_UTIL_ARRAY_CHECK_INDEX(idx, allow_end) checkIndex(idx, allow_end)
49 #ifndef CDPL_UTIL_ARRAY_CHECK_ITER
50 # ifdef CDPL_UTIL_ARRAY_CHECKS_DISABLE
51 # define CDPL_UTIL_ARRAY_CHECK_ITER(it, allow_end)
53 # define CDPL_UTIL_ARRAY_CHECK_ITER(it, allow_end) checkIterator(it, allow_end)
90 template <
typename ValueType>
159 Array(std::size_t num_elem,
const ValueType& value = ValueType()):
160 data(num_elem, value) {}
167 template <
typename InputIter>
168 Array(
const InputIter& first,
const InputIter& last):
226 void resize(std::size_t num_elem,
const ValueType& value = ValueType());
271 void assign(std::size_t num_elem,
const ValueType& value = ValueType());
283 template <
typename InputIter>
284 void assign(
const InputIter& first,
const InputIter& last);
315 void insertElements(std::size_t idx, std::size_t num_elem,
const ValueType& value = ValueType());
333 template <
typename InputIter>
334 void insertElements(std::size_t idx,
const InputIter& first,
const InputIter& last);
344 template <
typename InputIter>
508 void setElement(std::size_t idx,
const ValueType& value = ValueType());
562 void throwIndexError()
const;
563 void throwRangeError()
const;
564 void throwOperationFailed()
const;
588 typedef std::pair<std::size_t, std::size_t>
STPair;
618 template <
typename ValueType>
629 template <
typename ValueType>
640 template <
typename ValueType>
651 template <
typename ValueType>
662 template <
typename ValueType>
673 template <
typename ValueType>
682 template <
typename ValueType>
688 template <
typename ValueType>
694 template <
typename ValueType>
700 template <
typename ValueType>
706 template <
typename ValueType>
712 template <
typename ValueType>
718 template <
typename ValueType>
724 template <
typename ValueType>
727 data.resize(n, value);
730 template <
typename ValueType>
733 return data.capacity();
736 template <
typename ValueType>
739 data.reserve(min_size);
742 template <
typename ValueType>
748 template <
typename ValueType>
751 data.swap(array.data);
754 template <
typename ValueType>
757 data.assign(n, value);
760 template <
typename ValueType>
761 template <
typename InputIter>
764 data.assign(first, last);
767 template <
typename ValueType>
770 data.push_back(value);
773 template <
typename ValueType>
778 data.insert(data.begin() + idx, value);
781 template <
typename ValueType>
783 const ValueType& value)
787 return data.insert(it, value);
790 template <
typename ValueType>
795 data.insert(data.begin() + idx, n, value);
798 template <
typename ValueType>
803 data.insert(it, n, value);
806 template <
typename ValueType>
807 template <
typename InputIter>
812 data.insert(data.begin() + idx, first, last);
815 template <
typename ValueType>
816 template <
typename InputIter>
821 data.insert(it, first, last);
824 template <
typename ValueType>
833 template <
typename ValueType>
838 data.erase(data.begin() + idx);
841 template <
typename ValueType>
846 return data.erase(it);
849 template <
typename ValueType>
857 throw Base::RangeError(std::string(getClassName()) +
": invalid iterator range: first > last");
859 return data.erase(first, last);
862 template <
typename ValueType>
870 template <
typename ValueType>
878 template <
typename ValueType>
886 template <
typename ValueType>
894 template <
typename ValueType>
900 template <
typename ValueType>
906 template <
typename ValueType>
912 template <
typename ValueType>
918 template <
typename ValueType>
924 template <
typename ValueType>
930 template <
typename ValueType>
936 template <
typename ValueType>
942 template <
typename ValueType>
945 return data.rbegin();
948 template <
typename ValueType>
951 return data.rbegin();
954 template <
typename ValueType>
960 template <
typename ValueType>
966 template <
typename ValueType>
974 template <
typename ValueType>
982 template <
typename ValueType>
990 template <
typename ValueType>
998 template <
typename ValueType>
1006 template <
typename ValueType>
1010 throwOperationFailed();
1013 template <
typename ValueType>
1016 if ((allow_end && idx > data.size()) || (!allow_end && idx >= data.size()))
1020 template <
typename ValueType>
1023 if (it < data.begin() || (allow_end && it > data.end()) || (!allow_end && it >= data.end()))
1027 template <
typename ValueType>
1030 if (it < data.begin() || (allow_end && it > data.end()) || (!allow_end && it >= data.end()))
1034 template <
typename ValueType>
1037 throw Base::IndexError(std::string(getClassName()) +
": element index out of bounds");
1040 template <
typename ValueType>
1043 throw Base::RangeError(std::string(getClassName()) +
": invalid iterator");
1046 template <
typename ValueType>
1049 throw Base::OperationFailed(std::string(getClassName()) +
": operation requires non-empty array");
1052 template <
typename ValueType>
1060 template <
typename ValueType>
1066 template <
typename ValueType>
1069 return (array1.getData() != array2.getData());
1072 template <
typename ValueType>
1075 return (array1.getData() <= array2.getData());
1078 template <
typename ValueType>
1081 return (array1.getData() >= array2.getData());
1084 template <
typename ValueType>
1087 return (array1.getData() < array2.getData());
1090 template <
typename ValueType>
1093 return (array1.getData() > array2.getData());
#define CDPL_UTIL_ARRAY_CHECK_ITER(it, allow_end)
Definition: Array.hpp:53
#define CDPL_UTIL_ARRAY_CHECK_INDEX(idx, allow_end)
Definition: Array.hpp:45
Definition of exception classes.
Declaration of type CDPL::Util::BitSet.
Thrown to indicate that an index is out of range.
Definition: Base/Exceptions.hpp:152
Thrown to indicate that some requested operation has failed (e.g. due to unfulfilled preconditions or...
Definition: Base/Exceptions.hpp:211
Thrown to indicate that a value is out of range.
Definition: Base/Exceptions.hpp:114
Dynamic array class providing amortized constant time access to arbitrary elements.
Definition: Array.hpp:92
Array(std::size_t num_elem, const ValueType &value=ValueType())
Creates and initializes the array with num_elem copies of value.
Definition: Array.hpp:159
ValueType ElementType
The type of objects stored by the array.
Definition: Array.hpp:108
std::size_t size() const
Returns the number of elements stored in the array.
Definition: Array.hpp:713
void clear()
Erases all elements.
Definition: Array.hpp:743
virtual const char * getClassName() const
Returns the name of the (derived) array class.
Definition: Array.hpp:1053
StorageType::const_iterator ConstElementIterator
A constant random access iterator used to iterate over the elements of the array.
Definition: Array.hpp:128
StorageType::const_reverse_iterator ConstReverseElementIterator
A constant random access iterator used to iterate over the elements of the array in reverse order.
Definition: Array.hpp:134
StorageType::reverse_iterator ReverseElementIterator
A mutable random access iterator used to iterate over the elements of the array in reverse order.
Definition: Array.hpp:146
void reserve(std::size_t num_elem)
Preallocates memory for (at least) num_elem elements.
Definition: Array.hpp:737
ReverseElementIterator getElementsReverseBegin()
Returns a mutable iterator pointing to the beginning of the reversed array.
Definition: Array.hpp:949
Array(const InputIter &first, const InputIter &last)
Creates and initializes the array with copies of the elements in the range [first,...
Definition: Array.hpp:168
ElementIterator end()
Returns a mutable iterator pointing to the end of the array.
Definition: Array.hpp:937
ConstElementIterator getElementsEnd() const
Returns a constant iterator pointing to the end of the array.
Definition: Array.hpp:907
void checkIndex(std::size_t idx, bool allow_end) const
Definition: Array.hpp:1014
void checkIterator(const ConstElementIterator &it, bool allow_end) const
Definition: Array.hpp:1028
std::size_t getCapacity() const
Returns the number of elements for which memory has been allocated.
Definition: Array.hpp:731
BaseType & getBase()
Returns a non-const reference to itself.
Definition: Array.hpp:695
ConstElementIterator begin() const
Returns a constant iterator pointing to the beginning of the array.
Definition: Array.hpp:919
ConstReverseElementIterator getElementsReverseBegin() const
Returns a constant iterator pointing to the beginning of the reversed array.
Definition: Array.hpp:943
std::vector< ValueType > StorageType
The type of the std::vector used for the internal storage of the array elements.
Definition: Array.hpp:98
const StorageType & getData() const
Returns a const reference to the underlying array storage.
Definition: Array.hpp:689
ElementIterator begin()
Returns a mutable iterator pointing to the beginning of the array.
Definition: Array.hpp:925
void checkIterator(const ElementIterator &it, bool allow_end)
Definition: Array.hpp:1021
StorageType::iterator ElementIterator
A mutable random access iterator used to iterate over the elements of the array.
Definition: Array.hpp:140
void assign(std::size_t num_elem, const ValueType &value=ValueType())
This function fills the array with num_elem copies of the given value.
Definition: Array.hpp:755
void checkIfNonEmpty() const
Definition: Array.hpp:1007
ValueType & operator[](std::size_t idx)
Returns a non-const reference to the element at index idx.
Definition: Array.hpp:999
void removeElement(std::size_t idx)
Removes the element at the position specified by the index idx.
Definition: Array.hpp:834
Array BaseType
Specifies for derived classes the type of the Array base class.
Definition: Array.hpp:122
virtual ~Array()
Virtual destructor.
Definition: Array.hpp:175
ConstElementIterator getElementsBegin() const
Returns a constant iterator pointing to the beginning of the array.
Definition: Array.hpp:895
ConstElementIterator end() const
Returns a constant iterator pointing to the end of the array.
Definition: Array.hpp:931
ValueType & getElement(std::size_t idx)
Returns a non-const reference to the element at index idx.
Definition: Array.hpp:975
const BaseType & getBase() const
Returns a const reference to itself.
Definition: Array.hpp:701
const ValueType & operator[](std::size_t idx) const
Returns a const reference to the element at index idx.
Definition: Array.hpp:991
std::size_t SizeType
An unsigned integral type used to represent sizes and indices.
Definition: Array.hpp:113
bool isEmpty() const
Tells whether the array is empty (getSize() == 0).
Definition: Array.hpp:719
void insertElement(std::size_t idx, const ValueType &value=ValueType())
Inserts a new element before the location specified by the index idx.
Definition: Array.hpp:774
void popLastElement()
Removes the last element of the array.
Definition: Array.hpp:825
const ValueType & getLastElement() const
Returns a const reference to the last element of the array.
Definition: Array.hpp:871
void setElement(std::size_t idx, const ValueType &value=ValueType())
Assigns a new value to the element specified by the index idx.
Definition: Array.hpp:983
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.
Definition: Array.hpp:791
ElementIterator removeElements(const ElementIterator &first, const ElementIterator &last)
Removes the elements pointed to by the iterators in the range [first, last).
Definition: Array.hpp:850
Array()
Creates an empty array.
Definition: Array.hpp:151
std::size_t getSize() const
Returns the number of elements stored in the array.
Definition: Array.hpp:707
void assign(const InputIter &first, const InputIter &last)
This function fills a vector with copies of the elements in the range [first, last).
Definition: Array.hpp:762
const ValueType & getElement(std::size_t idx) const
Returns a const reference to the element at index idx.
Definition: Array.hpp:967
ElementIterator getElementsEnd()
Returns a mutable iterator pointing to the end of the array.
Definition: Array.hpp:913
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.
Definition: Array.hpp:725
ConstReverseElementIterator getElementsReverseEnd() const
Returns a constant iterator pointing to the end of the reversed array.
Definition: Array.hpp:955
ValueType & getFirstElement()
Returns a non-const reference to the first element of the array.
Definition: Array.hpp:879
std::shared_ptr< Array > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated Array instances.
Definition: Array.hpp:103
ValueType & getLastElement()
Returns a non-const reference to the last element of the array.
Definition: Array.hpp:887
ReverseElementIterator getElementsReverseEnd()
Returns a mutable iterator pointing to the end of the reversed array.
Definition: Array.hpp:961
StorageType & getData()
Returns a non-const reference to the underlying array storage.
Definition: Array.hpp:683
const ValueType & getFirstElement() const
Returns a const reference to the first element of the array.
Definition: Array.hpp:863
void addElement(const ValueType &value=ValueType())
Inserts a new element at the end of the array.
Definition: Array.hpp:768
void swap(Array &array)
Swaps the contents with array.
Definition: Array.hpp:749
ElementIterator getElementsBegin()
Returns a mutable iterator pointing to the beginning of the array.
Definition: Array.hpp:901
boost::transform_iterator< DerefFunc, typename BaseType::ElementIterator, ValueType & > ElementIterator
A mutable random access iterator used to iterate over the pointed-to objects.
Definition: IndirectArray.hpp:114
Array< double > DArray
Array storing floating-point values of type double.
Definition: Array.hpp:598
Array< std::size_t > STArray
Array storing unsigned integers of type std::size_t.
Definition: Array.hpp:578
Array< long > LArray
Array storing integers of type long.
Definition: Array.hpp:583
bool operator<(const Array< ValueType > &array1, const Array< ValueType > &array2)
Less than comparison operator.
Array< unsigned int > UIArray
Array storing unsigned integers of type unsigned int.
Definition: Array.hpp:573
bool operator>=(const Array< ValueType > &array1, const Array< ValueType > &array2)
Greater or equal comparison operator.
Array< BitSet > BitSetArray
Array storing Util::BitSet objects.
Definition: Array.hpp:608
bool operator>(const Array< ValueType > &array1, const Array< ValueType > &array2)
Greater than comparison operator.
Array< STPair > STPairArray
Array storing pairs of unsigned integers of type std::size_t.
Definition: Array.hpp:593
Array< std::string > SArray
Array storing std::string objects.
Definition: Array.hpp:603
bool operator!=(const Array< ValueType > &array1, const Array< ValueType > &array2)
Inequality comparison operator.
std::pair< std::size_t, std::size_t > STPair
Pair of unsigned integers of type std::size_t.
Definition: Array.hpp:588
bool operator<=(const Array< ValueType > &array1, const Array< ValueType > &array2)
Less or equal comparison operator.
bool operator==(const Array< ValueType > &array1, const Array< ValueType > &array2)
Equality comparison operator.
The namespace of the Chemical Data Processing Library.