yat  0.21pre
Container2DIterator.h
1 #ifndef _theplu_yat_utility_container2d_iterator_
2 #define _theplu_yat_utility_container2d_iterator_
3 
4 // $Id: Container2DIterator.h 4207 2022-08-26 04:36:28Z peter $
5 
6 /*
7  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009, 2010, 2015, 2022 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 "concept_check.h"
27 #include "iterator_traits.h"
28 #include "yat_assert.h"
29 
30 #include <boost/concept_check.hpp>
31 #include <boost/iterator/iterator_adaptor.hpp>
32 #include <boost/iterator/iterator_facade.hpp>
33 
34 #include <cstddef>
35 #include <iterator>
36 #include <stdexcept>
37 #include <utility>
38 
39 namespace theplu {
40 namespace yat {
41 namespace utility {
42 
61  template<typename Container, typename value, typename reference = value&>
63  : public boost::iterator_facade<
64  Container2DIterator<Container, value, reference>
65  , value
66  , boost::random_access_traversal_tag
67  , reference>
68  {
69  private:
71 
72  public:
77  {
78  BOOST_CONCEPT_ASSERT((Container2D<Container>));
79  };
80 
88  Container2DIterator(Container& container, size_t row, size_t column)
89  : container_(&container), index_(row*container.columns()+column)
90  {
91  BOOST_CONCEPT_ASSERT((Container2D<Container>));
92  }
93 
94 
102  template<typename C2, typename V2, typename R2>
104  typename boost::enable_if_convertible<C2*, Container*>::type* = 0,
105  typename boost::enable_if_convertible<R2, reference>::type* = 0)
106  : container_(&other.container()),
107  index_(other.row() * other.container().columns()+other.column())
108  {}
109 
110 
116  Container& container(void) const { return *container_; }
117 
123  size_t row(void) const { return row(index_); }
124 
130  size_t column(void) const { return column(index_); }
131 
132  private:
133  friend class boost::iterator_core_access;
134 
135  Container* container_;
136  size_t index_;
137 
138  void advance(int n) { index_+=n; }
139 
140  void decrement(void) { --index_; }
141 
142  int distance_to(const Container2DIterator& other) const
143  { return other.index_ - index_; }
144 
145  reference dereference(void) const
146  {
147  yat_assert<std::out_of_range>(index_ < this->size(),
148  "Container2DIterator::dereference");
149  return container_->operator()(row(), column());
150  }
151 
152  bool equal(const Container2DIterator& other) const
153  { return index_ == other.index_; }
154 
155  void increment(void) { ++index_; }
156 
157  size_t column(size_t i) const
158  { return i % container_->columns(); }
159  size_t row(size_t i) const
160  { return static_cast<size_t>(i/container_->columns()); }
161  size_t size() const
162  { return container_->columns()*container_->rows(); }
163 
164 
165  // Using compiler generated copy
166  //Container2DIterator(const Container2DIterator&);
167  //Container2DIterator& operator=(const Container2DIterator&);
168  };
169 
170 }}} // of namespace utility, yat, and theplu
171 
172 #endif
The Department of Theoretical Physics namespace as we define it.
Concept check for Container2D.
Definition: concept_check.h:65
size_t row(void) const
Definition: Container2DIterator.h:123
Iterator for a Container2D.
Definition: Container2DIterator.h:62
Container2DIterator(Container &container, size_t row, size_t column)
Constructor.
Definition: Container2DIterator.h:88
size_t column(void) const
Definition: Container2DIterator.h:130
Container & container(void) const
Definition: Container2DIterator.h:116
Container2DIterator(Container2DIterator< C2, V2, R2 > other, typename boost::enable_if_convertible< C2 *, Container *>::type *=0, typename boost::enable_if_convertible< R2, reference >::type *=0)
Definition: Container2DIterator.h:103
Container2DIterator(void)
Default Constructor.
Definition: Container2DIterator.h:76

Generated on Wed Jan 25 2023 03:34:29 for yat by  doxygen 1.8.14