Loading...
Searching...
No Matches
indexediterator.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_COMMON_INDEXEDITERATOR_HH
#define DUNE_COMMON_INDEXEDITERATOR_HH
#include <iterator>
#include <type_traits>
namespace Dune
{
template <class Iter>
class IndexedIterator
: public Iter
{
using Traits = std::iterator_traits<Iter>;
static_assert(std::is_copy_constructible_v<Iter>);
static_assert(std::is_base_of_v<std::forward_iterator_tag, typename Traits::iterator_category>);
public:
using size_type = typename Traits::difference_type;
template <class I = Iter,
std::enable_if_t<std::is_default_constructible_v<I>, bool> = true>
constexpr IndexedIterator ()
noexcept(std::is_nothrow_default_constructible_v<Iter>)
{}
constexpr IndexedIterator (Iter it, size_type index = 0)
noexcept(std::is_nothrow_copy_constructible_v<Iter>)
: Iter(it)
, index_(index)
{}
[[nodiscard]] constexpr size_type index () const noexcept
{
return index_;
}
constexpr IndexedIterator& operator++ ()
{
Iter::operator++();
++index_;
return *this;
}
constexpr IndexedIterator operator++ (int)
{
IndexedIterator tmp(*this);
this->operator++();
return tmp;
}
template <class I = Iter,
decltype(--std::declval<I&>(), bool{}) = true>
constexpr IndexedIterator& operator-- ()
{
Iter::operator--();
--index_;
return *this;
}
template <class I = Iter,
decltype(std::declval<I&>()--, bool{}) = true>
constexpr IndexedIterator operator-- (int)
{
IndexedIterator tmp(*this);
this->operator--();
return tmp;
}
template <class I = Iter,
decltype(std::declval<I&>()+=1, bool{}) = true>
constexpr IndexedIterator& operator+= (typename Iter::difference_type n)
{
Iter::operator+=(n);
index_ += n;
return *this;
}
template <class I = Iter,
decltype(std::declval<I&>()-=1, bool{}) = true>
constexpr IndexedIterator& operator-= (typename Iter::difference_type n)
{
Iter::operator-=(n);
index_ -= n;
return *this;
}
private:
size_type index_ = 0;
};
} // end namespace Dune
#endif // DUNE_COMMON_INDEXEDITERATOR_HH
std
STL namespace.
Dune
Dune namespace
Definition alignedallocator.hh:13
Dune::IndexedIterator::operator++
constexpr IndexedIterator & operator++()
Increment the iterator and the index.
Definition indexediterator.hh:60
Dune::IndexedIterator::operator--
constexpr IndexedIterator & operator--()
Decrement the iterator and the index.
Definition indexediterator.hh:78
Dune::IndexedIterator::index
constexpr size_type index() const noexcept
Return the enumeration index.
Definition indexediterator.hh:54
Dune::IndexedIterator::IndexedIterator
constexpr IndexedIterator() noexcept(std::is_nothrow_default_constructible_v< Iter >)
Default constructor default-constructs the Iter base type and the index by 0.
Definition indexediterator.hh:41
Dune::IndexedIterator::IndexedIterator
constexpr IndexedIterator(Iter it, size_type index=0) noexcept(std::is_nothrow_copy_constructible_v< Iter >)
Definition indexediterator.hh:47
Dune::IndexedIterator::size_type
typename Traits::difference_type size_type
Type used for storing the traversal index.
Definition indexediterator.hh:36