source: trunk/yat/utility/IteratorWeighted.h @ 909

Last change on this file since 909 was 909, checked in by Peter, 15 years ago

using weighted_iterator_traits, closes #151

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