forked from jscheiny/Streams
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStream.h
More file actions
278 lines (205 loc) · 8.56 KB
/
Copy pathStream.h
File metadata and controls
278 lines (205 loc) · 8.56 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#ifndef SCHEINERMAN_STREAM_STREAM_H
#define SCHEINERMAN_STREAM_STREAM_H
#include "StreamForward.h"
#include "StreamError.h"
#include "providers/Providers.h"
#include "Utility.h"
#include <unordered_set>
#include <functional>
#include <type_traits>
#include <iostream>
#include <iterator>
#include <vector>
#include <random>
#include <chrono>
#include <deque>
#include <list>
#include <set>
namespace stream {
struct MakeStream {
template<typename T>
static Stream<RemoveRef<T>> empty();
template<typename T>
static Stream<RemoveRef<T>> repeat(T&& value);
template<typename T>
static Stream<RemoveRef<T>> repeat(T&& value, size_t times);
template<typename Iterator>
static Stream<IteratorType<Iterator>> cycle(Iterator begin, Iterator end);
template<typename Iterator>
static Stream<IteratorType<Iterator>> cycle(Iterator begin, Iterator end,
size_t times);
template<typename Container>
static Stream<ContainerType<Container>> cycle(const Container& cont);
template<typename Container>
static Stream<ContainerType<Container>> cycle(const Container& cont,
size_t times);
template<typename T>
static Stream<T> cycle(std::initializer_list<T> init);
template<typename T>
static Stream<T> cycle(std::initializer_list<T> init, size_t times);
template<typename Container>
static Stream<ContainerType<Container>> cycle_move(Container&& cont);
template<typename Container>
static Stream<ContainerType<Container>> cycle_move(Container&& cont,
size_t times);
template<typename Generator>
static Stream<std::result_of_t<Generator()>> generate(Generator&& generator);
template<typename T, typename Function>
static Stream<RemoveRef<T>> iterate(T&& initial, Function&& function);
template<typename... Args, typename Function>
static Stream<RemoveRef<Head<Args...>>> recurrence(Function&& function, Args&&... initial);
template<typename T>
static Stream<RemoveRef<T>> counter(T&& start);
template<typename T, typename U>
static Stream<RemoveRef<T>> counter(T&& start, U&& increment);
template<typename T, typename U>
static Stream<RemoveRef<T>> counter(T&& start, const U& increment);
template<typename T>
static Stream<RemoveRef<T>> range(T&& lower, T&& upper);
template<typename T, typename U>
static Stream<RemoveRef<T>> range(T&& lower, T&& upper, U&& increment);
template<typename T, typename U>
static Stream<RemoveRef<T>> range(T&& lower, T&& upper, const U& increment);
template<typename T>
static Stream<RemoveRef<T>> closed_range(T&& lower, T&& upper);
template<typename T, typename U>
static Stream<RemoveRef<T>> closed_range(T&& lower, T&& upper, U&& increment);
template<typename T, typename U>
static Stream<RemoveRef<T>> closed_range(T&& lower, T&& upper, const U& increment);
template<typename T, template<typename> class Distribution,
typename Engine=std::default_random_engine,
typename Seed,
typename ... GenArgs>
static Stream<T> randoms_seeded(Seed&& seed, GenArgs&&... args);
template<typename T, template<typename> class Distribution,
typename Engine=std::default_random_engine,
typename ... GenArgs>
static Stream<T> randoms(GenArgs&&... args);
template<typename Engine=std::default_random_engine, typename T>
static Stream<T> uniform_random_ints(T lower, T upper);
template<typename Engine=std::default_random_engine, typename T, typename Seed>
static Stream<T> uniform_random_ints(T lower, T upper, Seed&& seed);
template<typename Engine=std::default_random_engine, typename T=double>
static Stream<T> uniform_random_reals(T lower=0.0, T upper=1.0);
template<typename Engine=std::default_random_engine, typename T, typename Seed>
static Stream<T> uniform_random_reals(T lower, T upper, Seed&& seed);
template<typename Engine=std::default_random_engine, typename T=double>
static Stream<T> normal_randoms(T mean=0.0, T stddev=1.0);
template<typename Engine=std::default_random_engine, typename T, typename Seed>
static Stream<T> normal_randoms(T mean, T stddev, Seed&& seed);
template<typename Engine=std::default_random_engine, typename T=bool>
static Stream<T> coin_flips();
template<typename Engine=std::default_random_engine, typename T=bool, typename Seed>
static Stream<T> coin_flips(Seed&& seed);
template<typename T>
static Stream<RemoveRef<T>> singleton(T&& value);
template<typename Iterator>
static Stream<IteratorType<Iterator>> from(Iterator begin, Iterator end);
template<typename Container>
static Stream<ContainerType<Container>> from(const Container& cont);
template<typename T>
static Stream<T> from(T* array, std::size_t length);
template<typename T>
static Stream<T> from(std::initializer_list<T> init);
template<typename Container>
static Stream<ContainerType<Container>> from_move(Container&& cont);
private:
static auto default_seed() {
return std::chrono::high_resolution_clock::now().time_since_epoch().count();
}
};
template<typename T> class Operator;
template<typename T> class Terminator;
template<typename T>
class Stream {
public:
using element_type = T;
using iterator = typename provider::StreamProvider<T>::Iterator;
iterator begin() {
return source_->begin();
}
iterator end() {
return source_->end();
}
Stream()
: source_(make_stream_provider<provider::Empty, T>()) {}
Stream(Stream<T>&& other) = default;
Stream<T>& operator= (Stream<T>&& other) = default;
Stream(const Stream<T>& other) = delete;
Stream<T>& operator= (const Stream<T>& other) = default;
Stream(StreamProviderPtr<T> source)
: source_(std::move(source)) {}
template<typename Iterator>
Stream(Iterator begin, Iterator end)
: source_(make_stream_provider<provider::Iterator, T, Iterator>(
begin, end)) {}
template<typename Container>
Stream(const Container& container)
: Stream(container.begin(), container.end()) {}
Stream(std::initializer_list<T> init)
: Stream(std::deque<T>(init.begin(), init.end())) {}
template<typename F>
auto operator| (Operator<F>&& op) ->
decltype(op.apply_to(std::move(*this))) {
return op.apply_to(std::move(*this));
}
template<typename F>
auto operator| (Operator<F>& op) ->
decltype(op.apply_to(std::move(*this))) {
return op.apply_to(std::move(*this));
}
template<typename F>
auto operator| (Terminator<F>&& term) ->
decltype(term.apply_to(std::move(*this))) {
return term.apply_to(std::move(*this));
}
template<typename F>
auto operator| (Terminator<F>& term) ->
decltype(term.apply_to(std::move(*this))) {
return term.apply_to(std::move(*this));
}
template<typename A> operator std::vector<T, A>();
template<typename A> operator std::list<T, A>();
template<typename A> operator std::deque<T, A>();
template<typename C, typename A> operator std::set<T, C, A>();
template<typename C, typename A> operator std::multiset<T, C, A>();
template<typename H, typename P, typename A> operator std::unordered_set<T, H, P, A>();
template<typename H, typename P, typename A> operator std::unordered_multiset<T, H, P, A>();
StreamProviderPtr<T>& getSource() {
return source_;
}
void close() {
source_.reset();
}
bool occupied() const {
return bool(source_);
}
void swap(Stream<T>& other) {
source_.swap(other.sorce_);
}
std::string pipeline() {
std::stringstream ss;
provider::PrintInfo info = source_->print(ss, 1);
ss << "Stream pipeline with "
<< info.stages << " stage" << (info.stages == 1 ? "" : "s") << " and "
<< info.sources << " source" << (info.sources == 1 ? "" : "s") << ".";
return ss.str();
}
template<typename> friend class Operator;
template<typename> friend class Terminator;
private:
StreamProviderPtr<T> source_;
void check_vacant(const std::string& method) {
if(!occupied()) {
throw VacantStreamException(method);
}
}
};
} /* namespace stream */
#include "StreamOperations.h"
#include "StreamOperators.h"
#include "StreamTerminators.h"
#include "StreamGenerators.h"
#include "StreamAlgebra.h"
#include "StreamConversions.h"
#endif