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>
156 Array(std::size_t num_elem,
const ValueType& value = ValueType()):
157 data(num_elem, value) {}
164 template <
typename InputIter>
165 Array(
const InputIter& first,
const InputIter& last):
223 void resize(std::size_t num_elem,
const ValueType& value = ValueType());
268 void assign(std::size_t num_elem,
const ValueType& value = ValueType());
280 template <
typename InputIter>
281 void assign(
const InputIter& first,
const InputIter& last);
312 void insertElements(std::size_t idx, std::size_t num_elem,
const ValueType& value = ValueType());
330 template <
typename InputIter>
331 void insertElements(std::size_t idx,
const InputIter& first,
const InputIter& last);
341 template <
typename InputIter>
505 void setElement(std::size_t idx,
const ValueType& value = ValueType());
559 void throwIndexError()
const;
560 void throwRangeError()
const;
561 void throwOperationFailed()
const;
585 typedef std::pair<std::size_t, std::size_t>
STPair;
615 template <
typename ValueType>
626 template <
typename ValueType>
637 template <
typename ValueType>
648 template <
typename ValueType>
659 template <
typename ValueType>
670 template <
typename ValueType>
679 template <
typename ValueType>
685 template <
typename ValueType>
691 template <
typename ValueType>
697 template <
typename ValueType>
703 template <
typename ValueType>
709 template <
typename ValueType>
715 template <
typename ValueType>
721 template <
typename ValueType>
724 data.resize(n, value);
727 template <
typename ValueType>
730 return data.capacity();
733 template <
typename ValueType>
736 data.reserve(min_size);
739 template <
typename ValueType>
745 template <
typename ValueType>
748 data.swap(array.data);
751 template <
typename ValueType>
754 data.assign(n, value);
757 template <
typename ValueType>
758 template <
typename InputIter>
761 data.assign(first, last);
764 template <
typename ValueType>
767 data.push_back(value);
770 template <
typename ValueType>
775 data.insert(data.begin() + idx, value);
778 template <
typename ValueType>
780 const ValueType& value)
784 return data.insert(it, value);
787 template <
typename ValueType>
792 data.insert(data.begin() + idx, n, value);
795 template <
typename ValueType>
800 data.insert(it, n, value);
803 template <
typename ValueType>
804 template <
typename InputIter>
809 data.insert(data.begin() + idx, first, last);
812 template <
typename ValueType>
813 template <
typename InputIter>
818 data.insert(it, first, last);
821 template <
typename ValueType>
830 template <
typename ValueType>
835 data.erase(data.begin() + idx);
838 template <
typename ValueType>
843 return data.erase(it);
846 template <
typename ValueType>
854 throw Base::RangeError(std::string(getClassName()) +
": invalid iterator range: first > last");
856 return data.erase(first, last);
859 template <
typename ValueType>
867 template <
typename ValueType>
875 template <
typename ValueType>
883 template <
typename ValueType>
891 template <
typename ValueType>
897 template <
typename ValueType>
903 template <
typename ValueType>
909 template <
typename ValueType>
915 template <
typename ValueType>
921 template <
typename ValueType>
927 template <
typename ValueType>
933 template <
typename ValueType>
939 template <
typename ValueType>
942 return data.rbegin();
945 template <
typename ValueType>
948 return data.rbegin();
951 template <
typename ValueType>
957 template <
typename ValueType>
963 template <
typename ValueType>
971 template <
typename ValueType>
979 template <
typename ValueType>
987 template <
typename ValueType>
995 template <
typename ValueType>
1003 template <
typename ValueType>
1007 throwOperationFailed();
1010 template <
typename ValueType>
1013 if ((allow_end && idx > data.size()) || (!allow_end && idx >= data.size()))
1017 template <
typename ValueType>
1020 if (it < data.begin() || (allow_end && it > data.end()) || (!allow_end && it >= data.end()))
1024 template <
typename ValueType>
1027 if (it < data.begin() || (allow_end && it > data.end()) || (!allow_end && it >= data.end()))
1031 template <
typename ValueType>
1034 throw Base::IndexError(std::string(getClassName()) +
": element index out of bounds");
1037 template <
typename ValueType>
1040 throw Base::RangeError(std::string(getClassName()) +
": invalid iterator");
1043 template <
typename ValueType>
1046 throw Base::OperationFailed(std::string(getClassName()) +
": operation requires non-empty array");
1049 template <
typename ValueType>
1057 template <
typename ValueType>
1063 template <
typename ValueType>
1066 return (array1.getData() != array2.getData());
1069 template <
typename ValueType>
1072 return (array1.getData() <= array2.getData());
1075 template <
typename ValueType>
1078 return (array1.getData() >= array2.getData());
1081 template <
typename ValueType>
1084 return (array1.getData() < array2.getData());
1087 template <
typename ValueType>
1090 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:156
ValueType ElementType
The type of objects stored by the array.
Definition: Array.hpp:105
std::size_t size() const
Returns the number of elements stored in the array.
Definition: Array.hpp:710
void clear()
Erases all elements.
Definition: Array.hpp:740
virtual const char * getClassName() const
Returns the name of the (derived) array class.
Definition: Array.hpp:1050
StorageType::const_iterator ConstElementIterator
A constant random access iterator used to iterate over the elements of the array.
Definition: Array.hpp:125
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:131
StorageType::reverse_iterator ReverseElementIterator
A mutable random access iterator used to iterate over the elements of the array in reverse order.
Definition: Array.hpp:143
void reserve(std::size_t num_elem)
Preallocates memory for (at least) num_elem elements.
Definition: Array.hpp:734
ReverseElementIterator getElementsReverseBegin()
Returns a mutable iterator pointing to the beginning of the reversed array.
Definition: Array.hpp:946
Array(const InputIter &first, const InputIter &last)
Creates and initializes the array with copies of the elements in the range [first,...
Definition: Array.hpp:165
ElementIterator end()
Returns a mutable iterator pointing to the end of the array.
Definition: Array.hpp:934
ConstElementIterator getElementsEnd() const
Returns a constant iterator pointing to the end of the array.
Definition: Array.hpp:904
void checkIndex(std::size_t idx, bool allow_end) const
Definition: Array.hpp:1011
void checkIterator(const ConstElementIterator &it, bool allow_end) const
Definition: Array.hpp:1025
std::size_t getCapacity() const
Returns the number of elements for which memory has been allocated.
Definition: Array.hpp:728
BaseType & getBase()
Returns a non-const reference to itself.
Definition: Array.hpp:692
ConstElementIterator begin() const
Returns a constant iterator pointing to the beginning of the array.
Definition: Array.hpp:916
ConstReverseElementIterator getElementsReverseBegin() const
Returns a constant iterator pointing to the beginning of the reversed array.
Definition: Array.hpp:940
std::vector< ValueType > StorageType
Definition: Array.hpp:95
const StorageType & getData() const
Returns a const reference to the underlying array storage.
Definition: Array.hpp:686
ElementIterator begin()
Returns a mutable iterator pointing to the beginning of the array.
Definition: Array.hpp:922
void checkIterator(const ElementIterator &it, bool allow_end)
Definition: Array.hpp:1018
StorageType::iterator ElementIterator
A mutable random access iterator used to iterate over the elements of the array.
Definition: Array.hpp:137
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:752
void checkIfNonEmpty() const
Definition: Array.hpp:1004
ValueType & operator[](std::size_t idx)
Returns a non-const reference to the element at index idx.
Definition: Array.hpp:996
void removeElement(std::size_t idx)
Removes the element at the position specified by the index idx.
Definition: Array.hpp:831
Array BaseType
Specifies for derived classes the type of the Array base class.
Definition: Array.hpp:119
virtual ~Array()
Virtual destructor.
Definition: Array.hpp:172
ConstElementIterator getElementsBegin() const
Returns a constant iterator pointing to the beginning of the array.
Definition: Array.hpp:892
ConstElementIterator end() const
Returns a constant iterator pointing to the end of the array.
Definition: Array.hpp:928
ValueType & getElement(std::size_t idx)
Returns a non-const reference to the element at index idx.
Definition: Array.hpp:972
const BaseType & getBase() const
Returns a const reference to itself.
Definition: Array.hpp:698
const ValueType & operator[](std::size_t idx) const
Returns a const reference to the element at index idx.
Definition: Array.hpp:988
std::size_t SizeType
The type of objects stored by the array.
Definition: Array.hpp:110
bool isEmpty() const
Tells whether the array is empty (getSize() == 0).
Definition: Array.hpp:716
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:771
void popLastElement()
Removes the last element of the array.
Definition: Array.hpp:822
const ValueType & getLastElement() const
Returns a const reference to the last element of the array.
Definition: Array.hpp:868
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:980
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:788
ElementIterator removeElements(const ElementIterator &first, const ElementIterator &last)
Removes the elements pointed to by the iterators in the range [first, last).
Definition: Array.hpp:847
Array()
Creates an empty array.
Definition: Array.hpp:148
std::size_t getSize() const
Returns the number of elements stored in the array.
Definition: Array.hpp:704
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:759
const ValueType & getElement(std::size_t idx) const
Returns a const reference to the element at index idx.
Definition: Array.hpp:964
ElementIterator getElementsEnd()
Returns a mutable iterator pointing to the end of the array.
Definition: Array.hpp:910
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:722
ConstReverseElementIterator getElementsReverseEnd() const
Returns a constant iterator pointing to the end of the reversed array.
Definition: Array.hpp:952
ValueType & getFirstElement()
Returns a non-const reference to the first element of the array.
Definition: Array.hpp:876
std::shared_ptr< Array > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated Array instances.
Definition: Array.hpp:100
ValueType & getLastElement()
Returns a non-const reference to the last element of the array.
Definition: Array.hpp:884
ReverseElementIterator getElementsReverseEnd()
Returns a mutable iterator pointing to the end of the reversed array.
Definition: Array.hpp:958
StorageType & getData()
Returns a non-const reference to the underlying array storage.
Definition: Array.hpp:680
const ValueType & getFirstElement() const
Returns a const reference to the first element of the array.
Definition: Array.hpp:860
void addElement(const ValueType &value=ValueType())
Inserts a new element at the end of the array.
Definition: Array.hpp:765
void swap(Array &array)
Swaps the contents with array.
Definition: Array.hpp:746
ElementIterator getElementsBegin()
Returns a mutable iterator pointing to the beginning of the array.
Definition: Array.hpp:898
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:595
Array< std::size_t > STArray
Array storing unsigned integers of type std::size_t.
Definition: Array.hpp:575
Array< long > LArray
Array storing integers of type long.
Definition: Array.hpp:580
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:570
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:605
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:590
Array< std::string > SArray
Array storing std::string objects.
Definition: Array.hpp:600
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:585
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.