// -*- C++ -*- /*************************************************************************** * blitz/array/iter.h Basic iterator for arrays. * * $Id$ * * Copyright (C) 1997-2011 Todd Veldhuizen * * This file is a part of Blitz. * * Blitz is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation, either version 3 * of the License, or (at your option) any later version. * * Blitz is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with Blitz. If not, see . * * Suggestions: blitz-devel@lists.sourceforge.net * Bugs: blitz-support@lists.sourceforge.net * * For more information, please see the Blitz++ Home Page: * https://sourceforge.net/projects/blitz/ * ****************************************************************************/ #ifndef BZ_ARRAY_H #error must be included via #endif #ifndef BZ_ARRAY_ITER_H #define BZ_ARRAY_ITER_H #ifdef BZ_HAVE_STL #include #endif #if defined(BZ_DEBUG) #define CheckIteratorValidity(X,Y) \ BZPRECHECK(data_!=0, X " invalid iterator (empty array)"); \ BZPRECHECK((data_>=beg_+Y && data_<=end_+Y), ((data_ class ConstArrayIterator { private: // Initialization common to begin,end constructors. // void Init(const Array& array) { // Making internal copies of these avoids keeping // a pointer to the array and doing indirection. lbound_ = array.lbound(); order_ = array.ordering(); ubound_(0) = array.ubound(0)+1; dataincr_(order_(0)) = array.stride(order_(0)); for (int i=1,r,s=order_(0);iend_) std::swap(beg_,end_); #endif } public: ConstArrayIterator() : data_(0) { } ConstArrayIterator(const Array& array) : data_(const_cast(array.data())) { Init(array); pos_ = lbound_; } ConstArrayIterator(const Array& array, const int) : data_(end_value(array)) { Init(array); pos_ = array.ubound(); ++pos_(order_(0)); } bool operator==(const ConstArrayIterator& x) const { return data_ == x.data_; } bool operator!=(const ConstArrayIterator& x) const { return data_ != x.data_; } const T& operator*() const { CheckIteratorValidity("Attempted to dereference",0); return *data_; } const T* restrict operator->() const { CheckIteratorValidity("Attempted to dereference",0); return data_; } ConstArrayIterator& operator++(); ConstArrayIterator& operator--(); ConstArrayIterator operator++(int) { ConstArrayIterator tmp = *this; ++(*this); return tmp; } ConstArrayIterator operator--(int) { ConstArrayIterator tmp = *this; --(*this); return tmp; } // get the current position of the Array iterator in index space const TinyVector& position() const { CheckIteratorValidity("Array::iterator::position() called on",0); return pos_; } private: TinyVector dataincr_, lbound_, ubound_, order_; static T* end_value(const Array& array) { T* endval = const_cast(array.data()) + array.stride(array.ordering(0)); for (int i=0;i pos_; T * restrict data_; #if defined(BZ_DEBUG) const T* restrict beg_; const T* restrict end_; #endif }; template class ArrayIterator : public ConstArrayIterator { private: typedef ConstArrayIterator T_base; using T_base::data_; #if defined(BZ_DEBUG) using T_base::beg_; using T_base::end_; #endif public: ArrayIterator() { } ArrayIterator(Array& x) : T_base(x) { } ArrayIterator(const Array& array, const int): T_base(array,0) { } T& operator*() const { CheckIteratorValidity("Attempted to dereference",0); return *data_; } T* restrict operator->() const { CheckIteratorValidity("Attempted to dereference",0); return data_; } ArrayIterator& operator++() { T_base::operator++(); return *this; } ArrayIterator operator++(int) { ArrayIterator tmp = *this; ++(*this); return tmp; } ArrayIterator& operator--() { T_base::operator--(); return *this; } ArrayIterator operator--(int) { ArrayIterator tmp = *this; --(*this); return tmp; } }; template ConstArrayIterator& ConstArrayIterator::operator++() { CheckIteratorValidity("Attempted to increment",0); // The first loop iteration is peeled as it increases performance. // The same improvement can be obtained by telling the compiler that // the test is likely to be true, but this has too many portability issues // for now. // With a compiler peeling loops correctly (or with an effective BZ_LIKELY // macro, this could be simply written as: // // for (int i=0;i ConstArrayIterator& ConstArrayIterator::operator--() { CheckIteratorValidity("Attempted to decrement",1); // The first loop iteration is peeled as it increases performance. // The same improvement can be obtained by telling the compiler that // the test is likely to be true, but this has too many portability issues // for now. // With a compiler peeling loops correctly (or with an effective BZ_LIKELY // macro, this could be simply written as: // // for (int i=0;i struct iterator_traits< BZ_BLITZ_SCOPE(ConstArrayIterator) > { typedef bidirectional_iterator_tag iterator_category; typedef T value_type; typedef blitz::diffType difference_type; typedef const T* pointer; typedef const T& reference; }; template struct iterator_traits< BZ_BLITZ_SCOPE(ArrayIterator) > { typedef bidirectional_iterator_tag iterator_category; typedef T value_type; typedef blitz::diffType difference_type; typedef T* pointer; typedef T& reference; }; BZ_NAMESPACE_END #endif // BZ_HAVE_STL #endif // BZ_ARRAY_ITER_H