source: trunk/yat/utility/Iterator.h @ 966

Last change on this file since 966 was 966, checked in by Peter, 16 years ago

fixing some doxygen warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.5 KB
Line 
1#ifndef _theplu_yat_utility_iterator_
2#define _theplu_yat_utility_iterator_
3
4// $Id: Iterator.h 966 2007-10-11 17:01:01Z peter $
5
6/*
7  Copyright (C) 2007 Peter Johansson
8
9  This file is part of the yat library, http://trac.thep.lu.se/trac/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 2 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "yat_assert.h"
28
29#include <iterator>
30#include <stddef.h>
31#include <stdexcept>
32
33namespace theplu {
34namespace yat {
35namespace utility {
36
37  /**
38     @brief Iterator
39  */
40  template<typename return_type, typename Container>
41  class Iterator
42  {
43  public:
44    typedef std::random_access_iterator_tag iterator_category;
45    typedef double value_type;
46    typedef size_t difference_type;
47    typedef double* pointer;
48    typedef double& reference;
49
50    /**
51       \brief Default Constructor
52    */
53    Iterator(void) {};
54
55    /**
56       \brief Constructor
57
58       \param container iterator points to
59       \param index telling which element iterator points to
60    */
61    Iterator(Container& container, difference_type index)
62      : container_(&container), index_(index) {}
63
64    /**
65       \return element
66     */
67    return_type operator*(void) const 
68    { yat_assert<std::out_of_range>(index_<container_->size());
69      return container_->operator()(index_); }
70
71    /**
72       \return element \a n steps forward
73     */
74    return_type operator[](difference_type n) const 
75    { yat_assert<std::out_of_range>(index_+n < container_->size());
76      return container_->operator()(index_+n); }
77
78    /**
79       \brief pre-increment
80
81       \return reference to *this
82     */
83    Iterator& operator++(void) { ++index_; return *this; }
84
85    /**
86       \brief post-increment
87
88       \return copy of iterator prior increment
89     */
90    Iterator operator++(int) 
91    { Iterator<return_type, Container> tmp(*this); ++index_; return tmp;}
92
93    /**
94       \brief iterate \f$ n \f$ steps forward
95
96       \return reference to resulting iterator
97     */
98    Iterator& operator+=(int n) { index_+=n; return *this; }
99
100    /**
101       \brief post-decrement
102
103       \return copy of iterator prior decrement
104     */
105    Iterator operator--(int) 
106    { Iterator<return_type, Container> tmp(*this); --index_; return tmp;}
107
108    /**
109       \brief pre-decrement
110
111       \return reference to resulting iterator
112     */
113    Iterator& operator--(void) { --index_; return *this; }
114
115    /**
116       \brief iterate \f$ n \f$ steps backwards
117
118       \return reference to resulting iterator
119     */
120    Iterator& operator-=(int n) { index_-=n; return *this; }
121
122    /**
123       \brief addition operator
124
125       \return copy of resulting iterator
126     */
127    friend Iterator operator+(const Iterator& lhs, size_t n) 
128    { return Iterator<return_type, Container>(*lhs.container_, lhs.index_+n); }
129
130    /**
131       \brief subtraction operator
132
133       \return copy of resulting iterator
134     */
135    friend Iterator operator-(const Iterator& lhs, size_t n) 
136    { return Iterator<return_type, Container>(*lhs.container_, lhs.index_-n); }
137
138    /**
139       \brief difference operator
140
141       \return distance between \a lhs and \a rhs
142     */
143    friend difference_type operator-(const Iterator& lhs, const Iterator& rhs) 
144    { return lhs.index_-rhs.index_; }
145
146   
147    /**
148       \brief Conversion operator
149
150       This operator allows automatic conversion from iterator to
151       const_iterator
152     */
153    operator Iterator<const double, const Container> () 
154    { return Iterator<const double, const Container>(*container_, index_); } 
155
156    /**
157       \brief Equality operator
158
159       \return True if \a lhs and \a rhs are pointing to same element
160     */
161    friend bool operator==(const Iterator<return_type, Container>& lhs, 
162                           const Iterator<return_type, Container>& rhs)
163    { return lhs.container_==rhs.container_ && lhs.index_==rhs.index_; }
164   
165    /**
166       \brief Non-equality operator
167
168       \return False if \a lhs and \a rhs are pointing to same element
169     */
170    friend bool operator!=(const Iterator<return_type, Container>& lhs, 
171                           const Iterator<return_type, Container>& rhs)
172    { return !(lhs.container_==rhs.container_ && lhs.index_==rhs.index_); }
173   
174    /**
175       \brief Less operator
176     */
177    friend bool operator<(const Iterator<return_type, Container>& lhs, 
178                           const Iterator<return_type, Container>& rhs)
179    { return lhs.index_<rhs.index_; }
180   
181    /**
182       \brief Less equal operator
183     */
184    friend bool operator<=(const Iterator<return_type, Container>& lhs, 
185                           const Iterator<return_type, Container>& rhs)
186    { return lhs.index_<=rhs.index_; }
187   
188    /**
189       \brief Larger operator
190     */
191    friend bool operator>(const Iterator<return_type, Container>& lhs, 
192                           const Iterator<return_type, Container>& rhs)
193    { return lhs.index_>rhs.index_; }
194   
195    /**
196       \brief Larger equal operator
197     */
198    friend bool operator>=(const Iterator<return_type, Container>& lhs, 
199                           const Iterator<return_type, Container>& rhs)
200    { return lhs.index_>=rhs.index_; }
201   
202  private:
203    Container* container_;
204    size_t index_;
205
206    // Using compiler generated copy
207    //Iterator(const Iterator&);
208    //Iterator& operator=(const Iterator&);
209  };
210
211}}} // of namespace utility, yat, and theplu
212
213#endif
Note: See TracBrowser for help on using the repository browser.