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

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

Another try, hm...

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