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

Last change on this file since 1083 was 1083, checked in by Peter, 13 years ago

working on #247

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