Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dune-common
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Timo Koch
dune-common
Commits
79d81d37
Commit
79d81d37
authored
20 years ago
by
Christian Engwer
Browse files
Options
Downloads
Patches
Plain Diff
* Doxygen Doku
* missing namespace added [[Imported from SVN: r1111]]
parent
ff7b8625
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
common/genericiterator.hh
+138
-55
138 additions, 55 deletions
common/genericiterator.hh
with
138 additions
and
55 deletions
common/genericiterator.hh
+
138
−
55
View file @
79d81d37
...
...
@@ -6,77 +6,160 @@
#include
<dune/common/iteratorfacades.hh>
template
<
class
C
,
class
T
>
class
GenericIterator
:
public
Dune
::
RandomAccessIteratorFacade
<
GenericIterator
<
C
,
T
>
,
T
,
T
&
,
int
>
namespace
Dune
{
friend
class
GenericIterator
<
typename
Dune
::
RemoveConst
<
C
>::
Type
,
typename
Dune
::
RemoveConst
<
T
>::
Type
>
;
friend
class
GenericIterator
<
const
typename
Dune
::
RemoveConst
<
C
>::
Type
,
const
typename
Dune
::
RemoveConst
<
T
>::
Type
>
;
public:
/*! \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 This file implements a generic iterator classes for writing stl conformant iterators.
*
* With using this generic iterator writing iterators for containers
* with operator[] is only a matter of seconds
*/
/** @addtogroup GenericIterator
*
* @{
*/
/**
* @brief Generic class for stl conformant iterators for container classes with operator[].
*
*/
template
<
class
C
,
class
T
>
class
GenericIterator
:
public
Dune
::
RandomAccessIteratorFacade
<
GenericIterator
<
C
,
T
>
,
T
,
T
&
,
int
>
{
friend
class
GenericIterator
<
typename
Dune
::
RemoveConst
<
C
>::
Type
,
typename
Dune
::
RemoveConst
<
T
>::
Type
>
;
friend
class
GenericIterator
<
const
typename
Dune
::
RemoveConst
<
C
>::
Type
,
const
typename
Dune
::
RemoveConst
<
T
>::
Type
>
;
// Constructors needed by the base iterators.
GenericIterator
()
:
container_
(
0
),
position_
(
0
)
{}
public:
GenericIterator
(
C
&
cont
,
int
pos
)
:
container_
(
&
cont
),
position_
(
pos
)
{}
// Constructors needed by the base iterators.
GenericIterator
()
:
container_
(
0
),
position_
(
0
)
{}
GenericIterator
(
const
GenericIterator
<
typename
Dune
::
RemoveConst
<
C
>::
Type
,
typename
Dune
::
RemoveConst
<
T
>::
Type
>&
other
)
:
container_
(
other
.
container_
),
position_
(
other
.
position_
)
{}
GenericIterator
(
C
&
cont
,
int
pos
)
:
container_
(
&
cont
),
position_
(
pos
)
{}
GenericIterator
(
const
GenericIterator
<
typename
Dune
::
RemoveConst
<
C
>::
Type
,
typename
Dune
::
RemoveConst
<
T
>::
Type
>&
other
)
:
container_
(
other
.
container_
),
position_
(
other
.
position_
)
{}
GenericIterator
(
const
GenericIterator
<
const
typename
Dune
::
RemoveConst
<
C
>::
Type
,
const
typename
Dune
::
RemoveConst
<
T
>::
Type
>&
other
)
:
container_
(
other
.
container_
),
position_
(
other
.
position_
)
{}
// Methods needed by the forward iterator
bool
equals
(
const
GenericIterator
<
typename
Dune
::
RemoveConst
<
C
>::
Type
,
typename
Dune
::
RemoveConst
<
T
>::
Type
>&
other
)
const
{
return
position_
==
other
.
position_
&&
container_
==
other
.
container_
;
}
GenericIterator
(
const
GenericIterator
<
const
typename
Dune
::
RemoveConst
<
C
>::
Type
,
const
typename
Dune
::
RemoveConst
<
T
>::
Type
>&
other
)
:
container_
(
other
.
container_
),
position_
(
other
.
position_
)
{}
// Methods needed by the forward iterator
bool
equals
(
const
GenericIterator
<
typename
Dune
::
RemoveConst
<
C
>::
Type
,
typename
Dune
::
RemoveConst
<
T
>::
Type
>&
other
)
const
{
return
position_
==
other
.
position_
&&
container_
==
other
.
container_
;
}
bool
equals
(
const
GenericIterator
<
const
typename
Dune
::
RemoveConst
<
C
>::
Type
,
const
typename
Dune
::
RemoveConst
<
T
>::
Type
>&
other
)
const
{
return
position_
==
other
.
position_
&&
container_
==
other
.
container_
;
}
T
&
dereference
()
const
{
return
container_
->
operator
[](
position_
);
}
bool
equals
(
const
GenericIterator
<
const
typename
Dune
::
RemoveConst
<
C
>::
Type
,
const
typename
Dune
::
RemoveConst
<
T
>::
Type
>&
other
)
const
{
return
position_
==
other
.
position_
&&
container_
==
other
.
container_
;
}
void
increment
()
{
++
position_
;
}
T
&
dereference
()
const
{
return
container_
->
operator
[](
position_
)
;
}
// Additional function needed by BidirectionalIterator
void
decrement
(){
--
position_
;
}
void
increment
(){
++
position_
;
}
// Additional function needed by
RandomAccess
Iterator
T
&
elementAt
(
int
i
)
const
{
return
container_
->
operator
[](
position_
+
i
)
;
}
// Additional function needed by
Bidirectional
Iterator
void
decrement
()
{
--
position_
;
}
void
advance
(
int
n
){
position_
=
position_
+
n
;
}
// Additional function needed by RandomAccessIterator
T
&
elementAt
(
int
i
)
const
{
return
container_
->
operator
[](
position_
+
i
);
}
std
::
ptrdiff_t
distanceTo
(
GenericIterator
<
const
typename
Dune
::
RemoveConst
<
C
>::
Type
,
const
typename
Dune
::
RemoveConst
<
T
>::
Type
>
other
)
const
{
assert
(
other
.
container_
==
container_
);
return
other
.
position_
-
position_
;
}
void
advance
(
int
n
){
position_
=
position_
+
n
;
}
std
::
ptrdiff_t
distanceTo
(
GenericIterator
<
typename
Dune
::
RemoveConst
<
C
>::
Type
,
typename
Dune
::
RemoveConst
<
T
>::
Type
>
other
)
const
{
assert
(
other
.
container_
==
container_
);
return
other
.
position_
-
position_
;
}
private
:
C
*
container_
;
size_t
position_
;
};
std
::
ptrdiff_t
distanceTo
(
GenericIterator
<
const
typename
Dune
::
RemoveConst
<
C
>::
Type
,
const
typename
Dune
::
RemoveConst
<
T
>::
Type
>
other
)
const
{
assert
(
other
.
container_
==
container_
);
return
other
.
position_
-
position_
;
}
std
::
ptrdiff_t
distanceTo
(
GenericIterator
<
typename
Dune
::
RemoveConst
<
C
>::
Type
,
typename
Dune
::
RemoveConst
<
T
>::
Type
>
other
)
const
{
assert
(
other
.
container_
==
container_
);
return
other
.
position_
-
position_
;
}
private
:
C
*
container_
;
size_t
position_
;
};
/** @} */
}
#endif
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment