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

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

some msg when using yat_assert

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