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

Last change on this file since 884 was 884, checked in by Peter, 14 years ago

some documentation

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.1 KB
Line 
1#ifndef _theplu_yat_utility_iterator_
2#define _theplu_yat_utility_iterator_
3
4// $Id: Iterator.h 884 2007-09-22 22:53:38Z 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 <iterator>
28#include <stddef.h>
29
30namespace theplu {
31namespace yat {
32namespace utility {
33
34  /**
35     @brief Iterator
36  */
37  template<typename return_type, typename Container>
38  class Iterator
39  {
40  public:
41    typedef std::random_access_iterator_tag iterator_category;
42    typedef double value_type;
43    typedef size_t difference_type;
44    typedef double* pointer;
45    typedef double& reference;
46
47    /**
48       \brief Default Constructor
49    */
50    Iterator(void) {};
51
52    /**
53       \brief Constructor
54
55       \param container iterator points to
56       \param index telling which element iterator points to
57    */
58    Iterator(Container& container, difference_type index)
59      : container_(&container), index_(index) {}
60
61    /**
62       \return element
63     */
64    return_type operator*(void) const { return container_->operator()(index_); }
65
66    /**
67       \brief pre-increment
68
69       \return reference to *this
70     */
71    Iterator& operator++(void) { ++index_; return *this; }
72
73    /**
74       \brief post-increment
75
76       \return copy of iterator prior increment
77     */
78    Iterator operator++(int) 
79    { Iterator<return_type, Container> tmp(*this); ++index_; return tmp;}
80
81    /**
82       \brief iterate \f$ n \f$ steps forward
83
84       \return reference to resulting iterator
85     */
86    Iterator& operator+=(int n) { index_+=n; return *this; }
87
88    /**
89       \brief post-decrement
90
91       \return copy of iterator prior decrement
92     */
93    Iterator operator--(int) 
94    { Iterator<return_type, Container> tmp(*this); --index_; return tmp;}
95
96    /**
97       \brief pre-decrement
98
99       \return reference to resulting iterator
100     */
101    Iterator& operator--(void) { --index_; return *this; }
102
103    /**
104       \brief iterate \f$ n \f$ steps backwards
105
106       \return reference to resulting iterator
107     */
108    Iterator& operator-=(int n) { index_-=n; return *this; }
109
110    /**
111       \brief addition operator
112
113       \return copy of resulting iterator
114     */
115    friend Iterator operator+(const Iterator& lhs, size_t n) 
116    { return Iterator<return_type, Container>(*lhs.container_, lhs.index_+n); }
117
118    /**
119       \brief subtraction operator
120
121       \return copy of resulting iterator
122     */
123    friend Iterator operator-(const Iterator& lhs, size_t n) 
124    { return Iterator<return_type, Container>(*lhs.container_, lhs.index_-n); }
125
126    /**
127       \brief difference operator
128
129       \return distance between \a lhs and \a rhs
130     */
131    friend difference_type operator-(const Iterator& lhs, const Iterator& rhs) 
132    { return lhs.index_-rhs.index_; }
133
134   
135    /**
136       \brief Conversion operator
137
138       This operator allows automatic conversion from iterator to
139       const_iterator
140     */
141    operator Iterator<const double, const Container> () 
142    { return Iterator<const double, const Container>(*container_, index_); } 
143
144    /**
145       \brief Equality operator
146
147       \return True if \a lhs and \rhs are pointing to same element
148     */
149    friend bool operator==(const Iterator<return_type, Container>& lhs, 
150                           const Iterator<return_type, Container>& rhs)
151    { return lhs.container_==rhs.container_ && lhs.index_==rhs.index_; }
152   
153    /**
154       \brief Non-equality operator
155
156       \return False if \a lhs and \rhs are pointing to same element
157     */
158    friend bool operator!=(const Iterator<return_type, Container>& lhs, 
159                           const Iterator<return_type, Container>& rhs)
160    { return !(lhs.container_==rhs.container_ && lhs.index_==rhs.index_); }
161   
162    /**
163       \brief Less operator
164     */
165    friend bool operator<(const Iterator<return_type, Container>& lhs, 
166                           const Iterator<return_type, Container>& rhs)
167    { return lhs.index_<rhs.index_; }
168   
169    /**
170       \brief Less equal operator
171     */
172    friend bool operator<=(const Iterator<return_type, Container>& lhs, 
173                           const Iterator<return_type, Container>& rhs)
174    { return lhs.index_<=rhs.index_; }
175   
176    /**
177       \brief Larger operator
178     */
179    friend bool operator>(const Iterator<return_type, Container>& lhs, 
180                           const Iterator<return_type, Container>& rhs)
181    { return lhs.index_>rhs.index_; }
182   
183    /**
184       \brief Larger equal operator
185     */
186    friend bool operator>=(const Iterator<return_type, Container>& lhs, 
187                           const Iterator<return_type, Container>& rhs)
188    { return lhs.index_>=rhs.index_; }
189   
190  private:
191    Container* container_;
192    size_t index_;
193
194    // Using compiler generated copy
195    //Iterator(const Iterator&);
196    //Iterator& operator=(const Iterator&);
197  };
198
199}}} // of namespace utility, yat, and theplu
200
201#endif
Note: See TracBrowser for help on using the repository browser.