1 | #ifndef _theplu_yat_utility_container2d_iterator_ |
---|
2 | #define _theplu_yat_utility_container2d_iterator_ |
---|
3 | |
---|
4 | // $Id: Container2DIterator.h 1562 2008-10-10 17:10:21Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson |
---|
8 | Copyright (C) 2008 Peter Johansson |
---|
9 | |
---|
10 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
11 | |
---|
12 | The yat library is free software; you can redistribute it and/or |
---|
13 | modify it under the terms of the GNU General Public License as |
---|
14 | published by the Free Software Foundation; either version 3 of the |
---|
15 | License, or (at your option) any later version. |
---|
16 | |
---|
17 | The yat library is distributed in the hope that it will be useful, |
---|
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
20 | General Public License for more details. |
---|
21 | |
---|
22 | You should have received a copy of the GNU General Public License |
---|
23 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
24 | */ |
---|
25 | |
---|
26 | #include "IteratorPolicy.h" |
---|
27 | #include "yat_assert.h" |
---|
28 | |
---|
29 | #include <boost/iterator/iterator_facade.hpp> |
---|
30 | |
---|
31 | #include <iterator> |
---|
32 | #include <stddef.h> |
---|
33 | #include <stdexcept> |
---|
34 | #include <utility> |
---|
35 | |
---|
36 | namespace theplu { |
---|
37 | namespace yat { |
---|
38 | namespace utility { |
---|
39 | |
---|
40 | /** |
---|
41 | @brief Iterator |
---|
42 | */ |
---|
43 | template<typename Container, typename value, |
---|
44 | typename pointer = value*, typename reference = value&, |
---|
45 | class Policy = IteratorPolicy<Container, reference, value> > |
---|
46 | class Container2DIterator |
---|
47 | : public boost::iterator_facade< |
---|
48 | Container2DIterator<Container, value, pointer, reference, Policy> |
---|
49 | , value |
---|
50 | , std::random_access_iterator_tag |
---|
51 | , reference> |
---|
52 | { |
---|
53 | public: |
---|
54 | /** |
---|
55 | Tells whether iterator is weighted or not. |
---|
56 | |
---|
57 | \see weighted_iterator_tag and unweighted_iterator_tag |
---|
58 | */ |
---|
59 | typedef typename Policy::weighted_iterator_type weighted_iterator_type; |
---|
60 | |
---|
61 | private: |
---|
62 | typedef Container2DIterator<Container, value, pointer,reference,Policy> self; |
---|
63 | |
---|
64 | public: |
---|
65 | /** |
---|
66 | \brief Default Constructor |
---|
67 | */ |
---|
68 | Container2DIterator(void) {}; |
---|
69 | |
---|
70 | /** |
---|
71 | \brief Constructor |
---|
72 | |
---|
73 | \param container iterator points to |
---|
74 | \param row telling which row iterator points to |
---|
75 | \param column telling which column iterator points to |
---|
76 | */ |
---|
77 | Container2DIterator(Container& container, size_t row, size_t column) |
---|
78 | : container_(&container), index_(row*container.columns()+column) {} |
---|
79 | |
---|
80 | /** |
---|
81 | \return data |
---|
82 | */ |
---|
83 | typename Policy::data_type data(void) const |
---|
84 | { yat_assert<std::out_of_range>(index_<this->size(), |
---|
85 | "Container2DIterator::data"); |
---|
86 | return ip_.data(*container_, row(index_), column(index_)); |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | \return weight |
---|
91 | */ |
---|
92 | typename Policy::weight_type weight(void) const |
---|
93 | { yat_assert<std::out_of_range>(index_<this->size(), |
---|
94 | "Container2DIterator::weight"); |
---|
95 | return ip_.weight(*container_, row(index_), column(index_)); |
---|
96 | } |
---|
97 | |
---|
98 | private: |
---|
99 | friend class boost::iterator_core_access; |
---|
100 | |
---|
101 | Container* container_; |
---|
102 | size_t index_; |
---|
103 | Policy ip_; |
---|
104 | |
---|
105 | void advance(int n) { index_+=n; } |
---|
106 | |
---|
107 | void decrement(void) { --index_; } |
---|
108 | |
---|
109 | int distance_to(const Container2DIterator& other) const |
---|
110 | { return other.index_ - index_; } |
---|
111 | |
---|
112 | reference dereference(void) const |
---|
113 | { |
---|
114 | yat_assert<std::out_of_range>(index_ < this->size(), |
---|
115 | "Container2DIterator::dereference"); |
---|
116 | return container_->operator()(row(index_), column(index_)); |
---|
117 | } |
---|
118 | |
---|
119 | bool equal(const Container2DIterator& other) const |
---|
120 | { return index_ == other.index_; } |
---|
121 | |
---|
122 | void increment(void) { ++index_; } |
---|
123 | |
---|
124 | size_t column(size_t i) const |
---|
125 | { return i % container_->columns(); } |
---|
126 | size_t row(size_t i) const |
---|
127 | { return static_cast<size_t>(i/container_->columns()); } |
---|
128 | size_t size() const |
---|
129 | { return container_->columns()*container_->rows(); } |
---|
130 | |
---|
131 | |
---|
132 | // Using compiler generated copy |
---|
133 | //Container2DIterator(const Container2DIterator&); |
---|
134 | //Container2DIterator& operator=(const Container2DIterator&); |
---|
135 | }; |
---|
136 | |
---|
137 | /** |
---|
138 | Specialization for Container2DIterator |
---|
139 | */ |
---|
140 | template<typename A, typename B, typename C, typename D, typename E> |
---|
141 | struct iterator_traits<Container2DIterator<A, B, C, D, E> > { |
---|
142 | /** |
---|
143 | data_reference is defined by Policy class E::data_type |
---|
144 | */ |
---|
145 | typedef typename E::data_type data_reference; |
---|
146 | |
---|
147 | /** |
---|
148 | weight_reference is defined by Policy class E::weight_type |
---|
149 | */ |
---|
150 | typedef typename E::weight_type weight_reference; |
---|
151 | |
---|
152 | /** |
---|
153 | \return data |
---|
154 | */ |
---|
155 | data_reference data(Container2DIterator<A, B, C, D, E> iter) const |
---|
156 | { return iter.data(); } |
---|
157 | |
---|
158 | /** |
---|
159 | \return weight |
---|
160 | */ |
---|
161 | weight_reference weight(Container2DIterator<A, B, C, D, E> iter) const |
---|
162 | { return iter.weight(); } |
---|
163 | |
---|
164 | }; |
---|
165 | |
---|
166 | |
---|
167 | }}} // of namespace utility, yat, and theplu |
---|
168 | |
---|
169 | #endif |
---|