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