forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enumerate.hpp
116 lines (93 loc) · 3.46 KB
/
enumerate.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
105
106
107
108
109
110
111
112
113
114
115
116
#ifndef ENUMERABLE__H__
#define ENUMERABLE__H__
#include "iterbase.hpp"
#include <utility>
#include <iterator>
#include <functional>
#include <initializer_list>
#include <type_traits>
// enumerate functionality for python-style for-each enumerate loops
// for (auto e : enumerate(vec)) {
// std::cout << e.index
// << ": "
// << e.element
// << '\n';
// }
namespace iter {
//Forward declarations of Enumerable and enumerate
template <typename Container>
class Enumerable;
template <typename Container>
Enumerable<Container> enumerate(Container&&);
template <typename T>
Enumerable<std::initializer_list<T>> enumerate(std::initializer_list<T>);
template <typename Container>
class Enumerable {
private:
Container container;
// The only thing allowed to directly instantiate an Enumerable is
// the enumerate function
friend Enumerable enumerate<Container>(Container&&);
template <typename T>
friend Enumerable<std::initializer_list<T>> enumerate(
std::initializer_list<T>);
// Value constructor for use only in the enumerate function
Enumerable(Container container)
: container(std::forward<Container>(container))
{ }
public:
// "yielded" by the Enumerable::Iterator. Has a .index, and a
// .element referencing the value yielded by the subiterator
class IterYield {
public:
std::size_t index;
iterator_deref<Container> element;
IterYield(std::size_t i, iterator_deref<Container> elem)
: index{i},
element{elem}
{ }
};
// Holds an iterator of the contained type and a size_t for the
// index. Each call to ++ increments both of these data members.
// Each dereference returns an IterYield.
class Iterator {
private:
iterator_type<Container> sub_iter;
std::size_t index;
public:
Iterator (iterator_type<Container> si)
: sub_iter{si},
index{0}
{ }
IterYield operator*() {
return IterYield(this->index, *this->sub_iter);
}
Iterator& operator++() {
++this->sub_iter;
++this->index;
return *this;
}
bool operator!=(const Iterator& other) const {
return this->sub_iter != other.sub_iter;
}
};
Iterator begin() {
return {std::begin(this->container)};
}
Iterator end() {
return {std::end(this->container)};
}
};
template <typename Container>
Enumerable<Container> enumerate(Container&& container) {
return {std::forward<Container>(container)};
}
// for initializer lists. copy constructs the list into the Enumerable
template <typename T>
Enumerable<std::initializer_list<T>> enumerate(
std::initializer_list<T> il)
{
return {il};
}
}
#endif //ifndef ENUMERABLE__H__