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

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

docs typo

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.8 KB
Line 
1#ifndef _theplu_yat_utility_container2d_iterator_
2#define _theplu_yat_utility_container2d_iterator_
3
4// $Id: Container2DIterator.h 1540 2008-09-30 03:42:30Z 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 "IteratorPolicy.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
36namespace theplu {
37namespace yat {
38namespace utility {
39
40  /**
41     @brief Iterator
42  */
43  template<typename Container, typename value, 
44           typename pointer = value*, typename reference = value&,
45           class Policy = IteratorPolicy<Container, reference, value> > 
46  class Container2DIterator
47    : public boost::iterator_facade<
48    Container2DIterator<Container, value, pointer, reference, Policy>
49    , value
50    , std::random_access_iterator_tag
51    , reference>
52  {
53  public:
54    /**
55       Tells whether iterator is weighted or not.
56
57       \see weighted_iterator_tag and unweighted_iterator_tag
58     */
59    typedef typename Policy::weighted_iterator_type weighted_iterator_type;
60
61  private:
62    typedef Container2DIterator<Container, value, pointer,reference,Policy> self;
63
64  public:
65    /**
66       \brief Default Constructor
67    */
68    Container2DIterator(void) {};
69
70    /**
71       \brief Constructor
72
73       \param container iterator points to
74       \param row telling which row iterator points to
75       \param column telling which column iterator points to
76    */
77    Container2DIterator(Container& container, size_t row, size_t column)
78      : container_(&container), index_(row*container.columns()+column) {}
79
80    /**
81       \return data
82    */
83    typename Policy::data_type data(void) const 
84    { yat_assert<std::out_of_range>(index_<this->size(), 
85                                    "Container2DIterator::data"); 
86      return ip_.data(*container_, row(index_), column(index_)); 
87    }
88
89    /**
90       \return weight
91    */
92    typename Policy::weight_type weight(void) const 
93    { yat_assert<std::out_of_range>(index_<this->size(), 
94                                    "Container2DIterator::weight"); 
95      return ip_.weight(*container_, row(index_), column(index_)); 
96    }
97
98  private:
99    friend class boost::iterator_core_access;
100
101    Container* container_;
102    size_t index_;
103    Policy ip_;
104
105    void advance(int n) { index_+=n; }
106
107    void decrement(void) { --index_; }
108
109    int distance_to(const Container2DIterator& other) const
110    { return other.index_ - index_; }
111
112    reference dereference(void) const 
113    { 
114      yat_assert<std::out_of_range>(index_ < this->size(), 
115                                    "Container2DIterator::dereference");
116      return ip_.dereference(*container_, row(index_), column(index_));
117    }
118
119    bool equal(const Container2DIterator& other) const
120    { return index_ == other.index_; }
121
122    void increment(void) { ++index_; }
123
124    size_t column(size_t i) const 
125    { return i % container_->columns(); }
126    size_t row(size_t i) const 
127    { return static_cast<size_t>(i/container_->columns()); }
128    size_t size() const 
129    { return container_->columns()*container_->rows(); }
130
131
132    // Using compiler generated copy
133    //Container2DIterator(const Container2DIterator&);
134    //Container2DIterator& operator=(const Container2DIterator&);
135  };
136
137  /**
138     Specialization for Container2DIterator
139   */
140  template<typename A, typename B, typename C, typename D, typename Policy> 
141  struct weighted_iterator_traits<Container2DIterator<A, B, C, D, Policy> > {
142    /**
143       Whether Container2DIterator is weighted is desiced by Policy.
144    */
145    typedef typename Policy::weighted_iterator_type type;
146  };
147
148  /**
149     Specialization for Container2DIterator
150   */
151  template<typename A, typename B, typename C, typename D, typename E> 
152  struct iterator_traits<Container2DIterator<A, B, C, D, E> > {
153    /**
154       data_reference is defined by Policy class E::data_type
155     */
156    typedef typename E::data_type data_reference;
157
158    /**
159       weight_reference is defined by Policy class E::weight_type
160     */
161    typedef typename E::weight_type weight_reference;
162
163    /**
164       \return data
165    */
166    data_reference data(Container2DIterator<A, B, C, D, E> iter) const 
167    { return iter.data(); }
168
169    /**
170       \return weight
171    */
172    weight_reference weight(Container2DIterator<A, B, C, D, E> iter) const 
173    { return iter.weight(); }
174
175  };
176
177
178}}} // of namespace utility, yat, and theplu
179
180#endif
Note: See TracBrowser for help on using the repository browser.