source: trunk/yat/utility/Iterator.h @ 916

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

Sorry this commit is a bit to big.

Adding a yat_assert. The yat assert are turned on by providing a
'-DYAT_DEBUG' flag to preprocessor if normal cassert is turned
on. This flag is activated for developers running configure with
--enable-debug. The motivation is that we can use these yat_asserts in
header files and the yat_asserts will be invisible to the normal user
also if he uses C-asserts.

added output operator in DataLookup2D and removed output operator in
MatrixLookup?

Removed template function add_values in Averager and weighted version

Added function to AveragerWeighted? taking iterator to four ranges.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.2 KB
Line 
1#ifndef _theplu_yat_utility_iterator_
2#define _theplu_yat_utility_iterator_
3
4// $Id: Iterator.h 916 2007-09-30 00:50:10Z peter $
5
6/*
7  Copyright (C) 2007 Peter Johansson
8
9  This file is part of the yat library, http://trac.thep.lu.se/trac/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 2 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "yat_assert.h"
28
29#include <iterator>
30#include <stddef.h>
31
32namespace theplu {
33namespace yat {
34namespace utility {
35
36  /**
37     @brief Iterator
38  */
39  template<typename return_type, typename Container>
40  class Iterator
41  {
42  public:
43    typedef std::random_access_iterator_tag iterator_category;
44    typedef double value_type;
45    typedef size_t difference_type;
46    typedef double* pointer;
47    typedef double& reference;
48
49    /**
50       \brief Default Constructor
51    */
52    Iterator(void) {};
53
54    /**
55       \brief Constructor
56
57       \param container iterator points to
58       \param index telling which element iterator points to
59    */
60    Iterator(Container& container, difference_type index)
61      : container_(&container), index_(index) {}
62
63    /**
64       \return element
65     */
66    return_type operator*(void) const 
67    { yat_assert(index_<container_->size());
68      return container_->operator()(index_); }
69
70    /**
71       \brief pre-increment
72
73       \return reference to *this
74     */
75    Iterator& operator++(void) { ++index_; return *this; }
76
77    /**
78       \brief post-increment
79
80       \return copy of iterator prior increment
81     */
82    Iterator operator++(int) 
83    { Iterator<return_type, Container> tmp(*this); ++index_; return tmp;}
84
85    /**
86       \brief iterate \f$ n \f$ steps forward
87
88       \return reference to resulting iterator
89     */
90    Iterator& operator+=(int n) { index_+=n; return *this; }
91
92    /**
93       \brief post-decrement
94
95       \return copy of iterator prior decrement
96     */
97    Iterator operator--(int) 
98    { Iterator<return_type, Container> tmp(*this); --index_; return tmp;}
99
100    /**
101       \brief pre-decrement
102
103       \return reference to resulting iterator
104     */
105    Iterator& operator--(void) { --index_; return *this; }
106
107    /**
108       \brief iterate \f$ n \f$ steps backwards
109
110       \return reference to resulting iterator
111     */
112    Iterator& operator-=(int n) { index_-=n; return *this; }
113
114    /**
115       \brief addition operator
116
117       \return copy of resulting iterator
118     */
119    friend Iterator operator+(const Iterator& lhs, size_t n) 
120    { return Iterator<return_type, Container>(*lhs.container_, lhs.index_+n); }
121
122    /**
123       \brief subtraction operator
124
125       \return copy of resulting iterator
126     */
127    friend Iterator operator-(const Iterator& lhs, size_t n) 
128    { return Iterator<return_type, Container>(*lhs.container_, lhs.index_-n); }
129
130    /**
131       \brief difference operator
132
133       \return distance between \a lhs and \a rhs
134     */
135    friend difference_type operator-(const Iterator& lhs, const Iterator& rhs) 
136    { return lhs.index_-rhs.index_; }
137
138   
139    /**
140       \brief Conversion operator
141
142       This operator allows automatic conversion from iterator to
143       const_iterator
144     */
145    operator Iterator<const double, const Container> () 
146    { return Iterator<const double, const Container>(*container_, index_); } 
147
148    /**
149       \brief Equality operator
150
151       \return True if \a lhs and \rhs are pointing to same element
152     */
153    friend bool operator==(const Iterator<return_type, Container>& lhs, 
154                           const Iterator<return_type, Container>& rhs)
155    { return lhs.container_==rhs.container_ && lhs.index_==rhs.index_; }
156   
157    /**
158       \brief Non-equality operator
159
160       \return False if \a lhs and \rhs are pointing to same element
161     */
162    friend bool operator!=(const Iterator<return_type, Container>& lhs, 
163                           const Iterator<return_type, Container>& rhs)
164    { return !(lhs.container_==rhs.container_ && lhs.index_==rhs.index_); }
165   
166    /**
167       \brief Less operator
168     */
169    friend bool operator<(const Iterator<return_type, Container>& lhs, 
170                           const Iterator<return_type, Container>& rhs)
171    { return lhs.index_<rhs.index_; }
172   
173    /**
174       \brief Less equal operator
175     */
176    friend bool operator<=(const Iterator<return_type, Container>& lhs, 
177                           const Iterator<return_type, Container>& rhs)
178    { return lhs.index_<=rhs.index_; }
179   
180    /**
181       \brief Larger operator
182     */
183    friend bool operator>(const Iterator<return_type, Container>& lhs, 
184                           const Iterator<return_type, Container>& rhs)
185    { return lhs.index_>rhs.index_; }
186   
187    /**
188       \brief Larger equal operator
189     */
190    friend bool operator>=(const Iterator<return_type, Container>& lhs, 
191                           const Iterator<return_type, Container>& rhs)
192    { return lhs.index_>=rhs.index_; }
193   
194  private:
195    Container* container_;
196    size_t index_;
197
198    // Using compiler generated copy
199    //Iterator(const Iterator&);
200    //Iterator& operator=(const Iterator&);
201  };
202
203}}} // of namespace utility, yat, and theplu
204
205#endif
Note: See TracBrowser for help on using the repository browser.