1 | #ifndef _theplu_yat_utility_container2d_iterator_ |
---|
2 | #define _theplu_yat_utility_container2d_iterator_ |
---|
3 | |
---|
4 | // $Id: Container2DIterator.h 1566 2008-10-10 22:01:16Z 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 "iterator_traits.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 for a \ref concept_container_2d |
---|
42 | |
---|
43 | The iterator can be used to traverse through a \ref |
---|
44 | concept_container_2d row by row. |
---|
45 | |
---|
46 | \see StrideIterator |
---|
47 | */ |
---|
48 | template<typename Container, typename value, typename reference = value&> |
---|
49 | class Container2DIterator |
---|
50 | : public boost::iterator_facade< |
---|
51 | Container2DIterator<Container, value, reference> |
---|
52 | , value |
---|
53 | , std::random_access_iterator_tag |
---|
54 | , reference> |
---|
55 | { |
---|
56 | private: |
---|
57 | typedef Container2DIterator<Container, value, reference> self; |
---|
58 | |
---|
59 | public: |
---|
60 | /** |
---|
61 | \brief Default Constructor |
---|
62 | */ |
---|
63 | Container2DIterator(void) {}; |
---|
64 | |
---|
65 | /** |
---|
66 | \brief Constructor |
---|
67 | |
---|
68 | \param container iterator points to |
---|
69 | \param row telling which row iterator points to |
---|
70 | \param column telling which column iterator points to |
---|
71 | */ |
---|
72 | Container2DIterator(Container& container, size_t row, size_t column) |
---|
73 | : container_(&container), index_(row*container.columns()+column) {} |
---|
74 | |
---|
75 | private: |
---|
76 | friend class boost::iterator_core_access; |
---|
77 | |
---|
78 | Container* container_; |
---|
79 | size_t index_; |
---|
80 | |
---|
81 | void advance(int n) { index_+=n; } |
---|
82 | |
---|
83 | void decrement(void) { --index_; } |
---|
84 | |
---|
85 | int distance_to(const Container2DIterator& other) const |
---|
86 | { return other.index_ - index_; } |
---|
87 | |
---|
88 | reference dereference(void) const |
---|
89 | { |
---|
90 | yat_assert<std::out_of_range>(index_ < this->size(), |
---|
91 | "Container2DIterator::dereference"); |
---|
92 | return container_->operator()(row(index_), column(index_)); |
---|
93 | } |
---|
94 | |
---|
95 | bool equal(const Container2DIterator& other) const |
---|
96 | { return index_ == other.index_; } |
---|
97 | |
---|
98 | void increment(void) { ++index_; } |
---|
99 | |
---|
100 | size_t column(size_t i) const |
---|
101 | { return i % container_->columns(); } |
---|
102 | size_t row(size_t i) const |
---|
103 | { return static_cast<size_t>(i/container_->columns()); } |
---|
104 | size_t size() const |
---|
105 | { return container_->columns()*container_->rows(); } |
---|
106 | |
---|
107 | |
---|
108 | // Using compiler generated copy |
---|
109 | //Container2DIterator(const Container2DIterator&); |
---|
110 | //Container2DIterator& operator=(const Container2DIterator&); |
---|
111 | }; |
---|
112 | |
---|
113 | }}} // of namespace utility, yat, and theplu |
---|
114 | |
---|
115 | #endif |
---|