Skip to content
Snippets Groups Projects
Commit 60716c20 authored by Markus Blatt's avatar Markus Blatt
Browse files

Refactored the Iterators.

All iterators (except reverse iterators) are now full grown std
iterators.

This closes flyspray issue #193.

[[Imported from SVN: r670]]
parent 4cebb22a
No related branches found
No related tags found
No related merge requests found
......@@ -9,6 +9,7 @@
#include "istlexception.hh"
#include "allocator.hh"
#include <dune/common/iteratorfacades.hh>
/** \file
\brief Implements several basic array containers.
......@@ -74,87 +75,83 @@ namespace Dune {
return p[i];
}
// forward declaration
class const_iterator;
//! iterator class for sequential access
class iterator
template<class T>
class RealIterator
: public BidirectionalIteratorFacade<RealIterator<T>, T>
{
public:
//! \brief The unqualified value type
typedef typename Dune::RemoveConst<T>::Type ValueType;
friend class BidirectionalIteratorFacade<RealIterator<const ValueType>, const ValueType>;
friend class BidirectionalIteratorFacade<RealIterator<ValueType>, ValueType>;
friend class RealIterator<const ValueType>;
friend class RealIterator<ValueType>;
//! constructor
iterator ()
{
p = 0;
i = 0;
}
RealIterator ()
: p(0), i(0)
{}
iterator (const B* _p, B* _i) : p(_p), i(_i)
{ }
RealIterator (const B* _p, B* _i) : p(_p), i(_i)
{ }
//! prefix increment
iterator& operator++()
{
++i;
return *this;
}
RealIterator(const RealIterator<const ValueType>& it)
: p(it.p), i(it.i)
{}
//! prefix decrement
iterator& operator--()
RealIterator(const RealIterator<ValueType>& it)
: p(it.p), i(it.i)
{}
//! return index
size_type index ()
{
--i;
return *this;
return i-p;
}
//! equality
bool operator== (const iterator& it) const
bool equals (const RealIterator<ValueType>& other) const
{
return (i)==(it.i);
assert(other.p==p);
return i==other.i;
}
//! inequality
bool operator!= (const iterator& it) const
//! equality
bool equals (const RealIterator<const ValueType>& other) const
{
return (i)!=(it.i);
assert(other.p==p);
return i==other.i;
}
//! equality with a const iterator
bool operator== (const const_iterator& it) const
protected:
//! prefix increment
void increment()
{
return (i)==(it.i);
++i;
}
//! inequality with a const iterator
bool operator!= (const const_iterator& it) const
//! prefix decrement
void decrement()
{
return (i)!=(it.i);
--i;
}
//! dereferencing
B& operator* () const
B& dereference () const
{
return *i;
}
//! arrow
B* operator-> () const
{
return i;
}
//! return index corresponding to pointer
size_type index () const
{
return i-p;
}
friend class const_iterator;
private:
const B* p;
B* i;
};
//! iterator type for sequential access
typedef RealIterator<B> iterator;
//! begin iterator
iterator begin ()
{
......@@ -188,85 +185,8 @@ namespace Dune {
return iterator(p,p+n);
}
//! const_iterator class for sequential access
class const_iterator
{
public:
//! constructor
const_iterator ()
{
p = 0;
i = 0;
}
const_iterator (const B* _p, const B* _i) : p(_p), i(_i)
{ }
const_iterator (const iterator& it) : p(it.p), i(it.i)
{ }
//! prefix increment
const_iterator& operator++()
{
++i;
return *this;
}
//! prefix decrement
const_iterator& operator--()
{
--i;
return *this;
}
//! equality
bool operator== (const const_iterator& it) const
{
return (i)==(it.i);
}
//! inequality
bool operator!= (const const_iterator& it) const
{
return (i)!=(it.i);
}
//! equality
bool operator== (const iterator& it) const
{
return (i)==(it.i);
}
//! inequality
bool operator!= (const iterator& it) const
{
return (i)!=(it.i);
}
//! dereferencing
const B& operator* () const
{
return *i;
}
//! arrow
const B* operator-> () const
{
return i;
}
//! return index corresponding to pointer
size_type index () const
{
return i-p;
}
friend class iterator;
private:
const B* p;
const B* i;
};
//! iterator class for sequential access
typedef RealIterator<const B> const_iterator;
//! begin const_iterator
const_iterator begin () const
......@@ -416,7 +336,7 @@ namespace Dune {
/** This container extends base_array_unmanaged by memory management
with the usual copy semantics providing the full range of
copy constructor, destructor and assignement operators.
copy constructor, destructor and assignment operators.
You can make
......@@ -630,74 +550,57 @@ namespace Dune {
return p[l];
}
// forward declaration
class const_iterator;
//! iterator class for sequential access
class iterator
template<class T>
class RealIterator
: public BidirectionalIteratorFacade<RealIterator<T>, T>
{
public:
//! \brief The unqualified value type
typedef typename Dune::RemoveConst<T>::Type ValueType;
friend class BidirectionalIteratorFacade<RealIterator<const ValueType>, const ValueType>;
friend class BidirectionalIteratorFacade<RealIterator<ValueType>, ValueType>;
friend class RealIterator<const ValueType>;
friend class RealIterator<ValueType>;
//! constructor
iterator ()
RealIterator ()
: p(0), j(0), i(0)
{}
iterator (B* _p, size_type* _j, size_type _i) : p(_p), j(_j), i(_i)
//! constructor
RealIterator (B* _p, size_type* _j, size_type _i)
: p(_p), j(_j), i(_i)
{ }
//! prefix increment
iterator& operator++()
{
++i;
return *this;
}
//! Copy constructor from const iterator.
RealIterator(const RealIterator<const ValueType>& it)
: p(it.p), j(it.j), i(it.i)
{}
/**
* @brief Copy constructor from mutable iterator
*/
RealIterator(const RealIterator<ValueType>& it)
: p(it.p), j(it.j), i(it.i)
{}
//! prefix decrement
iterator& operator--()
{
--i;
return *this;
}
//! equality
bool operator== (const iterator& it) const
bool equals (const RealIterator<ValueType>& it) const
{
// return (p+i)==(it.p+it.i);
assert(p==it.p);
return (i)==(it.i);
}
//! inequality
bool operator!= (const iterator& it) const
{
// return (p+i)!=(it.p+it.i);
return (i)!=(it.i);
}
//! equality
bool operator== (const const_iterator& it) const
bool equals (const RealIterator<const ValueType>& it) const
{
// return (p+i)==(it.p+it.i);
assert(p==it.p);
return (i)==(it.i);
}
//! inequality
bool operator!= (const const_iterator& it) const
{
// return (p+i)!=(it.p+it.i);
return (i)!=(it.i);
}
//! dereferencing
B& operator* () const
{
return p[i];
}
//! arrow
B* operator-> () const
{
return p+i;
}
//! return index corresponding to pointer
size_type index () const
......@@ -723,7 +626,24 @@ namespace Dune {
return i;
}
friend class const_iterator;
protected:
//! prefix increment
void increment()
{
++i;
}
//! prefix decrement
void decrement()
{
--i;
}
//! dereferencing
B& dereference () const
{
return p[i];
}
private:
B* p;
......@@ -731,6 +651,9 @@ namespace Dune {
size_type i;
};
/** @brief The iterator type. */
typedef RealIterator<B> iterator;
//! begin iterator
iterator begin ()
{
......@@ -773,104 +696,7 @@ namespace Dune {
}
//! const_iterator class for sequential access
class const_iterator
{
public:
//! constructor
const_iterator ()
{
p = 0;
j = 0;
i = 0;
}
//! \todo please doc me!
const_iterator (const B* _p, const size_type* _j, size_type _i) : p(_p), j(_j), i(_i)
{ }
//! Copy constructor from a non-const iterator
const_iterator (const iterator& it) : p(it.p), j(it.j), i(it.i)
{ }
//! prefix increment
const_iterator& operator++()
{
++i;
return *this;
}
//! prefix decrement
const_iterator& operator--()
{
--i;
return *this;
}
//! equality
bool operator== (const const_iterator& it) const
{
// return (p+i)==(it.p+it.i);
return (i)==(it.i);
}
//! inequality
bool operator!= (const const_iterator& it) const
{
// return (p+i)!=(it.p+it.i);
return (i)!=(it.i);
}
//! equality
bool operator== (const iterator& it) const
{
// return (p+i)==(it.p+it.i);
return (i)==(it.i);
}
//! inequality
bool operator!= (const iterator& it) const
{
// return (p+i)!=(it.p+it.i);
return (i)!=(it.i);
}
//! dereferencing
const B& operator* () const
{
return p[i];
}
//! arrow
const B* operator-> () const
{
return p+i;
}
//! return index corresponding to pointer
size_type index () const
{
return j[i];
}
/**
* @brief offset from the fist entry.
*
* An iterator positioned at the beginning
* has to be increment this amount of times to
* the same position.
*/
size_type offset () const
{
return i;
}
friend class iterator;
private:
const B* p;
const size_type* j;
size_type i;
};
typedef RealIterator<const B> const_iterator;
//! begin const_iterator
const_iterator begin () const
......
......@@ -15,6 +15,9 @@
#include "allocator.hh"
#include "bvector.hh"
#include <dune/common/stdstreams.hh>
#include <dune/common/iteratorfacades.hh>
#include <dune/common/typetraits.hh>
#include <dune/common/helpertemplates.hh>
/*! \file
* \brief Implementation of the BCRSMatrix class
......@@ -157,96 +160,110 @@ namespace Dune {
//===== iterator interface to rows of the matrix
// forward declaration
class ConstIterator;
//! Iterator access to matrix rows
class Iterator
template<class T>
class RealRowIterator
: public RandomAccessIteratorFacade<RealRowIterator<T>, T>
{
public:
//! \brief The unqualified value type
typedef typename Dune::RemoveConst<T>::Type ValueType;
friend class RandomAccessIteratorFacade<RealRowIterator<const ValueType>, const ValueType>;
friend class RandomAccessIteratorFacade<RealRowIterator<ValueType>, ValueType>;
friend class RealRowIterator<const ValueType>;
friend class RealRowIterator<ValueType>;
//! constructor
Iterator (row_type* _p, size_type _i)
{
p = _p;
i = _i;
}
RealRowIterator (row_type* _p, size_type _i)
: p(_p), i(_i)
{}
//! empty constructor, use with care!
Iterator ()
{ }
RealRowIterator ()
: p(0), i(0)
{}
//! prefix increment
Iterator& operator++()
RealRowIterator(const RealRowIterator<const ValueType>& it)
: p(it.p), i(it.i)
{}
RealRowIterator(const RealRowIterator<ValueType>& it)
: p(it.p), i(it.i)
{}
//! return index
size_type index ()
{
++i;
return *this;
return i;
}
//! prefix decrement
Iterator& operator--()
std::ptrdiff_t distanceTo(RealRowIterator<ValueType> other)
{
--i;
return *this;
assert(other.p==p);
return (other.i-i);
}
//! equality
bool operator== (const Iterator& it) const
std::ptrdiff_t distanceTo(const RealRowIterator<const ValueType> other)
{
// return (p+i)==(it.p+it.i);
return (i)==(it.i);
assert(other.p==p);
return (other.i-i);
}
//! inequality
bool operator!= (const Iterator& it) const
//! equality
bool equals (const RealRowIterator<ValueType>& other) const
{
// return (p+i)!=(it.p+it.i);
return (i)!=(it.i);
assert(other.p==p);
return i==other.i;
}
//! equality
bool operator== (const ConstIterator& it) const
bool equals (const RealRowIterator<const ValueType>& other) const
{
// return (p+i)==(it.p+it.i);
return (i)==(it.i);
assert(other.p==p);
return i==other.i;
}
//! inequality
bool operator!= (const ConstIterator& it) const
protected:
//! prefix increment
void increment()
{
// return (p+i)!=(it.p+it.i);
return (i)!=(it.i);
++i;
}
//! less than
bool operator< (const Iterator& it) const
//! prefix decrement
void decrement()
{
return (i)<(it.i);
--i;
}
//! dereferencing
row_type& operator* () const
void advance(std::ptrdiff_t diff)
{
return p[i];
i+=diff;
}
//! arrow
row_type* operator-> () const
T& elementAt(std::ptrdiff_t diff)
{
return p+i;
return p[i+diff];
}
//! return index
size_type index ()
//! dereferencing
row_type& dereference () const
{
return i;
return p[i];
}
friend class ConstIterator;
private:
row_type* p;
size_type i;
};
//! The iterator over the (mutable matrix rows
typedef RealRowIterator<row_type> Iterator;
//! Get iterator to first row
Iterator begin ()
{
......@@ -277,89 +294,9 @@ namespace Dune {
/** \brief Iterator for the entries of each row */
typedef typename row_type::Iterator ColIterator;
//! The const iterator over the matrix rows
typedef RealRowIterator<const row_type> ConstIterator;
//! Const iterator access to rows
class ConstIterator
{
public:
//! constructor
ConstIterator (const row_type* _p, size_type _i) : p(_p), i(_i)
{ }
//! empty constructor, use with care!
ConstIterator ()
{
p = 0;
i = 0;
}
//! Copy constructor from mutable iterator.
ConstIterator(const Iterator& other)
: p(other.p), i(other.i)
{}
//! prefix increment
ConstIterator& operator++()
{
++i;
return *this;
}
//! prefix decrement
ConstIterator& operator--()
{
--i;
return *this;
}
//! equality
bool operator== (const ConstIterator& it) const
{
return (p+i)==(it.p+it.i);
}
//! inequality
bool operator!= (const ConstIterator& it) const
{
return (p+i)!=(it.p+it.i);
}
//! equality
bool operator== (const Iterator& it) const
{
return (p+i)==(it.p+it.i);
}
//! inequality
bool operator!= (const Iterator& it) const
{
return (p+i)!=(it.p+it.i);
}
//! dereferencing
const row_type& operator* () const
{
return p[i];
}
//! arrow
const row_type* operator-> () const
{
return p+i;
}
//! return index
size_type index () const
{
return i;
}
friend class Iterator;
private:
const row_type* p;
size_type i;
};
//! Get const iterator to first row
ConstIterator begin () const
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment