casa
$Rev:20696$
|
00001 //# VectorSTLIterator.h: Random access iterator for aips++ Vectors 00002 //# Copyright (C) 2004 00003 //# Associated Universities, Inc. Washington DC, USA. 00004 //# 00005 //# This library is free software; you can redistribute it and/or modify it 00006 //# under the terms of the GNU Library General Public License as published by 00007 //# the Free Software Foundation; either version 2 of the License, or (at your 00008 //# option) any later version. 00009 //# 00010 //# This library is distributed in the hope that it will be useful, but WITHOUT 00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 //# License for more details. 00014 //# 00015 //# You should have received a copy of the GNU Library General Public License 00016 //# along with this library; if not, write to the Free Software Foundation, 00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 00018 //# 00019 //# Correspondence concerning AIPS++ should be addressed as follows: 00020 //# Internet email: aips2-request@nrao.edu. 00021 //# Postal address: AIPS++ Project Office 00022 //# National Radio Astronomy Observatory 00023 //# 520 Edgemont Road 00024 //# Charlottesville, VA 22903-2475 USA 00025 //# 00026 //# $Id: VectorSTLIterator.h 20935 2010-08-17 14:01:02Z gervandiepen $ 00027 00028 #ifndef CASA_VECTORSTLITERATOR_H 00029 #define CASA_VECTORSTLITERATOR_H 00030 00031 //# Includes 00032 #include <casa/aips.h> 00033 #include <casa/Arrays/Vector.h> 00034 #include <iterator> 00035 00036 namespace casa { //# NAMESPACE CASA - BEGIN 00037 00038 //# Forward Declarations 00039 00040 // <summary> AIPS++ Vector iterator </summary> 00041 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos=""> 00042 // </reviewed> 00043 // <synopsis> 00044 // This class creates a random access STL iterator for an aips++ Vector. All 00045 // the STL functionality is present (or if something missing can be easily 00046 // added). <br> 00047 // The following comments hold: 00048 // <ul> 00049 // <li> A VectorSTLIterator can be created from a <src>Vector</src> (non-STL) 00050 // It is the same as using <src>Vector.begin()</src> 00051 // <li> No contiguous iterator is provided. Its construction is not necessary, 00052 // since <src>Vector.data()</src> is a fully functional STL iterator already. 00053 // <li> This iterator is non-intrusive. Since it needs state (the 'step') 00054 // nothing substantial is gained by having it included in the Vector class. 00055 // The major gain would be to be able to use the standard nomenclature: 00056 // <src>Vector::iterator()</src> rather than <src>VectorSTLiterator</src> 00057 // <li> It would be advisable, and easy to implement, to add a template argument 00058 // to the <src>Array</src> classes: <src><class T, Bool isCont=True></src>. The 00059 // default is contiguous, since creation is contiguous. In that case correct 00060 // iterators for e.g. contiguous arrays are supplied automatically. 00061 // <li> needs probably some 'const' fine tuning; and the <src>-></src> operator 00062 // </ul> 00063 // </synopsis> 00064 00065 template <class T> 00066 class VectorSTLIterator 00067 : public std::iterator<std::random_access_iterator_tag, T> { 00068 public: 00069 typedef T value_type; 00070 typedef value_type* pointer; 00071 typedef const value_type* const_pointer; 00072 typedef VectorSTLIterator<T> iterator; 00073 typedef const VectorSTLIterator<T> const_iterator; 00074 typedef value_type& reference; 00075 typedef const value_type& const_reference; 00076 typedef std::size_t size_type; 00077 typedef ptrdiff_t difference_type; 00078 // Constructors. The iterator constructor from a <src>Vector</src> is 00079 // the same as if created from <src>Vector.begin()</src>. Copy 00080 // constructor and assignment can be the default ones. 00081 // <group> 00082 explicit VectorSTLIterator(const Vector<T> &c) 00083 : start_p(const_cast<T*>(c.data())), 00084 step_p (std::max(ssize_t(1), c.steps()(0))), 00085 iter_p (const_cast<T*>(c.data())) 00086 {} 00087 VectorSTLIterator() : start_p(0), step_p(1), iter_p(0) 00088 {} 00089 VectorSTLIterator(const typename Array<T>::IteratorSTL &c) 00090 : start_p(c.pos()), 00091 step_p (std::max(ssize_t(1), c.steps()(0))), 00092 iter_p (start_p) 00093 {} 00094 // Copy constructor. 00095 //# It is certainly needed for the Intel compiler. 00096 VectorSTLIterator(const VectorSTLIterator<T>& that) 00097 : std::iterator<std::random_access_iterator_tag, T>(that), 00098 start_p(that.start_p), step_p(that.step_p), 00099 iter_p(that.iter_p) 00100 {} 00101 // Assignment. 00102 //# It is certainly needed for the Intel compiler. 00103 VectorSTLIterator<T>& operator=(const VectorSTLIterator<T>& that) 00104 { 00105 if (this != &that) { 00106 std::iterator<std::random_access_iterator_tag, T>::operator=(that); 00107 start_p = that.start_p; 00108 step_p = that.step_p; 00109 iter_p = that.iter_p; 00110 } 00111 return *this; 00112 } 00113 // </group> 00114 // Destructor 00115 ~VectorSTLIterator() {;} 00116 // Access 00117 // <group> 00118 reference operator[](uInt i) { return *(start_p+i*step_p); }; 00119 const_reference operator[](uInt i) const { 00120 return *(start_p+i*step_p); }; 00121 reference operator*() { return *iter_p; }; 00122 const_reference operator*() const { return *iter_p; }; 00123 pointer pos() const {return iter_p; } 00124 // </group> 00125 // Manipulation 00126 // <group> 00127 const iterator &operator++() { iter_p+=step_p; return *this; }; 00128 iterator operator++(int) { 00129 iterator t = *this; iter_p+=step_p; return t; }; 00130 iterator &operator--() { iter_p-=step_p; return *this; }; 00131 iterator operator--(int) { 00132 VectorSTLIterator<T> t = *this;iter_p-=step_p; return t; }; 00133 iterator &operator+=(difference_type i) { 00134 iter_p+=i*step_p; return *this; }; 00135 iterator &operator-=(difference_type i) { 00136 iter_p-=i*step_p; return *this; }; 00137 iterator operator+(difference_type i) const { 00138 VectorSTLIterator<T> t = *this; return t+=i; }; 00139 iterator operator-(difference_type i) const { 00140 VectorSTLIterator<T> t = *this; return t-=i; }; 00141 // </group> 00142 // Size related 00143 // <group> 00144 difference_type operator-(const VectorSTLIterator<T> &x) const { 00145 return (iter_p-x.iter_p)/step_p; }; 00146 // </group> 00147 // Comparisons 00148 // <group> 00149 Bool operator== (const iterator &other) const { 00150 return iter_p == other.iter_p; }; 00151 Bool operator!= (const iterator other) const { 00152 return iter_p != other.iter_p; }; 00153 Bool operator< (const iterator &other) const { 00154 return iter_p < other.iter_p; }; 00155 Bool operator== (const_pointer const pos) const { 00156 return iter_p == pos; }; 00157 Bool operator!= (const_pointer const pos) const { 00158 return iter_p != pos; }; 00159 Bool operator< (const_pointer const pos) const { 00160 return iter_p < pos; }; 00161 // </group> 00162 protected: 00163 // Start (for random indexing) 00164 pointer const start_p; 00165 // Distance between elements 00166 difference_type step_p; 00167 // Current element pointer 00168 pointer iter_p; 00169 }; 00170 00171 00172 } //# NAMESPACE CASA - END 00173 00174 #endif