| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception |
| 3 |
#ifndef DUNE_PYTHON_ISTL_SLICE_HH |
| 4 |
#define DUNE_PYTHON_ISTL_SLICE_HH |
| 5 |
|
| 6 |
#include <cassert> |
| 7 |
|
| 8 |
#include <type_traits> |
| 9 |
|
| 10 |
#include <dune/common/iteratorfacades.hh> |
| 11 |
|
| 12 |
namespace Dune |
| 13 |
{ |
| 14 |
|
| 15 |
namespace Python |
| 16 |
{ |
| 17 |
|
| 18 |
// ArraySlice |
| 19 |
// ---------- |
| 20 |
|
| 21 |
template< class A > |
| 22 |
struct ArraySlice |
| 23 |
{ |
| 24 |
typedef typename A::member_type member_type; |
| 25 |
typedef typename A::size_type size_type; |
| 26 |
|
| 27 |
typedef decltype( std::declval< A & >()[ 0 ] ) reference; |
| 28 |
typedef decltype( std::declval< const A & >()[ 0 ] ) const_reference; |
| 29 |
|
| 30 |
private: |
| 31 |
template< class T > |
| 32 |
struct IteratorImpl |
| 33 |
: public RandomAccessIteratorFacade< IteratorImpl< T >, T > |
| 34 |
{ |
| 35 |
friend class IteratorImpl< std::add_const_t< T > >; |
| 36 |
friend class IteratorImpl< std::remove_const_t< T > >; |
| 37 |
|
| 38 |
friend class RandomAccessIteratorFacade< IteratorImpl< std::add_const_t< T > >, std::add_const_t< T > >; |
| 39 |
friend class RandomAccessIteratorFacade< IteratorImpl< std::remove_const_t< T > >, std::remove_const_t< T > >; |
| 40 |
|
| 41 |
IteratorImpl () noexcept = default; |
| 42 |
IteratorImpl ( T &array, size_type start, size_type step ) noexcept : array_( &array ), index_( start ), step_( step ) {} |
| 43 |
IteratorImpl ( const IteratorImpl< std::remove_const_t< T > > &other ) noexcept : array_( other.array_ ), index_( other.index_ ), step_( other.step_ ) {} |
| 44 |
|
| 45 |
private: |
| 46 |
bool equals ( const IteratorImpl &other ) const noexcept { return (index_ == other.index_); } |
| 47 |
|
| 48 |
std::ptrdiff_t distanceTo ( const IteratorImpl &other ) const noexcept { return (other.index_ - index_) / step_; } |
| 49 |
|
| 50 |
reference elementAt ( std::ptrdiff_t i ) const noexcept( noexcept( std::declval< T & >()[ 0 ] ) ) { return (*array_)[ index_ ]; } |
| 51 |
|
| 52 |
void increment () noexcept { index_ += step_; } |
| 53 |
void decrement () noexcept { index_ -= step_; } |
| 54 |
void advance ( std::ptrdiff_t i ) noexcept { index_ += i*step_; } |
| 55 |
|
| 56 |
T *array_ = nullptr; |
| 57 |
size_type index_ = 0, step_ = 0; |
| 58 |
}; |
| 59 |
|
| 60 |
public: |
| 61 |
typedef IteratorImpl< const A > const_iterator; |
| 62 |
typedef IteratorImpl< A > iterator; |
| 63 |
|
| 64 |
ArraySlice ( A &array, size_type start, size_type step, size_type size ) noexcept |
| 65 |
: array_( array ), start_( start ), step_( step ), size_( size ) |
| 66 |
{} |
| 67 |
|
| 68 |
reference operator[] ( size_type i ) noexcept( noexcept( std::declval< A & >()[ 0 ] ) ) { return array_[ start_ + i*step_ ]; } |
| 69 |
const_reference operator[] ( size_type i ) const noexcept( noexcept( std::declval< const A & >()[ 0 ] ) ) { return array_[ start_ + i*step_ ]; } |
| 70 |
|
| 71 |
const_iterator begin () const noexcept { return const_iterator( array_, start_, step_ ); } |
| 72 |
iterator begin () noexcept { return iterator( array_, start_, step_); } |
| 73 |
|
| 74 |
const_iterator end () const noexcept { return const_iterator( array_, start_ + step_*size_, step_ ); } |
| 75 |
iterator end () noexcept { return iterator( array_, start_ + step_*size_, step_ ); } |
| 76 |
|
| 77 |
const_iterator beforeEnd () const noexcept { return const_iterator( array_, start_ + step_*(size_-1), step_ ); } |
| 78 |
iterator beforeEnd () noexcept { return iterator( array_, start_ + step_*(size_-1), step_ ); } |
| 79 |
|
| 80 |
const_iterator beforeBegin () const noexcept { return const_iterator( array_, start_ - step_, step_ ); } |
| 81 |
iterator beforeBegin () noexcept { return iterator( array_, start_ - step_, step_ ); } |
| 82 |
|
| 83 |
const_iterator find ( size_type i ) const noexcept { return iterator( array_, start_ + step_*std::min( i, size_ ), step_ ); } |
| 84 |
iterator find ( size_type i ) noexcept { return iterator( array_, start_ + step_*std::min( i, size_ ), step_ ); } |
| 85 |
|
| 86 |
size_type size () const noexcept { return size_; } |
| 87 |
|
| 88 |
private: |
| 89 |
A &array_; |
| 90 |
size_type start_, step_, size_; |
| 91 |
}; |
| 92 |
|
| 93 |
} // namespace Python |
| 94 |
|
| 95 |
} // namespace Dune |
| 96 |
|
| 97 |
#endif // #ifndef DUNE_PYTHON_ISTL_SLICE_HH |