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

Last change on this file since 1082 was 1082, checked in by Peter, 14 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 1082 2008-02-13 12:50:45Z 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, value> > 
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,
54                     Policy> 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 row telling which row iterator points to
67       \param colun telling which column iterator points to
68    */
69    Iterator(Container& container, size_t row, size_t column)
70      : container_(&container), index_(row*container.columns()+column) {}
71
72    /**
73       \return data
74    */
75    typename Policy::data_type data(void) const 
76    { yat_assert<std::out_of_range>(index_<this->size(), 
77                                    "Iterator::data"); 
78      return ip_.data(*container_, row(index_), column(index_)); 
79    }
80
81    /**
82       \return weight
83    */
84    typename Policy::weight_type weight(void) const 
85    { yat_assert<std::out_of_range>(index_<this->size(), 
86                                    "Iterator::weight"); 
87      return ip_.weight(*container_, row(index_), column(index_)); 
88    }
89
90    /**
91       \return element
92     */
93    reference operator*(void) const 
94    { 
95      yat_assert<std::out_of_range>(index_ < this->size(), 
96                                    "Iterator::operator*");
97      return ip_.dereference(*container_, row(index_), column(index_));
98    }
99
100    /**
101       \return element \a n steps forward
102     */
103    reference operator[](size_t n) const 
104    { 
105      yat_assert<std::out_of_range>(index_+n < this->size(),
106                                    "Iterator::operator[]");
107      return ip_.dereference(*container_, row(index_+n), column(index_+n));
108    }
109
110    /**
111       \brief pre-increment
112
113       \return reference to *this
114     */
115    Iterator& operator++(void) { ++index_; return *this; }
116
117    /**
118       \brief post-increment
119
120       \return copy of iterator prior increment
121     */
122    Iterator operator++(int) 
123    { self tmp(*this); ++index_; return tmp;}
124
125    /**
126       \brief iterate \f$ n \f$ steps forward
127
128       \return reference to resulting iterator
129     */
130    Iterator& operator+=(int n) { index_+=n; return *this; }
131
132    /**
133       \brief post-decrement
134
135       \return copy of iterator prior decrement
136     */
137    Iterator operator--(int) 
138    { self tmp(*this); --index_; return tmp;}
139
140    /**
141       \brief pre-decrement
142
143       \return reference to resulting iterator
144     */
145    Iterator& operator--(void) { --index_; return *this; }
146
147    /**
148       \brief iterate \f$ n \f$ steps backwards
149
150       \return reference to resulting iterator
151     */
152    Iterator& operator-=(int n) { index_-=n; return *this; }
153
154    /**
155       \brief addition operator
156
157       \return copy of resulting iterator
158     */
159    friend Iterator operator+(const Iterator& lhs, size_t n) 
160    { return self(*lhs.container_, lhs.index_+n); }
161
162    /**
163       \brief subtraction operator
164
165       \return copy of resulting iterator
166     */
167    friend Iterator operator-(const Iterator& lhs, size_t n) 
168    { return self(*lhs.container_, lhs.index_-n); }
169
170    /**
171       \brief difference operator
172
173       \return distance between \a lhs and \a rhs
174     */
175    friend size_t operator-(const Iterator& lhs, const Iterator& rhs) 
176    { 
177      yat_assert<std::runtime_error>(lhs.container_==rhs.container_, 
178                                     "Iterator::operator-");
179      return lhs.index_-rhs.index_; 
180    }
181
182   
183    /**
184       \brief Equality operator
185
186       \return True if \a lhs and \a rhs are pointing to same element
187     */
188    friend bool operator==(const self& lhs, const self& rhs)
189    { 
190      yat_assert<std::runtime_error>(lhs.container_==rhs.container_, 
191                                     "Iterator::operator==");
192      return lhs.index_==rhs.index_; 
193    }
194   
195    /**
196       \brief Non-equality operator
197
198       \return False if \a lhs and \a rhs are pointing to same element
199     */
200    friend bool operator!=(const Iterator& lhs, const Iterator& rhs)
201    { 
202      yat_assert<std::runtime_error>(lhs.container_==rhs.container_, 
203                                     "Iterator::operator!=");
204      return !(lhs==rhs); 
205    }
206   
207    /**
208       \brief Less 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 Less 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    /**
228       \brief Larger operator
229     */
230    friend bool operator>(const self& lhs, const self& rhs)
231    { 
232      yat_assert<std::runtime_error>(lhs.container_==rhs.container_, 
233                                     "Iterator::operator>");
234      return lhs.index_>rhs.index_; 
235    }
236   
237    /**
238       \brief Larger equal operator
239     */
240    friend bool operator>=(const self& lhs, const self& rhs)
241    { 
242      yat_assert<std::runtime_error>(lhs.container_==rhs.container_, 
243                                     "Iterator::operator>=");
244      return lhs.index_>=rhs.index_; 
245    }
246   
247  private:
248    Container* container_;
249    size_t index_;
250    Policy ip_;
251
252    size_t column(size_t i) const 
253    { return i % container_->columns(); }
254    size_t row(size_t i) const 
255    { return static_cast<size_t>(i/container_->columns()); }
256    size_t size() const 
257    { return container_->columns()*container_->rows(); }
258
259
260    // Using compiler generated copy
261    //Iterator(const Iterator&);
262    //Iterator& operator=(const Iterator&);
263  };
264}}} // of namespace utility, yat, and theplu
265
266#endif
Note: See TracBrowser for help on using the repository browser.