-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathStrideIterator.h
More file actions
72 lines (63 loc) · 2.21 KB
/
StrideIterator.h
File metadata and controls
72 lines (63 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#ifndef RCPP_vector_StrideIterator_h
#define RCPP_vector_StrideIterator_h
namespace Rcpp{
template <typename iterator>
class StrideIterator {
public:
using value_type = typename std::iterator_traits<iterator>::value_type ;
using reference = typename std::iterator_traits<iterator>::reference ;
using pointer = typename std::iterator_traits<iterator>::pointer ;
StrideIterator( iterator it_, int n_ ) : it(it_), n(n_){}
StrideIterator& operator++(){
it += n ;
return *this ;
}
StrideIterator operator++(int){
StrideIterator orig(it,n) ;
it += n ;
return orig ;
}
StrideIterator& operator--(){
it -= n ;
return *this ;
}
StrideIterator operator--(int){
StrideIterator orig(it,n) ;
it -= n ;
return orig ;
}
StrideIterator operator+( int m ) const {
return StrideIterator( it + m*n, n );
}
StrideIterator operator-( int m ) const {
return StrideIterator( it - m*n, n );
}
StrideIterator& operator+=( int m ){
it += n*m ;
return *this ;
}
StrideIterator& operator-=( int m ){
it -= n*m ;
return *this ;
}
int operator-( const StrideIterator& other){
return (other.it - it) / n ;
}
bool operator==( const StrideIterator& other) { return it == other.it ; }
bool operator!=( const StrideIterator& other) { return it != other.it ; }
bool operator<( const StrideIterator& other ) { return it < other.it ; }
bool operator>( const StrideIterator& other ) { return it > other.it ; }
bool operator<=( const StrideIterator& other ) { return it <= other.it ; }
bool operator>=( const StrideIterator& other ) { return it >= other.it ; }
inline reference operator[](int i){
return it[i*n] ;
}
inline reference operator*() {
return *it ;
}
private:
iterator it ;
int n ;
} ;
}
#endif