source: trunk/yat/utility/Container2DIterator.h @ 1565

Last change on this file since 1565 was 1565, checked in by Peter, 13 years ago

removed Policy from Container2DIterator - refs #398

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1#ifndef _theplu_yat_utility_container2d_iterator_
2#define _theplu_yat_utility_container2d_iterator_
3
4// $Id: Container2DIterator.h 1565 2008-10-10 21:49:07Z 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 "deprecate.h"
27#include "iterator_traits.h"
28#include "yat_assert.h"
29
30#include <boost/iterator/iterator_facade.hpp>
31
32#include <iterator>
33#include <stddef.h>
34#include <stdexcept>
35#include <utility>
36
37namespace theplu {
38namespace yat {
39namespace utility {
40
41  /**
42     @brief Iterator
43  */
44  template<typename Container, typename value, typename reference = value&>
45  class Container2DIterator
46    : public boost::iterator_facade<
47    Container2DIterator<Container, value, reference>
48    , value
49    , std::random_access_iterator_tag
50    , reference>
51  {
52  private:
53    typedef Container2DIterator<Container, value, reference> self;
54
55  public:
56    /**
57       \brief Default Constructor
58    */
59    Container2DIterator(void) {};
60
61    /**
62       \brief Constructor
63
64       \param container iterator points to
65       \param row telling which row iterator points to
66       \param column telling which column iterator points to
67    */
68    Container2DIterator(Container& container, size_t row, size_t column)
69      : container_(&container), index_(row*container.columns()+column) {}
70
71  private:
72    friend class boost::iterator_core_access;
73
74    Container* container_;
75    size_t index_;
76
77    void advance(int n) { index_+=n; }
78
79    void decrement(void) { --index_; }
80
81    int distance_to(const Container2DIterator& other) const
82    { return other.index_ - index_; }
83
84    reference dereference(void) const 
85    { 
86      yat_assert<std::out_of_range>(index_ < this->size(), 
87                                    "Container2DIterator::dereference");
88      return container_->operator()(row(index_), column(index_));
89    }
90
91    bool equal(const Container2DIterator& other) const
92    { return index_ == other.index_; }
93
94    void increment(void) { ++index_; }
95
96    size_t column(size_t i) const 
97    { return i % container_->columns(); }
98    size_t row(size_t i) const 
99    { return static_cast<size_t>(i/container_->columns()); }
100    size_t size() const 
101    { return container_->columns()*container_->rows(); }
102
103
104    // Using compiler generated copy
105    //Container2DIterator(const Container2DIterator&);
106    //Container2DIterator& operator=(const Container2DIterator&);
107  };
108
109}}} // of namespace utility, yat, and theplu
110
111#endif
Note: See TracBrowser for help on using the repository browser.