forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcycle.hpp
More file actions
109 lines (87 loc) · 3.23 KB
/
Copy pathcycle.hpp
File metadata and controls
109 lines (87 loc) · 3.23 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
#ifndef ITER_CYCLE_H_
#define ITER_CYCLE_H_
#include "iterbase.hpp"
#include <utility>
#include <iterator>
#include <initializer_list>
namespace iter {
template <typename Container>
class Cycle;
template <typename Container>
Cycle<Container> cycle(Container&&);
template <typename T>
Cycle<std::initializer_list<T>> cycle(std::initializer_list<T>);
template <typename Container>
class Cycle {
private:
friend Cycle cycle<Container>(Container&&);
template <typename T>
friend Cycle<std::initializer_list<T>> cycle(
std::initializer_list<T>);
Container container;
Cycle(Container&& in_container)
: container(std::forward<Container>(in_container))
{ }
public:
class Iterator
: public std::iterator<std::input_iterator_tag,
iterator_traits_deref<Container>>
{
private:
using iter_type = iterator_type<Container>;
iterator_type<Container> sub_iter;
iterator_type<Container> begin;
iterator_type<Container> end;
public:
Iterator (const iterator_type<Container>& iter,
iterator_type<Container>&& in_end)
: sub_iter{iter},
begin{iter},
end{std::move(in_end)}
{ }
iterator_deref<Container> operator*() {
return *this->sub_iter;
}
iterator_arrow<Container> operator->() {
return apply_arrow(this->sub_iter);
}
Iterator& operator++() {
++this->sub_iter;
// reset to beginning upon reaching the end
if (!(this->sub_iter != this->end)) {
this->sub_iter = this->begin;
}
return *this;
}
Iterator operator++(int) {
auto ret = *this;
++*this;
return ret;
}
bool operator!=(const Iterator& other) const {
return this->sub_iter != other.sub_iter;
}
bool operator==(const Iterator& other) const {
return !(*this != other);
}
};
Iterator begin() {
return {std::begin(this->container),
std::end(this->container)};
}
Iterator end() {
return {std::end(this->container),
std::end(this->container)};
}
};
template <typename Container>
Cycle<Container> cycle(Container&& container) {
return {std::forward<Container>(container)};
}
template <typename T>
Cycle<std::initializer_list<T>> cycle(std::initializer_list<T> il)
{
return {std::move(il)};
}
}
#endif