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

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

working on #267

  • 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 1040 2008-02-06 00:42:53Z 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>
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 typename T::result_type reference;
52    typedef typename T::container_type Container ;
53
54  private:
55    typedef Iterator<T> self;
56
57  public:
58    /**
59       \brief Default Constructor
60    */
61    Iterator(void) {};
62
63    /**
64       \brief Constructor
65
66       \param container iterator points to
67       \param index telling which element iterator points to
68    */
69    Iterator(Container& container, difference_type index)
70      : container_(&container), index_(index) {}
71
72    /**
73       \return element
74     */
75    reference operator*(void) const 
76    { 
77      yat_assert<std::out_of_range>(index_<container_->size(),
78                                    "Iterator::operator*");
79      return container_->operator()(index_); 
80    }
81
82    /**
83       \return element \a n steps forward
84     */
85    reference operator[](difference_type n) const 
86    { 
87      yat_assert<std::out_of_range>(index_+n < container_->size(),
88                                    "Iterator::operator[]");
89      return container_->operator()(index_+n); 
90    }
91
92    /**
93       \brief pre-increment
94
95       \return reference to *this
96     */
97    Iterator& operator++(void) { ++index_; return *this; }
98
99    /**
100       \brief post-increment
101
102       \return copy of iterator prior increment
103     */
104    Iterator operator++(int) 
105    { self tmp(*this); ++index_; return tmp;}
106
107    /**
108       \brief iterate \f$ n \f$ steps forward
109
110       \return reference to resulting iterator
111     */
112    Iterator& operator+=(int n) { index_+=n; return *this; }
113
114    /**
115       \brief post-decrement
116
117       \return copy of iterator prior decrement
118     */
119    Iterator operator--(int) 
120    { self tmp(*this); --index_; return tmp;}
121
122    /**
123       \brief pre-decrement
124
125       \return reference to resulting iterator
126     */
127    Iterator& operator--(void) { --index_; return *this; }
128
129    /**
130       \brief iterate \f$ n \f$ steps backwards
131
132       \return reference to resulting iterator
133     */
134    Iterator& operator-=(int n) { index_-=n; return *this; }
135
136    /**
137       \brief addition operator
138
139       \return copy of resulting iterator
140     */
141    friend Iterator operator+(const Iterator& lhs, size_t n) 
142    { return self(*lhs.container_, lhs.index_+n); }
143
144    /**
145       \brief subtraction operator
146
147       \return copy of resulting iterator
148     */
149    friend Iterator operator-(const Iterator& lhs, size_t n) 
150    { return self(*lhs.container_, lhs.index_-n); }
151
152    /**
153       \brief difference operator
154
155       \return distance between \a lhs and \a rhs
156     */
157    friend difference_type operator-(const Iterator& lhs, const Iterator& rhs) 
158    { return lhs.index_-rhs.index_; }
159
160   
161    /**
162       \brief Equality operator
163
164       \return True if \a lhs and \a rhs are pointing to same element
165     */
166    friend bool operator==(const self& lhs, const self& rhs)
167    { return lhs.container_==rhs.container_ && lhs.index_==rhs.index_; }
168   
169    /**
170       \brief Non-equality operator
171
172       \return False if \a lhs and \a rhs are pointing to same element
173     */
174    friend bool operator!=(const Iterator& lhs, 
175                           const Iterator& rhs)
176    { return !(lhs.container_==rhs.container_ && lhs.index_==rhs.index_); }
177   
178    /**
179       \brief Less operator
180     */
181    friend bool operator<(const self& lhs, const self& rhs)
182    { return lhs.index_<rhs.index_; }
183   
184    /**
185       \brief Less equal operator
186     */
187    friend bool operator<=(const self& lhs, const self& rhs)
188    { return lhs.index_<=rhs.index_; }
189   
190    /**
191       \brief Larger operator
192     */
193    friend bool operator>(const self& lhs, 
194                           const self& rhs)
195    { return lhs.index_>rhs.index_; }
196   
197    /**
198       \brief Larger equal operator
199     */
200    friend bool operator>=(const self& lhs, const self& rhs)
201    { return lhs.index_>=rhs.index_; }
202   
203  private:
204    Container* container_;
205    size_t index_;
206
207    // Using compiler generated copy
208    //Iterator(const Iterator&);
209    //Iterator& operator=(const Iterator&);
210  };
211}}} // of namespace utility, yat, and theplu
212
213#endif
Note: See TracBrowser for help on using the repository browser.