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

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

refs #247, #252, and #267

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