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

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

median and percentile functions now take iterators rather than vectors

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.4 KB
Line 
1#ifndef _theplu_yat_utility_iterator_
2#define _theplu_yat_utility_iterator_
3
4// $Id: Iterator.h 932 2007-10-05 21:03:46Z peter $
5
6/*
7  Copyright (C) 2007 Peter Johansson
8
9  This file is part of the yat library, http://trac.thep.lu.se/trac/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
32namespace theplu {
33namespace yat {
34namespace utility {
35
36  /**
37     @brief Iterator
38  */
39  template<typename return_type, typename Container>
40  class Iterator
41  {
42  public:
43    typedef std::random_access_iterator_tag iterator_category;
44    typedef double value_type;
45    typedef size_t difference_type;
46    typedef double* pointer;
47    typedef double& reference;
48
49    /**
50       \brief Default Constructor
51    */
52    Iterator(void) {};
53
54    /**
55       \brief Constructor
56
57       \param container iterator points to
58       \param index telling which element iterator points to
59    */
60    Iterator(Container& container, difference_type index)
61      : container_(&container), index_(index) {}
62
63    /**
64       \return element
65     */
66    return_type operator*(void) const 
67    { yat_assert(index_<container_->size());
68      return container_->operator()(index_); }
69
70    /**
71       \return element \a n steps forward
72     */
73    return_type operator[](difference_type n) const 
74    { yat_assert(index_+n < container_->size());
75      return container_->operator()(index_+n); }
76
77    /**
78       \brief pre-increment
79
80       \return reference to *this
81     */
82    Iterator& operator++(void) { ++index_; return *this; }
83
84    /**
85       \brief post-increment
86
87       \return copy of iterator prior increment
88     */
89    Iterator operator++(int) 
90    { Iterator<return_type, Container> tmp(*this); ++index_; return tmp;}
91
92    /**
93       \brief iterate \f$ n \f$ steps forward
94
95       \return reference to resulting iterator
96     */
97    Iterator& operator+=(int n) { index_+=n; return *this; }
98
99    /**
100       \brief post-decrement
101
102       \return copy of iterator prior decrement
103     */
104    Iterator operator--(int) 
105    { Iterator<return_type, Container> tmp(*this); --index_; return tmp;}
106
107    /**
108       \brief pre-decrement
109
110       \return reference to resulting iterator
111     */
112    Iterator& operator--(void) { --index_; return *this; }
113
114    /**
115       \brief iterate \f$ n \f$ steps backwards
116
117       \return reference to resulting iterator
118     */
119    Iterator& operator-=(int n) { index_-=n; return *this; }
120
121    /**
122       \brief addition operator
123
124       \return copy of resulting iterator
125     */
126    friend Iterator operator+(const Iterator& lhs, size_t n) 
127    { return Iterator<return_type, Container>(*lhs.container_, lhs.index_+n); }
128
129    /**
130       \brief subtraction operator
131
132       \return copy of resulting iterator
133     */
134    friend Iterator operator-(const Iterator& lhs, size_t n) 
135    { return Iterator<return_type, Container>(*lhs.container_, lhs.index_-n); }
136
137    /**
138       \brief difference operator
139
140       \return distance between \a lhs and \a rhs
141     */
142    friend difference_type operator-(const Iterator& lhs, const Iterator& rhs) 
143    { return lhs.index_-rhs.index_; }
144
145   
146    /**
147       \brief Conversion operator
148
149       This operator allows automatic conversion from iterator to
150       const_iterator
151     */
152    operator Iterator<const double, const Container> () 
153    { return Iterator<const double, const Container>(*container_, index_); } 
154
155    /**
156       \brief Equality operator
157
158       \return True if \a lhs and \rhs are pointing to same element
159     */
160    friend bool operator==(const Iterator<return_type, Container>& lhs, 
161                           const Iterator<return_type, Container>& rhs)
162    { return lhs.container_==rhs.container_ && lhs.index_==rhs.index_; }
163   
164    /**
165       \brief Non-equality operator
166
167       \return False if \a lhs and \rhs are pointing to same element
168     */
169    friend bool operator!=(const Iterator<return_type, Container>& lhs, 
170                           const Iterator<return_type, Container>& rhs)
171    { return !(lhs.container_==rhs.container_ && lhs.index_==rhs.index_); }
172   
173    /**
174       \brief Less operator
175     */
176    friend bool operator<(const Iterator<return_type, Container>& lhs, 
177                           const Iterator<return_type, Container>& rhs)
178    { return lhs.index_<rhs.index_; }
179   
180    /**
181       \brief Less equal operator
182     */
183    friend bool operator<=(const Iterator<return_type, Container>& lhs, 
184                           const Iterator<return_type, Container>& rhs)
185    { return lhs.index_<=rhs.index_; }
186   
187    /**
188       \brief Larger operator
189     */
190    friend bool operator>(const Iterator<return_type, Container>& lhs, 
191                           const Iterator<return_type, Container>& rhs)
192    { return lhs.index_>rhs.index_; }
193   
194    /**
195       \brief Larger equal operator
196     */
197    friend bool operator>=(const Iterator<return_type, Container>& lhs, 
198                           const Iterator<return_type, Container>& rhs)
199    { return lhs.index_>=rhs.index_; }
200   
201  private:
202    Container* container_;
203    size_t index_;
204
205    // Using compiler generated copy
206    //Iterator(const Iterator&);
207    //Iterator& operator=(const Iterator&);
208  };
209
210}}} // of namespace utility, yat, and theplu
211
212#endif
Note: See TracBrowser for help on using the repository browser.