#ifndef __JTOOLS__JARRAY_ITERATOR__ #define __JTOOLS__JARRAY_ITERATOR__ #include "JTools/JArray.hh" #include "JTools/JAbstractCollection.hh" /** * \author mdejong */ namespace JTOOLS {} namespace JPP { using namespace JTOOLS; } namespace JTOOLS { /** * ND array iterator. */ template struct JArrayIterator : public JArrayIterator { /** * Constructor. * * \param collection abscissa values */ JArrayIterator(const JAbstractCollection& collection) : JArrayIterator(collection) { i = 0; } /** * Check validity of iterator. * * \return true if valid; else false */ operator bool() const { return i != this->collection.getSize() && JArrayIterator::operator bool(); } /** * Get current value. * * \return value */ JArray operator *() const { return JArray(JArrayIterator::operator *(), this->collection.getX(i)); } /** * Prefix increment. * * \return this array sampler */ JArrayIterator& operator ++() { if (++i == this->collection.getSize()) { i = 0; // rewind JArrayIterator::operator ++(); // increment } return *this; } /** * Postfix increment. * * \return previous array sampler */ JArrayIterator operator ++(int) { const JArrayIterator previous(*this); ++(*this); return previous; } private: int i; }; /** * 1D array iterator. */ template struct JArrayIterator<1, T> { /** * Constructor. * * \param collection abscissa values */ JArrayIterator(const JAbstractCollection& collection) : collection(collection) { i = 0; } /** * Check validity of iterator. * * \return true if valid; else false */ operator bool() const { return i != this->collection.getSize(); } /** * Get current value. * * \return value */ JArray<1, double> operator *() const { return JArray<1, double>(this->collection.getX(i)); } /** * Prefix increment. * * \return this array sampler */ JArrayIterator& operator ++() { if (i != this->collection.getSize()) { ++i; } return *this; } /** * Postfix increment. * * \return previous array sampler */ JArrayIterator operator ++(int) { const JArrayIterator previous(*this); ++(*this); return previous; } const JAbstractCollection& collection; private: int i; }; } #endif