-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrider.h
135 lines (109 loc) · 3.65 KB
/
strider.h
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright Timothy H. Keitt 2017
// By use of this header, you agree to the following terms:
// 1. Your use conforms to either
// a) the Boost Software License http://www.boost.org/users/license.html or
// b) the license dictated by the R package https://github.com/thk686/strider and
// 2. You acknowledge use of this software in your product by either
// a) place a visible notice somewhere in your product or
// b) cite the software as you would any R package and
// 3. You do not remove these notices from this file
#ifndef __STRIDER_H__
#define __STRIDER_H__
#include <vector>
#include <functional>
#include <boost/iterator/iterator_adaptor.hpp>
namespace strider {
namespace detail {
using boost::iterator_adaptor;
using boost::enable_if_convertible;
using boost::iterator_core_access;
using std::next;
using std::size_t;
using std::vector;
using std::iterator_traits;
template <class Iterator>
class strided_iterator
: public iterator_adaptor<strided_iterator<Iterator>, Iterator>
{
using super_t = iterator_adaptor<strided_iterator<Iterator>, Iterator>;
friend class iterator_core_access;
public:
using difference_type = typename super_t::difference_type;
using value_type = typename super_t::value_type;
using pointer = typename super_t::pointer;
using reference = typename super_t::reference;
using iterator_category = typename super_t::iterator_category;
strided_iterator() {}
explicit strided_iterator(Iterator x, difference_type stride)
: super_t(x), m_stride(stride) {}
template<class OtherIterator>
strided_iterator(
strided_iterator<OtherIterator> const& r,
typename enable_if_convertible<OtherIterator, Iterator>::type* = 0)
: super_t(r.base()), m_stride(r.m_stride) {}
private:
reference dereference() const
{
return *this->base_reference();
}
void increment() { std::advance(this->base_reference(), m_stride); }
void decrement() { std::advance(this->base_reference(), -m_stride); }
void advance(difference_type n)
{
std::advance(this->base_reference(), n * m_stride);
}
template <class OtherIterator>
difference_type
distance_to(strided_iterator<OtherIterator> const& y) const
{
return std::distance(this->base_reference(), y.base()) / m_stride;
}
template <class OtherIterator>
bool equal(strided_iterator<OtherIterator> const& y) const
{
return this->base_reference() == y.base();
}
difference_type m_stride;
};
template<typename T>
inline strided_iterator<T>
make_strided(T iter,
typename iterator_traits<T>::difference_type stride = 0,
typename iterator_traits<T>::difference_type strides = 0)
{
return strided_iterator<T>(next(iter, stride * strides), stride);
}
template<typename T>
class strided_range
{
public:
using difference_type = typename iterator_traits<T>::difference_type;
strided_range(T iter, difference_type stride, difference_type strides)
: m_iter(iter), m_stride(stride), m_strides(strides) {}
strided_iterator<T> begin() const
{
return make_strided(m_iter, m_stride);
}
strided_iterator<T> end() const
{
return make_strided(m_iter, m_stride, m_strides);
}
private:
T m_iter;
difference_type m_stride, m_strides;
};
template<typename T>
inline strided_range<T>
make_strided_range(T iter,
typename iterator_traits<T>::difference_type stride,
typename iterator_traits<T>::difference_type strides)
{
return strided_range<T>(iter, stride, strides);
}
}; // namespace detail
using detail::make_strided;
using detail::strided_range;
using detail::make_strided_range;
using detail::strided_iterator;
}; // namespace strider
#endif // __STRIDER_H__