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

Last change on this file since 898 was 898, checked in by Markus Ringnér, 15 years ago

A first suggestion for how to adress #250. Also removed contamination of namespace std (see #251).

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