forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsliding_window.hpp
More file actions
118 lines (96 loc) · 3.7 KB
/
sliding_window.hpp
File metadata and controls
118 lines (96 loc) · 3.7 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
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
#ifndef SLIDING_WINDOW_HPP_
#define SLIDING_WINDOW_HPP_
#include "iterbase.hpp"
#include <vector>
#include <algorithm>
#include <functional>
#include <type_traits>
#include <utility>
#include <iterator>
namespace iter {
template <typename Container>
class SlidingWindow;
template <typename Container>
SlidingWindow<Container> sliding_window(Container&&, std::size_t);
template <typename T>
SlidingWindow<std::initializer_list<T>> sliding_window(
std::initializer_list<T>, std::size_t);
template <typename Container>
class SlidingWindow {
private:
Container container;
std::size_t window_size;
friend SlidingWindow sliding_window<Container>(
Container&&, std::size_t);
template <typename T>
friend SlidingWindow<std::initializer_list<T>> sliding_window(
std::initializer_list<T>, std::size_t);
SlidingWindow(Container container, std::size_t win_sz)
: container(std::forward<Container>(container)),
window_size{win_sz}
{ }
public:
class Iterator {
private:
// confusing, but, just makes the type of the vector
// returned by operator*()
using OpDerefElemType =
std::reference_wrapper<
typename std::remove_reference<
iterator_deref<Container>>::type>;
using DerefVec = std::vector<OpDerefElemType>;
std::vector<iterator_type<Container>> section;
std::size_t section_size = 0;
public:
Iterator(Container& container, std::size_t s)
: section_size{s}
{
auto iter = std::begin(container);
auto end = std::end(container);
for (std::size_t i = 0;
i < section_size && iter != end;
++iter, ++i) {
section.push_back(iter);
}
}
// for the end iter
Iterator(Container& container)
: section{std::end(container)},
section_size{0}
{ }
Iterator& operator++() {
for (auto&& iter : this->section) {
++iter;
}
return *this;
}
bool operator!=(const Iterator& rhs) const {
return this->section.back() != rhs.section.back();
}
DerefVec operator*() {
DerefVec vec;
for (auto&& iter : this->section) {
vec.push_back(*iter);
}
return vec;
}
};
Iterator begin() {
return {container, window_size};
}
Iterator end() {
return {container};
}
};
template <typename Container>
SlidingWindow<Container> sliding_window(
Container&& container, std::size_t window_size) {
return {std::forward<Container>(container), window_size};
}
template <typename T>
SlidingWindow<std::initializer_list<T>> sliding_window(
std::initializer_list<T> il, std::size_t window_size) {
return {il, window_size};
}
}
#endif //SLIDING_WINDOW_HPP_