-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStream.cpp
More file actions
176 lines (151 loc) · 4.04 KB
/
Stream.cpp
File metadata and controls
176 lines (151 loc) · 4.04 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
/*
Author: Greg Blodgett
The template extention of the BaseStream class.
Reference: https://docs.oracle.com/javase/8/docs/api/?java/util/stream/Stream.html
*/
#include<iostream>
#include<functional>
#include<vector>
#include "BaseStream.h"
#include "IntStream.h"
template<typename T>
class Stream : public BaseStream<T> {
public:
Stream(std::vector<T> const&);
Stream(const Stream<T>& );
~Stream();
template<typename Function> auto map(Function) const;
IntStream mapToInt(std::function<int(const T&)>) const;
auto forEach(std::function<void(const T&)>) const;
auto forEach(std::ostream&) const;
auto filter(std::function<bool(const T&)>) const;
bool anyMatch(std::function<bool(const T&)>) const;
bool allMatch(std::function<bool(const T&)>) const;
bool noneMatch(std::function<bool(const T&)>) const;
int count() const;
std::optional<T> findFirst(std::function<bool(const T&)>) const;
const T* toArray() const;
Stream<T> operator = (Stream<T> const&);
static Stream<T> of(T args...);
private:
const std::vector<T> elements;
};
template<typename T>
Stream<T>::~Stream() {}
template<typename T>
Stream<T>::Stream(const Stream<T> &other ) {
this->elements = other.elements;
}
template<typename T>
Stream<T> Stream<T>::operator=(Stream<T> const & obj)
{
return Stream<T>{obj};
}
template<typename T>
Stream<T>::Stream(std::vector<T> const& e) : elements(e){}
template<typename T>
Stream<T> Stream<T>::of(T args...)
{
return Stream{ std::vector<T>{args} };
}
// ---------------------------------------------------------------------------
template<typename T>
static Stream<T> of(T args...) {
return Stream{ std::vector<T>{args} };
}
template<typename T>
template<typename Function>
auto Stream<T>::map(Function func) const
{
T throwAway = elements.at(0);
using R = decltype(func(throwAway));
std::vector<R> newStream;
for (auto i = elements.begin(); i != elements.end(); ++i) {
T copy = *i;
newStream.push_back(func(copy));
}
return Stream<R>{newStream};
}
template<typename T>
auto Stream<T>::filter(std::function<bool(const T&)> predicate) const
{
std::vector<T> newStream;
for (auto i = elements.begin(); i != elements.end(); ++i) {
if (predicate(*i)) {
newStream.push_back(*i);
}
}
return Stream<T>{newStream};
}
template<typename T>
auto Stream<T>::forEach(std::function<void(const T&)> consumer) const
{
for (auto i = elements.begin(); i != elements.end(); ++i) {
consumer(*i);
}
}
template<typename T>
auto Stream<T>::forEach(std::ostream &stream) const
{
for (auto i = elements.begin(); i != elements.end(); ++i) {
stream << *i << " ";
}
}
template<typename T>
bool Stream<T>::anyMatch(std::function<bool(const T&)> predicate) const
{
for (auto i = elements.begin(); i != elements.end(); ++i) {
if (predicate(*i)) {
return true;
}
}
return false;
}
template<typename T>
bool Stream<T>::allMatch(std::function<bool(const T&)> predicate) const
{
for (auto i = elements.begin(); i != elements.end(); ++i) {
if (!predicate(*i)) {
return false;
}
}
return true;
}
template<typename T>
bool Stream<T>::noneMatch(std::function<bool(const T&)> predicate) const
{
for (auto i = elements.begin(); i != elements.end(); ++i) {
if (predicate(*i)) {
return false;
}
}
return true;
}
template<typename T>
int Stream<T>::count() const {
return elements.size();
}
template<typename T>
IntStream Stream<T>::mapToInt(std::function<int(const T&)> mapper) const
{
std::vector<int> intStream;
for (auto i = elements.begin(); i != elements.end(); ++i) {
intStream.push_back(mapper(*i));
}
return IntStream{intStream};
}
template<typename T>
const T * Stream<T>::toArray() const
{
return &elements[0];
}
template<typename T>
std::optional<T> Stream<T>::findFirst(std::function<bool(const T&)> target) const
{
for (auto i = elements.begin(); i != elements.end(); ++i) {
if (target(*i)) {
return std::optional<T>{*i};
}
}
return std::nullopt;
}