1 | #ifndef _theplu_yat_utility_const_iterator_ |
---|
2 | #define _theplu_yat_utility_const_iterator_ |
---|
3 | |
---|
4 | // $Id: constIterator.h 880 2007-09-21 23:43:22Z 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 "Iterator.h" |
---|
28 | |
---|
29 | #include <iterator> |
---|
30 | #include <stddef.h> |
---|
31 | |
---|
32 | namespace theplu { |
---|
33 | namespace yat { |
---|
34 | namespace utility { |
---|
35 | |
---|
36 | /** |
---|
37 | @brief read only Iterator |
---|
38 | */ |
---|
39 | template<typename Container> |
---|
40 | class constIterator |
---|
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 const double* pointer; |
---|
47 | typedef double reference; |
---|
48 | |
---|
49 | /** |
---|
50 | @brief Constructor |
---|
51 | */ |
---|
52 | constIterator(void) {}; |
---|
53 | constIterator(Container& container, difference_type index) |
---|
54 | : container_(&container), index_(index) {} |
---|
55 | // allow const conversion |
---|
56 | constIterator(const Iterator<Container>& iter) |
---|
57 | : container_(&iter.container_), index_(&(iter.index_)) {} |
---|
58 | |
---|
59 | reference operator*(void) const { return container_->operator()(index_); } |
---|
60 | const constIterator& operator++(void) { ++index_; return *this; } |
---|
61 | constIterator operator++(int) |
---|
62 | { constIterator<Container> tmp(*this); ++index_; return tmp;} |
---|
63 | constIterator& operator+=(int n) { index_+=n; return *this; } |
---|
64 | constIterator operator+(int n) |
---|
65 | { return constIterator<Container>(*container_, index_+n); } |
---|
66 | constIterator operator--(int) |
---|
67 | { constIterator<Container> tmp(*this); --index_; return tmp;} |
---|
68 | constIterator operator--(void) { --index_; return *this; } |
---|
69 | constIterator& operator-=(int n) { index_-=n; return *this; } |
---|
70 | constIterator operator-(size_t n) |
---|
71 | { return constIterator<Container>(*container_, index_-n); } |
---|
72 | size_t operator-(const constIterator& rhs) |
---|
73 | { return index_-rhs.index_; } |
---|
74 | |
---|
75 | bool equal(const constIterator<Container>& other) const |
---|
76 | { return container_==other.container_ && index_==other.index_; } |
---|
77 | |
---|
78 | bool less(const constIterator<Container>& other) const |
---|
79 | { return index_<other.index_; } |
---|
80 | |
---|
81 | private: |
---|
82 | Container* container_; |
---|
83 | size_t index_; |
---|
84 | |
---|
85 | // Using compiler generated copy |
---|
86 | //constIterator(const constIterator&); |
---|
87 | //constIterator& operator=(const constIterator&); |
---|
88 | }; |
---|
89 | |
---|
90 | template <typename T> |
---|
91 | inline bool operator==(const constIterator<T>& lhs, |
---|
92 | const constIterator<T>& rhs) |
---|
93 | { |
---|
94 | return lhs.equal(rhs); |
---|
95 | } |
---|
96 | |
---|
97 | template <typename T> |
---|
98 | inline bool operator!=(const constIterator<T>& lhs, |
---|
99 | const constIterator<T>& rhs) |
---|
100 | { |
---|
101 | return !(lhs==rhs); |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | template <typename T> |
---|
106 | inline bool operator<(const constIterator<T>& lhs, |
---|
107 | const constIterator<T>& rhs) |
---|
108 | { |
---|
109 | return lhs<rhs; |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | template <typename T> |
---|
114 | inline bool operator<=(const constIterator<T>& lhs, |
---|
115 | const constIterator<T>& rhs) |
---|
116 | { |
---|
117 | return !(rhs<lhs); |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | template <typename T> |
---|
122 | inline bool operator>(const constIterator<T>& lhs, |
---|
123 | const constIterator<T>& rhs) |
---|
124 | { |
---|
125 | return rhs<lhs; |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | template <typename T> |
---|
130 | inline bool operator>=(const constIterator<T>& lhs, |
---|
131 | const constIterator<T>& rhs) |
---|
132 | { |
---|
133 | return !(lhs<rhs); |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | }}} // of namespace utility, yat, and theplu |
---|
138 | |
---|
139 | #endif |
---|