29 #ifndef _H_GRB_ITERATOR_REGULAR 30 #define _H_GRB_ITERATOR_REGULAR 48 template<
typename T >
51 template<
typename T >
80 class PosBasedIterator {
94 static constexpr
const size_t block_size = 256;
125 const size_t count,
const size_t pos,
126 const R val,
const T state
128 _count( count ), _pos( pos ), _val( val ), _state( state )
136 typedef std::random_access_iterator_tag iterator_category;
138 typedef size_t difference_type;
140 typedef R value_type;
142 typedef const R * pointer;
148 typedef PosBasedIterator< R, T, SelfType > self_type;
150 typedef self_type & self_reference_type;
152 typedef const PosBasedIterator< R, T, SelfType > self_const_reference_type;
193 _count( count ), _val( dummy ), _state( state )
196 static_assert( block_size > 0,
197 "Regular iterator: internal block size must be larger than zero" );
199 if( P == 0 || s >= P ) {
200 throw std::runtime_error(
"Illegal values for s and/or P" );
203 size_t entries_per_process = count;
204 if( P > 1 && count > block_size ) {
205 const size_t bcount = (count % block_size) > 0
206 ? count / block_size + 1
207 : count / block_size;
208 const size_t bcount_per_process = (bcount % P) > 0
211 entries_per_process = bcount_per_process * block_size;
214 _count = (s+1) * entries_per_process;
215 if( _count > count ) { _count = count; }
218 ? s * entries_per_process
221 if( _pos > _count ) { _pos = _count; }
223 if( _pos != count ) {
224 SelfType::func( _val, state, _pos );
233 PosBasedIterator(
const self_const_reference_type &other ) :
234 _count( other._count ), _pos( other._pos ),
235 _val( other._val ), _state( other._state )
243 PosBasedIterator( PosBasedIterator< R, T, SelfType > &&other ) :
244 _count( other._count ), _pos( other._pos )
246 _val = std::move( other._val );
247 _state = std::move( other._state );
248 other._count = other._pos = 0;
256 ~PosBasedIterator() {}
276 self_reference_type operator=( self_const_reference_type other ) noexcept {
277 _count = other._count;
280 _state = other._state;
291 self_reference_type operator=(
292 PosBasedIterator< R, T, SelfType > &&other
294 _count = other._count;
296 _val = std::move( other._val );
297 _state = std::move( other._state );
298 other._count = other._pos = 0;
313 self_reference_type operator++() noexcept {
314 assert( _pos < _count );
316 if( _pos != _count ) {
317 SelfType::func( _val, _state, _pos );
332 friend void swap( self_reference_type left, self_reference_type right ) {
333 std::swap( left._count, right._count );
334 std::swap( left._pos, right._pos );
335 std::swap( left._val, right._val );
336 std::swap( left._state, right._state );
352 pointer operator->() const noexcept {
365 self_type operator++(
int) noexcept {
366 assert( _pos < _count );
368 SelfType::create_iterator( _count, _pos, _val, _state );
370 if( _pos != _count ) {
371 SelfType::func( _val, _state, _pos );
388 friend bool operator==(
389 self_const_reference_type left, self_const_reference_type right
391 assert( left._count == right._count );
392 return left._pos == right._pos;
407 friend bool operator!=(
408 self_const_reference_type left, self_const_reference_type right
410 return !(left == right);
425 self_reference_type operator--() noexcept {
428 if( _pos < _count ) {
429 SelfType::func( _val, _state, _pos );
444 self_type operator--(
int) noexcept {
447 SelfType::make_iterator( _pos, _count, _val, _pos );
449 if( _pos < _count ) {
450 SelfType::func( _val, _state, _pos );
476 value_type operator[](
const size_t i )
const noexcept {
477 assert( i < PosBasedIterator::_count );
479 SelfType::func( ret, _state, i );
500 friend bool operator<(
501 self_const_reference_type left,
502 self_const_reference_type right
504 assert( left._count == right._count );
505 return left._pos < right._pos;
525 friend bool operator>(
526 self_const_reference_type left,
527 self_const_reference_type right
529 return left._count == right._count &&
530 left._state == right._state &&
531 left._pos > right._pos;
552 friend bool operator<=(
553 self_const_reference_type left,
554 self_const_reference_type right
556 return left._count == right._count &&
557 left._state == right._state &&
558 left._pos <= right._pos;
579 friend bool operator>=(
580 self_const_reference_type left,
581 self_const_reference_type right
583 return left._count == right._count &&
584 left._state == right._state &&
585 left._pos >= right._pos;
605 self_reference_type operator+=(
const size_t count ) noexcept {
606 assert( _pos + count <= _count );
608 if( _pos != _count ) {
609 SelfType::func( _val, _state, _pos );
632 friend self_type operator+(
633 self_const_reference_type iterator,
636 assert( iterator._pos + count <= iterator._count );
637 const size_t pos = iterator._pos + count;
638 R val = iterator._val;
639 if( pos != iterator._count ) {
640 SelfType::func( val, iterator._state, pos );
643 iterator._count, pos,
656 friend self_type operator+(
658 self_const_reference_type iterator
660 assert( iterator._pos + count <= iterator._count );
661 const size_t pos = iterator._pos + count;
662 R val = iterator._val;
663 if( pos != iterator._count ) {
664 SelfType::func( val, iterator._state, pos );
667 iterator._count, pos,
689 self_reference_type operator-=(
const size_t count ) noexcept {
690 assert( _pos >= count );
713 friend self_type operator-(
714 self_const_reference_type iterator,
717 assert( iterator._pos >= count );
718 const size_t pos = iterator._pos - count;
719 R val = iterator._val;
720 if( pos != iterator._count ) {
721 SelfType::func( val, iterator._state, pos );
724 iterator._count, pos,
737 friend self_type operator-(
739 self_const_reference_type iterator
741 assert( iterator._pos >= count );
742 const size_t pos = iterator._pos - count;
743 R val = iterator._val;
744 if( pos != iterator._count ) {
745 SelfType::func( val, iterator._state, pos );
748 iterator._count, pos,
767 difference_type operator-(
768 self_const_reference_type iterator
770 assert( iterator._count == _count );
771 assert( iterator._state == _state );
772 return static_cast< difference_type >( _pos - iterator._pos );
779 namespace iterators {
791 template<
typename T >
794 friend class internal::PosBasedIterator< T, T, Repeater< T > >;
806 typedef grb::utils::internal::PosBasedIterator< T, T, Repeater< T > >
834 return RealType( count, pos, val, state );
845 inline static void func( T&,
const T&,
const size_t ) {}
876 return RealType( count, start, val, val, s, P );
899 template<
typename T >
902 friend class internal::PosBasedIterator<
903 T, std::tuple< size_t, size_t, size_t >,
916 typedef grb::utils::internal::PosBasedIterator<
917 T, std::tuple< size_t, size_t, size_t >, Sequence< T >
943 const std::tuple< size_t, size_t, size_t > state
945 return RealType( count, pos, val, state );
954 inline static void func(
956 const std::tuple< size_t, size_t, size_t > &state,
959 const size_t offset = std::get<0>(state);
960 const size_t stride = std::get<1>(state);
961 const size_t repetitions = std::get<2>(state);
962 val = offset + ( pos / repetitions ) * stride;
992 const size_t offset = static_cast< size_t >( 0 ),
993 const size_t stride = static_cast< size_t >( 1 ),
994 const size_t repetitions = static_cast< size_t >( 1 ),
1002 std::tuple< size_t, size_t, size_t >( offset, stride, repetitions ),
1013 namespace containers {
1025 template<
typename T >
1162 template<
typename T =
size_t >
1171 const size_t _start, _end;
1174 const size_t _stride;
1177 const size_t _repetitions;
1180 const size_t _count;
1224 const size_t start,
const size_t end,
1225 const size_t stride = static_cast< size_t >( 1 ),
1226 const size_t repetitions = static_cast< size_t >( 1 )
1231 _repetitions( repetitions ),
1235 : ( (
end-start) % stride > 0
1236 ? ((
end-start) / stride + 1)
1237 : (
end-start) / stride
1241 assert( start <=
end );
1275 _repetitions, T(), s, P );
1309 _repetitions, T(), s, P );
1321 _repetitions, T(), s, P );
1332 _repetitions, T(), s, P );
1343 #endif // end _H_GRB_ITERATOR_REGULAR const_iterator cbegin(const size_t s=0, const size_t P=1) const
Returns a const-iterator at start position to this container.
Definition: regular.hpp:1319
static RealType make_iterator(const size_t count, const bool start, const T val, const size_t s=0, const size_t P=1)
Constructs an iterator over a collection that contains the same constant value val count times.
Definition: regular.hpp:869
const_iterator cbegin(const size_t s=0, const size_t P=1) const
Returns a const-iterator at start position to this container.
Definition: regular.hpp:1136
iterator begin(const size_t s=0, const size_t P=1) const
Returns a const-iterator at start position to this container.
Definition: regular.hpp:1273
An iterator that repeats the same value for a set number of times.
Definition: regular.hpp:49
iterator end(const size_t s=0, const size_t P=1) const
Returns a const-iterator at end position to this container.
Definition: regular.hpp:1307
iterator begin(const size_t s=0, const size_t P=1) const
Returns a const-iterator at start position to this container.
Definition: regular.hpp:1092
grb::utils::iterators::Repeater< T >::RealType iterator
The iterator type.
Definition: regular.hpp:1043
iterator end(const size_t s=0, const size_t P=1) const
Returns a const-iterator at end position to this container.
Definition: regular.hpp:1125
An iterator over a collection of items that for each item returns , where is of the form .
Definition: regular.hpp:52
iterator const_iterator
The const-iterator type.
Definition: regular.hpp:1051
The sequential reference implementation.
Definition: backends.hpp:55
ConstantVector(const T val, const size_t n)
Constructs a container with memory usage that represents some vector of length n with contents .
Definition: regular.hpp:1061
A (dense) vector of a given size that holds the same constant value at each entry.
Definition: regular.hpp:1026
The ALP/GraphBLAS namespace.
Definition: graphblas.hpp:477
const_iterator cend(const size_t s=0, const size_t P=1) const
Returns a const-iterator at end position to this container.
Definition: regular.hpp:1330
A container that contains a sequence of numbers with a given stride, and optionally a given number of...
Definition: regular.hpp:1163
iterator const_iterator
The const-iterator type.
Definition: regular.hpp:1194
grb::utils::internal::PosBasedIterator< T, std::tuple< size_t, size_t, size_t >, Sequence< T > > RealType
The return type of the factory methods.
Definition: regular.hpp:918
static RealType make_iterator(const size_t count, const bool start, const size_t offset=static_cast< size_t >(0), const size_t stride=static_cast< size_t >(1), const size_t repetitions=static_cast< size_t >(1), T dummy=T(), const size_t s=0, const size_t P=1)
Constructs an iterator over a given sequence.
Definition: regular.hpp:989
const_iterator cend(const size_t s=0, const size_t P=1) const
Returns a const-iterator at end position to this container.
Definition: regular.hpp:1146
grb::utils::internal::PosBasedIterator< T, T, Repeater< T > > RealType
The return type of the factory methods.
Definition: regular.hpp:807
Range(const size_t start, const size_t end, const size_t stride=static_cast< size_t >(1), const size_t repetitions=static_cast< size_t >(1)) noexcept
Constructs a range.
Definition: regular.hpp:1223
grb::utils::iterators::Sequence< T >::RealType iterator
The iterator type.
Definition: regular.hpp:1186