//# VectorSTLIterator.h: Random access iterator for Casacore Vectors //# Copyright (C) 2004 //# Associated Universities, Inc. Washington DC, USA. //# //# This library is free software; you can redistribute it and/or modify it //# under the terms of the GNU Library General Public License as published by //# the Free Software Foundation; either version 2 of the License, or (at your //# option) any later version. //# //# This library 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 Library General Public //# License for more details. //# //# You should have received a copy of the GNU Library General Public License //# along with this library; if not, write to the Free Software Foundation, //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. //# //# Correspondence concerning AIPS++ should be addressed as follows: //# Internet email: aips2-request@nrao.edu. //# Postal address: AIPS++ Project Office //# National Radio Astronomy Observatory //# 520 Edgemont Road //# Charlottesville, VA 22903-2475 USA //# //# $Id$ #ifndef CASA_VECTORSTLITERATOR_2_H #define CASA_VECTORSTLITERATOR_2_H //# Includes #include "Vector.h" #include namespace casacore { //# NAMESPACE CASACORE - BEGIN //# Forward Declarations // Casacore Vector iterator // // // // This class creates a random access STL iterator for an Casacore Vector. All // the STL functionality is present (or if something missing can be easily // added).
// The following comments hold: // //
template > class VectorSTLIterator : public std::iterator { public: typedef T value_type; typedef value_type* pointer; typedef const value_type* const_pointer; typedef VectorSTLIterator iterator; typedef const VectorSTLIterator const_iterator; typedef value_type& reference; typedef const value_type& const_reference; typedef std::size_t size_type; typedef ptrdiff_t difference_type; // Constructors. The iterator constructor from a Vector is // the same as if created from Vector.begin(). Copy // constructor and assignment can be the default ones. // explicit VectorSTLIterator(const Vector &c) : start_p(const_cast(c.data())), step_p (std::max(ssize_t(1), c.steps()(0))), iter_p (const_cast(c.data())) {} VectorSTLIterator() : start_p(0), step_p(1), iter_p(0) {} VectorSTLIterator(const typename Array::IteratorSTL &c) : start_p(c.pos()), step_p (std::max(ssize_t(1), c.steps()(0))), iter_p (start_p) {} // // Access // reference operator[](size_t i) { return *(start_p+i*step_p); }; const_reference operator[](size_t i) const { return *(start_p+i*step_p); }; reference operator*() { return *iter_p; }; const_reference operator*() const { return *iter_p; }; pointer pos() const {return iter_p; } // // Manipulation // const iterator &operator++() { iter_p+=step_p; return *this; }; iterator operator++(int) { iterator t = *this; iter_p+=step_p; return t; }; iterator &operator--() { iter_p-=step_p; return *this; }; iterator operator--(int) { VectorSTLIterator t = *this;iter_p-=step_p; return t; }; iterator &operator+=(difference_type i) { iter_p+=i*step_p; return *this; }; iterator &operator-=(difference_type i) { iter_p-=i*step_p; return *this; }; iterator operator+(difference_type i) const { VectorSTLIterator t = *this; return t+=i; }; iterator operator-(difference_type i) const { VectorSTLIterator t = *this; return t-=i; }; // // Size related // difference_type operator-(const VectorSTLIterator &x) const { return (iter_p-x.iter_p)/step_p; }; // // Comparisons // bool operator== (const iterator &other) const { return iter_p == other.iter_p; }; bool operator!= (const iterator other) const { return iter_p != other.iter_p; }; bool operator< (const iterator &other) const { return iter_p < other.iter_p; }; bool operator== (const_pointer const pos) const { return iter_p == pos; }; bool operator!= (const_pointer const pos) const { return iter_p != pos; }; bool operator< (const_pointer const pos) const { return iter_p < pos; }; // protected: // Start (for random indexing) pointer const start_p; // Distance between elements difference_type step_p; // Current element pointer pointer iter_p; }; } //# NAMESPACE CASACORE - END #endif