This repository was archived by the owner on Nov 15, 2022. It is now read-only.
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
148 lines (122 loc) · 5.03 KB
/
Copy pathaccumulate.hpp
File metadata and controls
148 lines (122 loc) · 5.03 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
#ifndef ITER_ACCUMULATE_H_
#define ITER_ACCUMULATE_H_
#include "iterbase.hpp"
#include <utility>
#include <iterator>
#include <initializer_list>
#include <functional>
#include <type_traits>
namespace iter {
//Forward declarations of Accumulator and accumulate
template <typename Container, typename AccumulateFunc>
class Accumulator;
template <typename Container, typename AccumulateFunc>
Accumulator<Container, AccumulateFunc> accumulate(
Container&&, AccumulateFunc);
template <typename T, typename AccumulateFunc>
Accumulator<std::initializer_list<T>, AccumulateFunc> accumulate(
std::initializer_list<T>, AccumulateFunc);
template <typename Container, typename AccumulateFunc>
class Accumulator {
private:
Container container;
AccumulateFunc accumulate_func;
friend Accumulator accumulate<Container, AccumulateFunc>(
Container&&, AccumulateFunc);
template <typename T, typename AF>
friend Accumulator<std::initializer_list<T>, AF> accumulate(
std::initializer_list<T>, AF);
// AccumVal must be default constructible
using AccumVal =
std::remove_reference_t<
std::result_of_t<AccumulateFunc(
iterator_deref<Container>,
iterator_deref<Container>)>>;
static_assert(
std::is_default_constructible<AccumVal>::value,
"Cannot accumulate a non-default constructible type");
Accumulator(Container&& in_container,
AccumulateFunc in_accumulate_func)
: container(std::forward<Container>(in_container)),
accumulate_func(in_accumulate_func)
{ }
public:
class Iterator
: public std::iterator<std::input_iterator_tag, AccumVal>
{
private:
iterator_type<Container> sub_iter;
iterator_type<Container> sub_end;
AccumulateFunc accumulate_func;
AccumVal acc_val;
public:
Iterator (iterator_type<Container>&& iter,
iterator_type<Container>&& end,
AccumulateFunc in_accumulate_func)
: sub_iter{std::move(iter)},
sub_end{std::move(end)},
accumulate_func(in_accumulate_func),
// only get first value if not an end iterator
acc_val(!(iter != end) ? AccumVal{} : *iter)
{ }
const AccumVal& operator*() const {
return this->acc_val;
}
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};
}
};
// Helper function to instantiate an Accumulator
template <typename Container, typename AccumulateFunc>
Accumulator<Container, AccumulateFunc> accumulate(
Container&& container,
AccumulateFunc accumulate_func)
{
return {std::forward<Container>(container), accumulate_func};
}
template <typename Container>
auto accumulate(Container&& container) {
return accumulate(std::forward<Container>(container),
std::plus<std::remove_reference_t<
iterator_deref<Container>>>{});
}
template <typename T, typename AccumulateFunc>
Accumulator<std::initializer_list<T>, AccumulateFunc> accumulate(
std::initializer_list<T> il,
AccumulateFunc accumulate_func)
{
return {std::move(il), accumulate_func};
}
template <typename T>
auto accumulate(std::initializer_list<T> il) {
return accumulate(std::move(il), std::plus<T>{});
}
}
#endif