forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccumulate.hpp
More file actions
161 lines (132 loc) · 4.75 KB
/
Copy pathaccumulate.hpp
File metadata and controls
161 lines (132 loc) · 4.75 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#ifndef ITER_ACCUMULATE_H_
#define ITER_ACCUMULATE_H_
#include "internal/iterbase.hpp"
#include <utility>
#include <iterator>
#include <initializer_list>
#include <functional>
#include <type_traits>
#include <memory>
namespace iter {
namespace impl {
template <typename Container, typename AccumulateFunc>
class Accumulator;
}
template <typename Container, typename AccumulateFunc>
impl::Accumulator<Container, AccumulateFunc> accumulate(
Container&&, AccumulateFunc);
template <typename T, typename AccumulateFunc>
impl::Accumulator<std::initializer_list<T>, AccumulateFunc> accumulate(
std::initializer_list<T>, AccumulateFunc);
}
template <typename Container, typename AccumulateFunc>
class iter::impl::Accumulator {
private:
Container container;
AccumulateFunc accumulate_func;
friend Accumulator iter::accumulate<Container, AccumulateFunc>(
Container&&, AccumulateFunc);
template <typename T, typename AF>
friend Accumulator<std::initializer_list<T>, AF> iter::accumulate(
std::initializer_list<T>, AF);
// AccumVal must be default constructible
using AccumVal =
typename std::remove_reference<typename std::result_of<AccumulateFunc(
iterator_deref<Container>, iterator_deref<Container>)>::type>::type;
Accumulator(Container&& in_container, AccumulateFunc in_accumulate_func)
: container(std::forward<Container>(in_container)),
accumulate_func(in_accumulate_func) {}
public:
Accumulator(Accumulator&&) = default;
class Iterator : public std::iterator<std::input_iterator_tag, AccumVal> {
private:
iterator_type<Container> sub_iter;
iterator_type<Container> sub_end;
AccumulateFunc* accumulate_func;
std::unique_ptr<AccumVal> acc_val;
public:
Iterator(iterator_type<Container>&& iter, iterator_type<Container>&& end,
AccumulateFunc& in_accumulate_fun)
: sub_iter{std::move(iter)},
sub_end{std::move(end)},
accumulate_func(&in_accumulate_fun),
// only get first value if not an end iterator
acc_val{!(iter != end) ? nullptr : new AccumVal(*iter)} {}
Iterator(const Iterator& other)
: sub_iter{other.sub_iter},
sub_end{other.sub_end},
accumulate_func{other.accumulate_func},
acc_val{other.acc_val ? new AccumVal(*other.acc_val) : nullptr} {}
Iterator& operator=(const Iterator& other) {
if (this == &other) { return *this; }
this->sub_iter = other.sub_iter;
this->sub_end = other.sub_end;
this->accumulate_func = other.accumulate_func;
this->acc_val.reset(
other.acc_val ? new AccumVal(*other.acc_val) : nullptr);
return *this;
}
Iterator(Iterator&&) = default;
Iterator& operator=(Iterator&&) = default;
const AccumVal& operator*() const {
return *this->acc_val;
}
const AccumVal* operator->() const {
return this->acc_val.get();
}
Iterator& operator++() {
++this->sub_iter;
if (this->sub_iter != this->sub_end) {
*this->acc_val = (*accumulate_func)(*this->acc_val, *this->sub_iter);
}
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),
this->accumulate_func};
}
Iterator end() {
return {std::end(this->container), std::end(this->container),
this->accumulate_func};
}
};
template <typename Container, typename AccumulateFunc>
iter::impl::Accumulator<Container, AccumulateFunc> iter::accumulate(
Container&& container, AccumulateFunc accumulate_func) {
return {std::forward<Container>(container), accumulate_func};
}
template <typename T, typename AccumulateFunc>
iter::impl::Accumulator<std::initializer_list<T>, AccumulateFunc>
iter::accumulate(std::initializer_list<T> il, AccumulateFunc accumulate_func) {
return {std::move(il), accumulate_func};
}
namespace iter {
template <typename Container>
auto accumulate(Container&& container) -> decltype(accumulate(
std::forward<Container>(container),
std::plus<typename std::
remove_reference<impl::iterator_deref<Container>>::type>{})) {
return accumulate(
std::forward<Container>(container),
std::plus<typename std::
remove_reference<impl::iterator_deref<Container>>::type>{});
}
template <typename T>
auto accumulate(std::initializer_list<T> il)
-> decltype(accumulate(std::move(il), std::plus<T>{})) {
return accumulate(std::move(il), std::plus<T>{});
}
}
#endif