/usr/include/dune/common/genericiterator.hh is in libdune-common-dev 2.2.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | // $Id: genericiterator.hh 7391 2013-02-26 12:26:33Z mblatt $
#ifndef DUNE_GENERICITERATOR_HH
#define DUNE_GENERICITERATOR_HH
#include <dune/common/iteratorfacades.hh>
#include <cassert>
namespace Dune {
/*! \defgroup GenericIterator GenericIterator
\ingroup IteratorFacades
\brief Generic Iterator class for writing stl conformant iterators
for any container class with operator[]
Using this template class you can create an iterator and a const_iterator
for any container class.
Imagine you have SimpleContainer and would like to have an iterator.
All you have to do is provide operator[], begin() and end()
(for const and for non-const).
\code
template<class T>
class SimpleContainer{
public:
typedef GenericIterator<SimpleContainer<T>,T> iterator;
typedef GenericIterator<const SimpleContainer<T>,const T> const_iterator;
SimpleContainer(){
for(int i=0; i < 100; i++)
values_[i]=i;
}
iterator begin(){
return iterator(*this, 0);
}
const_iterator begin() const{
return const_iterator(*this, 0);
}
iterator end(){
return iterator(*this, 100);
}
const_iterator end() const{
return const_iterator(*this, 100);
}
T& operator[](int i){
return values_[i];
}
const T& operator[](int i) const{
return values_[i];
}
private:
T values_[100];
};
\endcode
See dune/common/test/iteratorfacestest.hh for details or
Dune::QuadratureDefault in dune/quadrature/quadrature.hh
for a real example.
*/
/**
* @file
* @brief Implements a generic iterator class for writing stl conformant iterators.
*
* Using this generic iterator writing iterators for containers
* that implement operator[] is only a matter of seconds.
*/
/**
\brief Get the 'const' version of a reference to a mutable object
Given a reference R=T& const_reference<R>::type gives you the typedef for const T&
*/
template<class R>
struct const_reference
{
typedef const R type;
};
template<class R>
struct const_reference<const R>
{
typedef const R type;
};
template<class R>
struct const_reference<R&>
{
typedef const R& type;
};
template<class R>
struct const_reference<const R&>
{
typedef const R& type;
};
/**
\brief get the 'mutable' version of a reference to a const object
given a const reference R=const T& mutable_reference<R>::type gives you the typedef for T&
*/
template<class R>
struct mutable_reference
{
typedef R type;
};
template<class R>
struct mutable_reference<const R>
{
typedef R type;
};
template<class R>
struct mutable_reference<R&>
{
typedef R& type;
};
template<class R>
struct mutable_reference<const R&>
{
typedef R& type;
};
/** @addtogroup GenericIterator
*
* @{
*/
/**
* @brief Generic class for stl-conforming iterators for container classes with operator[].
*
* If template parameter C has a const qualifier we are a const iterator, otherwise we
* are a mutable iterator.
*/
template<class C, class T, class R=T&, class D = std::ptrdiff_t,
template<class,class,class,class> class IteratorFacade=RandomAccessIteratorFacade>
class GenericIterator :
public IteratorFacade<GenericIterator<C,T,R,D,IteratorFacade>,T,R,D>
{
friend class GenericIterator<typename remove_const<C>::type, typename remove_const<T>::type, typename mutable_reference<R>::type, D, IteratorFacade>;
friend class GenericIterator<const typename remove_const<C>::type, const typename remove_const<T>::type, typename const_reference<R>::type, D, IteratorFacade>;
typedef GenericIterator<typename remove_const<C>::type, typename remove_const<T>::type, typename mutable_reference<R>::type, D, IteratorFacade> MutableIterator;
typedef GenericIterator<const typename remove_const<C>::type, const typename remove_const<T>::type, typename const_reference<R>::type, D, IteratorFacade> ConstIterator;
public:
/**
* @brief The type of container we are an iterator for.
*
* The container type must provide an operator[] method.
*
* If C has a const qualifier we are a const iterator, otherwise we
* are a mutable iterator.
*/
typedef C Container;
/**
* @brief The value type of the iterator.
*
* This is the return type when dereferencing the iterator.
*/
typedef T Value;
/**
* @brief The type of the difference between two positions.
*/
typedef D DifferenceType;
/**
* @brief The type of the reference to the values accessed.
*/
typedef R Reference;
// Constructors needed by the base iterators
GenericIterator(): container_(0), position_(0)
{}
/**
* @brief Constructor
* @param cont Reference to the container we are an iterator for
* @param pos The postion the iterator will be positioned to
* (e.g. 0 for an iterator returned by Container::begin() or
* the sizeof the container for an iterator returned by Container::end()
*/
GenericIterator(Container& cont, DifferenceType pos)
: container_(&cont), position_(pos)
{}
/**
* @brief Copy constructor
*
* This is somehow hard to understand, therefore play with the cases:
* 1. if we are mutable this is the only valid copy constructor, as the argument is a mutable iterator
* 2. if we are a const iterator the argument is a mutable iterator => This is the needed conversion to initialize a const iterator from a mutable one.
*/
GenericIterator(const MutableIterator& other): container_(other.container_), position_(other.position_)
{}
/**
* @brief Copy constructor
*
* @warning Calling this method results in a compiler error, if this is a mutable iterator.
*
* This is somehow hard to understand, therefore play with the cases:
* 1. if we are mutable the arguments is a const iterator and therefore calling this method is mistake in the user's code and results in a (probably not understandable) compiler error
* 2. If we are a const iterator this is the default copy constructor as the argument is a const iterator too.
*/
GenericIterator(const ConstIterator& other): container_(other.container_), position_(other.position_)
{}
// Methods needed by the forward iterator
bool equals(const MutableIterator & other) const
{
return position_ == other.position_ && container_ == other.container_;
}
bool equals(const ConstIterator & other) const
{
return position_ == other.position_ && container_ == other.container_;
}
Reference dereference() const{
return container_->operator[](position_);
}
void increment(){
++position_;
}
// Additional function needed by BidirectionalIterator
void decrement(){
--position_;
}
// Additional function needed by RandomAccessIterator
Reference elementAt(DifferenceType i)const{
return container_->operator[](position_+i);
}
void advance(DifferenceType n){
position_=position_+n;
}
DifferenceType distanceTo(const MutableIterator& other)const
{
assert(other.container_==container_);
return other.position_ - position_;
}
DifferenceType distanceTo(const ConstIterator& other)const
{
assert(other.container_==container_);
return other.position_ - position_;
}
private:
Container *container_;
DifferenceType position_;
};
/** @} */
} // end namespace Dune
#endif
|