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

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

going back to previous design in which view and const_view are in different classes. Having them in same didnt work as expected. There is a problem converting vector::iterator to vector::const_iterator. I'll open a separate ticket for this issue.

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