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
119 lines (96 loc) · 3.14 KB
/
accumulate.hpp
File metadata and controls
119 lines (96 loc) · 3.14 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
#ifndef ITER_ACCUMULATE_H_
#define ITER_ACCUMULATE_H_
#include "internal/iterbase.hpp"
#include <utility>
#include <iterator>
#include <functional>
#include <type_traits>
#include <memory>
namespace iter {
namespace impl {
template <typename Container, typename AccumulateFunc>
class Accumulator;
using AccumulateFn = IterToolFnOptionalBindSecond<Accumulator, std::plus<>>;
}
constexpr impl::AccumulateFn accumulate{};
}
template <typename Container, typename AccumulateFunc>
class iter::impl::Accumulator {
private:
Container container;
AccumulateFunc accumulate_func;
friend AccumulateFn;
using AccumVal = std::remove_reference_t<std::result_of_t<AccumulateFunc(
iterator_deref<Container>, iterator_deref<Container>)>>;
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};
}
};
#endif