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

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

using std::iterator in Iterator

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.9 KB
Line 
1#ifndef _theplu_yat_utility_iterator_
2#define _theplu_yat_utility_iterator_
3
4// $Id: Iterator.h 1067 2008-02-10 23:09:04Z peter $
5
6/*
7  Copyright (C) 2007, 2008 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  /**
39     @brief Iterator
40  */
41  template<typename Container, typename T> 
42  class Iterator
43    : public std::iterator<std::random_access_iterator_tag, double, size_t, T, 
44                           void>
45  {
46  public:
47    //typedef size_t difference_type;
48    typedef T reference;
49    typedef double* pointer;
50   
51  private:
52    typedef Iterator<Container, T> self;
53
54  public:
55    /**
56       \brief Default Constructor
57    */
58    Iterator(void) {};
59
60    /**
61       \brief Constructor
62
63       \param container iterator points to
64       \param row telling which row iterator points to
65       \param colun telling which column iterator points to
66    */
67    Iterator(Container& container, size_t row, size_t column)
68      : container_(&container), index_(row*container.columns()+column) {}
69
70    /**
71       \return element
72     */
73    reference operator*(void) const 
74    { 
75      yat_assert<std::out_of_range>(index_ < this->size(), 
76                                    "Iterator::operator*");
77      return container_->operator()(this->row(index_), this->column(index_));
78    }
79
80    /**
81       \return element \a n steps forward
82     */
83    reference operator[](size_t n) const 
84    { 
85      yat_assert<std::out_of_range>(index_+n < this->size(),
86                                    "Iterator::operator[]");
87      return container_->operator()(this->row(index_+n), this->column(index_+n));
88    }
89
90    /**
91       \brief pre-increment
92
93       \return reference to *this
94     */
95    Iterator& operator++(void) { ++index_; return *this; }
96
97    /**
98       \brief post-increment
99
100       \return copy of iterator prior increment
101     */
102    Iterator operator++(int) 
103    { self tmp(*this); ++index_; return tmp;}
104
105    /**
106       \brief iterate \f$ n \f$ steps forward
107
108       \return reference to resulting iterator
109     */
110    Iterator& operator+=(int n) { index_+=n; return *this; }
111
112    /**
113       \brief post-decrement
114
115       \return copy of iterator prior decrement
116     */
117    Iterator operator--(int) 
118    { self tmp(*this); --index_; return tmp;}
119
120    /**
121       \brief pre-decrement
122
123       \return reference to resulting iterator
124     */
125    Iterator& operator--(void) { --index_; return *this; }
126
127    /**
128       \brief iterate \f$ n \f$ steps backwards
129
130       \return reference to resulting iterator
131     */
132    Iterator& operator-=(int n) { index_-=n; return *this; }
133
134    /**
135       \brief addition operator
136
137       \return copy of resulting iterator
138     */
139    friend Iterator operator+(const Iterator& lhs, size_t n) 
140    { return self(*lhs.container_, lhs.index_+n); }
141
142    /**
143       \brief subtraction operator
144
145       \return copy of resulting iterator
146     */
147    friend Iterator operator-(const Iterator& lhs, size_t n) 
148    { return self(*lhs.container_, lhs.index_-n); }
149
150    /**
151       \brief difference operator
152
153       \return distance between \a lhs and \a rhs
154     */
155    friend size_t operator-(const Iterator& lhs, const Iterator& rhs) 
156    { 
157      yat_assert<std::runtime_error>(lhs.container_==rhs.container_, 
158                                     "Iterator::operator-");
159      return lhs.index_-rhs.index_; 
160    }
161
162   
163    /**
164       \brief Equality operator
165
166       \return True if \a lhs and \a rhs are pointing to same element
167     */
168    friend bool operator==(const self& lhs, const self& rhs)
169    { 
170      yat_assert<std::runtime_error>(lhs.container_==rhs.container_, 
171                                     "Iterator::operator==");
172      return lhs.index_==rhs.index_; 
173    }
174   
175    /**
176       \brief Non-equality operator
177
178       \return False if \a lhs and \a rhs are pointing to same element
179     */
180    friend bool operator!=(const Iterator& lhs, const Iterator& rhs)
181    { 
182      yat_assert<std::runtime_error>(lhs.container_==rhs.container_, 
183                                     "Iterator::operator!=");
184      return !(lhs==rhs); 
185    }
186   
187    /**
188       \brief Less operator
189     */
190    friend bool operator<(const self& lhs, const self& rhs)
191    { 
192      yat_assert<std::runtime_error>(lhs.container_==rhs.container_, 
193                                     "Iterator::operator<");
194      return lhs.index_<rhs.index_; 
195    }
196   
197    /**
198       \brief Less equal operator
199     */
200    friend bool operator<=(const self& lhs, const self& rhs)
201    { 
202      yat_assert<std::runtime_error>(lhs.container_==rhs.container_, 
203                                     "Iterator::operator<=");
204      return lhs.index_<=rhs.index_; 
205    }
206   
207    /**
208       \brief Larger operator
209     */
210    friend bool operator>(const self& lhs, const self& rhs)
211    { 
212      yat_assert<std::runtime_error>(lhs.container_==rhs.container_, 
213                                     "Iterator::operator>");
214      return lhs.index_>rhs.index_; 
215    }
216   
217    /**
218       \brief Larger equal operator
219     */
220    friend bool operator>=(const self& lhs, const self& rhs)
221    { 
222      yat_assert<std::runtime_error>(lhs.container_==rhs.container_, 
223                                     "Iterator::operator>=");
224      return lhs.index_>=rhs.index_; 
225    }
226   
227  private:
228    Container* container_;
229    size_t index_;
230
231    size_t column(size_t i) const 
232    { return i % container_->columns(); }
233    size_t row(size_t i) const 
234    { return static_cast<size_t>(i/container_->columns()); }
235    size_t size() const 
236    { return container_->columns()*container_->rows(); }
237
238    // Using compiler generated copy
239    //Iterator(const Iterator&);
240    //Iterator& operator=(const Iterator&);
241  };
242}}} // of namespace utility, yat, and theplu
243
244#endif
Note: See TracBrowser for help on using the repository browser.