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

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

reimplementing yat_assert as a throwing function

  • 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 936 2007-10-05 23:02:08Z peter $
5
6#include "yat_assert.h"
7
8#include <iterator>
9#include <stddef.h>
10#include <stdexcept>
11
12namespace theplu {
13namespace yat {
14namespace utility {
15
16  /**
17     @brief IteratorWeighted
18  */
19  template<typename return_type, typename Container>
20  class IteratorWeighted
21  {
22  public:
23    typedef std::random_access_iterator_tag iterator_category;
24    typedef double value_type;
25    typedef size_t difference_type;
26    typedef double* pointer;
27    typedef double& reference;
28
29    /**
30       \brief Default Constructor
31    */
32    IteratorWeighted(void) {};
33
34    /**
35       \brief Constructor
36
37       \param container iterator points to
38       \param index telling which element iterator points to
39    */
40    IteratorWeighted(Container& container, difference_type index)
41      : container_(&container), index_(index) {}
42
43    /**
44       \return element
45     */
46    return_type operator*(void) const 
47    { 
48      yat_assert<std::out_of_range>(index_<container_->size());
49      return container_->operator()(index_); 
50    }
51
52    /**
53       \return data
54    */
55    return_type data(void) const 
56    { yat_assert<std::out_of_range>(index_<container_->size()); 
57      return container_->data(index_); }
58
59    /**
60       \return weight
61    */
62    return_type weight(void) const 
63    { yat_assert<std::out_of_range>(index_<container_->size());
64      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.