yat  0.14.5pre
Container2DIterator.h
1 #ifndef _theplu_yat_utility_container2d_iterator_
2 #define _theplu_yat_utility_container2d_iterator_
3 
4 // $Id: Container2DIterator.h 3417 2015-05-25 01:35:59Z peter $
5 
6 /*
7  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009, 2010, 2015 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_facade.hpp>
32 
33 #include <cstddef>
34 #include <iterator>
35 #include <stdexcept>
36 #include <utility>
37 
38 namespace theplu {
39 namespace yat {
40 namespace utility {
41 
60  template<typename Container, typename value, typename reference = value&>
62  : public boost::iterator_facade<
63  Container2DIterator<Container, value, reference>
64  , value
65  , boost::random_access_traversal_tag
66  , reference>
67  {
68  private:
70 
71  public:
76  {
77  BOOST_CONCEPT_ASSERT((Container2D<Container>));
78  };
79 
87  Container2DIterator(Container& container, size_t row, size_t column)
88  : container_(&container), index_(row*container.columns()+column)
89  {
90  BOOST_CONCEPT_ASSERT((Container2D<Container>));
91  }
92 
93  private:
94  friend class boost::iterator_core_access;
95 
96  Container* container_;
97  size_t index_;
98 
99  void advance(int n) { index_+=n; }
100 
101  void decrement(void) { --index_; }
102 
103  int distance_to(const Container2DIterator& other) const
104  { return other.index_ - index_; }
105 
106  reference dereference(void) const
107  {
108  yat_assert<std::out_of_range>(index_ < this->size(),
109  "Container2DIterator::dereference");
110  return container_->operator()(row(index_), column(index_));
111  }
112 
113  bool equal(const Container2DIterator& other) const
114  { return index_ == other.index_; }
115 
116  void increment(void) { ++index_; }
117 
118  size_t column(size_t i) const
119  { return i % container_->columns(); }
120  size_t row(size_t i) const
121  { return static_cast<size_t>(i/container_->columns()); }
122  size_t size() const
123  { return container_->columns()*container_->rows(); }
124 
125 
126  // Using compiler generated copy
127  //Container2DIterator(const Container2DIterator&);
128  //Container2DIterator& operator=(const Container2DIterator&);
129  };
130 
131 }}} // of namespace utility, yat, and theplu
132 
133 #endif
Concept check for Container2D.
Definition: concept_check.h:59
Iterator for a Container2D.
Definition: Container2DIterator.h:61
Container2DIterator(Container &container, size_t row, size_t column)
Constructor.
Definition: Container2DIterator.h:87
Container2DIterator(void)
Default Constructor.
Definition: Container2DIterator.h:75

Generated on Tue Sep 26 2017 02:33:29 for yat by  doxygen 1.8.5