yat  0.10.4pre
Container2DIterator.h
1 #ifndef _theplu_yat_utility_container2d_iterator_
2 #define _theplu_yat_utility_container2d_iterator_
3 
4 // $Id: Container2DIterator.h 2384 2010-12-22 14:03:36Z peter $
5 
6 /*
7  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009, 2010 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  , std::random_access_iterator_tag
66  , reference>
67  {
68  private:
70 
71  public:
76  {
77  BOOST_CONCEPT_ASSERT((Container2D<Container>));
78  BOOST_CONCEPT_ASSERT((boost::Convertible<reference,value>));
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  BOOST_CONCEPT_ASSERT((boost::Convertible<reference,value>));
93  }
94 
95  private:
96  friend class boost::iterator_core_access;
97 
98  Container* container_;
99  size_t index_;
100 
101  void advance(int n) { index_+=n; }
102 
103  void decrement(void) { --index_; }
104 
105  int distance_to(const Container2DIterator& other) const
106  { return other.index_ - index_; }
107 
108  reference dereference(void) const
109  {
110  yat_assert<std::out_of_range>(index_ < this->size(),
111  "Container2DIterator::dereference");
112  return container_->operator()(row(index_), column(index_));
113  }
114 
115  bool equal(const Container2DIterator& other) const
116  { return index_ == other.index_; }
117 
118  void increment(void) { ++index_; }
119 
120  size_t column(size_t i) const
121  { return i % container_->columns(); }
122  size_t row(size_t i) const
123  { return static_cast<size_t>(i/container_->columns()); }
124  size_t size() const
125  { return container_->columns()*container_->rows(); }
126 
127 
128  // Using compiler generated copy
129  //Container2DIterator(const Container2DIterator&);
130  //Container2DIterator& operator=(const Container2DIterator&);
131  };
132 
133 }}} // of namespace utility, yat, and theplu
134 
135 #endif

Generated on Mon Nov 11 2013 09:41:44 for yat by  doxygen 1.8.1