14 #include <initializer_list> 16 #include <boost/serialization/split_member.hpp> 17 #include <boost/serialization/nvp.hpp> 18 #include <boost/serialization/vector.hpp> 20 namespace duds {
namespace general {
27 struct NddArrayError :
virtual std::exception,
virtual boost::exception { };
96 typedef std::vector<SizeType>
DimVec;
101 typedef std::initializer_list<SizeType>
DimList;
138 DimVec::const_iterator iter = dsize.begin();
140 while (iter != dsize.end()) {
150 array =
new T[elems];
169 array =
new T[elems];
170 T *t = array, *ot = src.
array;
173 for (SizeType i = elems; i > 0; --i, ++t, ++ot) {
208 if (pos.size() != dsize.size()) {
211 typename Dim::const_iterator p = pos.begin();
212 DimVec::const_iterator d = dsize.begin();
213 SizeType index = 0, step = 1;
226 }
while (d != dsize.end());
241 if (dims.size() > 0) {
261 NddArray(
const Dim &dims) : dsize(dims.first(), dims.last()) {
262 if (!dsize.empty()) {
285 elems(ndda.elems), dsize(
std::move(ndda.dsize)) {
287 ndda.array =
nullptr;
323 ndda.array =
nullptr;
345 typename AV::const_iterator src = av.begin();
346 for (; src != av.end(); ++src, ++dest) {
362 template <std::
size_t N>
364 copyFrom< std::array< T, N > >(a);
378 copyFrom< std::vector< T > >(v);
392 template <std::
size_t N>
399 for (std::size_t loop = N; loop; ++src, ++dest, --loop) {
416 template <std::
size_t X, std::
size_t Y>
422 std::size_t x, y = 0;
424 for (x = 0; x < X; ++dest, ++x) {
441 template <std::
size_t N>
443 copyFrom< std::array< T, N > >(a);
458 copyFrom< std::vector< T > >(v);
473 template <std::
size_t N>
491 template <std::
size_t X, std::
size_t Y>
509 template <std::
size_t N>
512 if (dsize.size() != 1) {
516 const T *src = array;
517 typename std::array<T, N>::iterator i = a.begin();
518 std::size_t loop = std::min(elems, a.size());
519 for (; loop; ++i, ++src, --loop) {
534 if (dsize.size() != 1) {
540 const T *src = array;
541 typename std::vector<T>::iterator i = v.begin();
542 for (; i != v.end(); ++i, ++src) {
559 template <std::
size_t N>
562 if (dsize.size() != 1) {
566 const T *src = array;
568 std::size_t loop = std::min(elems, N);
569 for (; loop; ++dest, ++src, --loop) {
586 template <std::
size_t X, std::
size_t Y>
589 if (dsize.size() != 2) {
594 const std::size_t xs = std::min(X, dsize[0]);
595 const std::size_t ys = std::min(Y, dsize[1]);
596 std::size_t x, y = 0;
597 for (; y < ys; ++y) {
599 for (x = 0; x < xs; ++src, ++x) {
620 return const_cast<T&
>(indexArray(pos));
635 return const_cast<T&
>(indexArray<DimList>(pos));
651 return indexArray(pos);
664 return indexArray<DimList>(pos);
679 T &
at(
const Dim &pos) {
682 return const_cast<T&
>(indexArray(pos));
694 T &
at(
const DimList &pos) {
697 return const_cast<T&
>(indexArray<DimList>(pos));
712 const T &
at(
const Dim &pos)
const {
713 return indexArray(pos);
725 const T &
at(
const DimList &pos)
const {
726 return indexArray<DimList>(pos);
760 return array[elems - 1];
772 return array[elems - 1];
821 return array + elems;
834 return array + elems;
847 return array + elems;
856 if ((elems != ndda.
elems) || (dsize != ndda.
dsize)) {
860 T *t = array, *o = ndda.
array;
861 for (SizeType i = elems; i; --i, ++t, ++o) {
876 if ((elems != ndda.
elems) || (dsize != ndda.
dsize)) {
880 T *t = array, *o = ndda.
array;
881 for (SizeType i = elems; i; --i, ++t, ++o) {
901 SizeType
dim(SizeType n)
const {
902 if (n >= dsize.size()) {
910 const DimVec &
dim()
const {
950 if (dims.size() == 0) {
1001 if (dims.size() == 0) {
1013 DimVec uniondim(std::min(dsize.size(), dims.size()));
1014 typename Dim::const_iterator diter = dims.begin();
1020 uniondim[idx] = (std::min(dsize[idx], *diter));
1022 }
while (++idx < uniondim.size());
1025 DimVec spos(numdims(), 0), dpos(na.
numdims());
1027 const T *sitm = array;
1038 if (spos[idx] >= uniondim[idx]) {
1045 }
while (++idx < uniondim.size());
1053 }
else if (idx < uniondim.size()) {
1062 ditm = &na.
at(dpos);
1087 return makeWithNewSize<DimList>(dims);
1110 template <
class Dim>
1112 *
this =
std::move(makeWithNewSize(dims));
1136 *
this =
std::move(makeWithNewSize<DimList>(dims));
1152 dsize.swap(other.
dsize);
1158 friend class boost::serialization::access;
1159 BOOST_SERIALIZATION_SPLIT_MEMBER();
1164 void save(A &a,
const unsigned int)
const {
1166 a & BOOST_SERIALIZATION_NVP(dsize);
1168 if (!dsize.empty()) {
1171 for (SizeType i = elems; i > 0; --i, ++t) {
1172 a & boost::serialization::make_nvp(
"item", *t);
1180 void load(A &a,
const unsigned int) {
1186 a & BOOST_SERIALIZATION_NVP(dsize);
1188 if (!dsize.empty()) {
1194 for (SizeType i = elems; i > 0; --i, ++t) {
1195 a & boost::serialization::make_nvp(
"item", *t);
1217 #endif // #ifndef NDDARRAY_HPP T & front()
The first element of the array.
void copyFrom(const T(&a)[X][Y])
Copies from a two dimensional array type into this array object.
void makeArray()
Computes the new total number of elements and allocates space for the elements.
void copyTo(T(&a)[N]) const
Copies the contents of this object into a one dimensional array.
void swap(NddArray &other)
Swaps array contents.
N-Dimensional Dynamic Array.
NddArray makeWithNewSize(const DimList &dims) const
Makes a new array with a new size and copies elements who's position is within bounds of the new arra...
NddArray makeWithNewSize(const Dim &dims) const
Makes a new array with a new size and copies elements who's position is within bounds of the new arra...
NddArray & operator=(const NddArray &ndda)
Copy assignment; uses the assignment operator of T.
const T & indexArray(const Dim &pos) const
Finds the location in array that holds the element for the given n-dimensional array position and ret...
const T & operator()(const DimList &pos) const
Obtain an element from the array stored at a specific position.
NddArray(const DimVec &dims)
Makes an array of the given size.
DimVec dsize
The lengths of each dimension within the array.
void swap(NddArray< T > &one, NddArray< T > &two)
Makes NddAray meet the requirements of Swappable to assist in using std::swap().
void clear() noexcept
Destroys the contents of the array.
T & back()
The last element of the array.
SizeType numelems() const
Returns the total number of elements within the array.
move_impl move(unsigned int c, unsigned int r)
Display stream manipulator that moves the display cursor to the given location.
void copyTo(std::array< T, N > &a) const
Copies the contents of this object into a std::array.
NddArray(NddArray &&ndda) noexcept
Move constructor; the elements are not copied.
SizeType dim(SizeType n) const
Returns the size of dimension n.
std::initializer_list< SizeType > DimList
A type that can specify the dimensions of the array, or a position of an element. ...
T * array
The array's element storage.
SizeType numdims() const
Returns the number of dimensions in the array.
void load(A &a, const unsigned int)
Boost serialization support.
Base class for exceptions thrown by NddArray.
T & operator()(const Dim &pos)
Obtain an element from the array stored at a specific position.
void copyTo(T(&a)[X][Y]) const
Copies the contents of this object into a two dimensional array.
std::vector< SizeType > DimVec
The type used to store the dimensions of the array.
bool empty() const
True if the array has zero dimensions.
void copyFrom(const T(&a)[N])
Copies from a one dimensional array type into this array object.
const T & back() const
The last element of the array.
A specified dimension is beyond the range of the array.
T & at(const Dim &pos)
Obtain an element from the array stored at a specific position.
const T * cend() const
An iterator (really just a pointer) to one past the last element of the array.
void copyFrom(const AV &av)
Copies from a one dimensional container into this array.
std::size_t SizeType
The type used to store dimension lengths and the total number of elements.
NddArray & operator=(NddArray &&ndda) noexcept
Move assignment; the elements are not copied.
An empty array, one with zero dimensions, cannot be indexed.
void remake(const DimList &dims)
Clears the array and allocates a new one of the given dimensions.
void copyFrom(const std::array< T, N > &a)
Copies from a std::array into this array.
NddArray(const DimList &dims)
Makes an array of the given size.
std::basic_ostream< Char, Traits > & clear(std::basic_ostream< Char, Traits > &os)
Display stream manipulator that clears all text from the display and places the cursor in the upper l...
T * begin()
An iterator (really just a pointer) to the first element of the array.
void copyElements(const NddArray &src)
Alloactes space for and copies the elements of the array.
void save(A &a, const unsigned int) const
Boost serialization support.
NddArray()
Makes an empty array.
SizeType size() const
Returns the number of elements stored in the array.
const T * end() const
An iterator (really just a pointer) to one past the last element of the array.
NddArray(const NddArray &ndda)
Copy constructor; uses the assignment operator of T.
void resize(const DimList &dims)
Resizes the array and keeps elements who's position is within bounds of the new dimensions.
T & operator()(const DimList &pos)
Obtain an element from the array stored at a specific position.
bool operator!=(const NddArray &ndda) const
Inequality operator; uses the inequality operator of T.
void remake(const DimVec &dims)
Clears the array and allocates a new one of the given dimensions.
const DimVec & dim() const
Provides access to the vector containing the array dimensions.
T & at(const DimList &pos)
Obtain an element from the array stored at a specific position.
NddArray & operator=(const std::vector< T > &v)
Copies from a std::vector into this array.
NddArray & operator=(const T(&a)[N])
Copies from a one dimensional array type into this array object.
~NddArray() noexcept
Destructor; destroys the elements of the array.
void resize(const Dim &dims)
Resizes the array and keeps elements who's position is within bounds of the new dimensions.
const T & operator()(const Dim &pos) const
Obtain an element from the array stored at a specific position.
const T & at(const Dim &pos) const
Obtain an element from the array stored at a specific position.
NddArray(const Dim &dims)
Makes an array of the given size.
void copyTo(std::vector< T > &v) const
Copies the contents of this object into a std::vector.
T * end()
An iterator (really just a pointer) to one past the last element of the array.
const T & at(const DimList &pos) const
Obtain an element from the array stored at a specific position.
const T * begin() const
An iterator (really just a pointer) to the first element of the array.
bool operator==(const NddArray &ndda) const
Equality operator; uses the equality operator of T.
NddArray & operator=(const std::array< T, N > &a)
Copies from a std::array into this array.
#define DUDS_THROW_EXCEPTION(x)
Works like BOOST_THROW_EXCEPTION, but includes a stack trace if DUDS_ERRORS_VERBOSE is defined...
A specified position has a different number of dimensions than the array.
A specified dimension is zero.
SizeType elems
Total number of elements; pre-calculated to speed up some operations.
NddArray & operator=(const T(&a)[X][Y])
Copies from a two dimensional array type into this array object.
void copyFrom(const std::vector< T > &v)
Copies from a std::vector into this array.
const T & front() const
The first element of the array.
const T * cbegin() const
An iterator (really just a pointer) to the first element of the array.
const T * const_iterator
Simple const iterator.
T * iterator
Simple iterator.