forked from alabuzhev/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sliding_window.hpp
104 lines (82 loc) · 2.53 KB
/
sliding_window.hpp
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
#ifndef ITER_SLIDING_WINDOW_HPP_
#define ITER_SLIDING_WINDOW_HPP_
#include "internal/iterbase.hpp"
#include "internal/iteratoriterator.hpp"
#include <deque>
#include <utility>
#include <iterator>
namespace iter {
namespace impl {
template <typename Container>
class WindowSlider;
}
template <typename Container>
impl::WindowSlider<Container> sliding_window(Container&&, std::size_t);
}
template <typename Container>
class iter::impl::WindowSlider {
private:
Container container;
std::size_t window_size;
friend WindowSlider iter::sliding_window<Container>(Container&&, std::size_t);
WindowSlider(Container&& in_container, std::size_t win_sz)
: container(std::forward<Container>(in_container)), window_size{win_sz} {}
using IndexVector = std::deque<iterator_type<Container>>;
using DerefVec = IterIterWrapper<IndexVector>;
public:
WindowSlider(WindowSlider&&) = default;
class Iterator : public std::iterator<std::input_iterator_tag, DerefVec> {
private:
iterator_type<Container> sub_iter;
DerefVec window;
public:
Iterator(iterator_type<Container>&& in_iter,
const iterator_type<Container>& in_end, std::size_t window_sz)
: sub_iter(std::move(in_iter)) {
std::size_t i{0};
while (i < window_sz && this->sub_iter != in_end) {
this->window.get().push_back(this->sub_iter);
++i;
if (i != window_sz) { ++this->sub_iter; }
}
}
bool operator!=(const Iterator& other) const {
return this->sub_iter != other.sub_iter;
}
bool operator==(const Iterator& other) const {
return !(*this != other);
}
DerefVec& operator*() {
return this->window;
}
DerefVec* operator->() {
return this->window;
}
Iterator& operator++() {
++this->sub_iter;
this->window.get().pop_front();
this->window.get().push_back(this->sub_iter);
return *this;
}
Iterator operator++(int) {
auto ret = *this;
++*this;
return ret;
}
};
Iterator begin() {
return {(this->window_size != 0 ? std::begin(this->container)
: std::end(this->container)),
std::end(this->container), this->window_size};
}
Iterator end() {
return {std::end(this->container), std::end(this->container),
this->window_size};
}
};
template <typename Container>
iter::impl::WindowSlider<Container> iter::sliding_window(
Container&& container, std::size_t window_size) {
return {std::forward<Container>(container), window_size};
}
#endif