This repository was archived by the owner on Nov 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream.hpp
More file actions
130 lines (106 loc) · 3.47 KB
/
stream.hpp
File metadata and controls
130 lines (106 loc) · 3.47 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
#pragma once
#include "types.hpp"
#include "algorithms.hpp"
struct force {
template<is::delayed d>
struct avoid;
FUNC_HEAD_ID;
template<is::delayed d>
struct apply_on<d> {
using ret = Ret(Ret(force, get::d_func<d>), Ret(map, force, get::d_args<d>));
};
};
template<typename f, typename... args>
struct delayed {
using __func = f;
using __args = Id(args...);
template<typename T>
struct apply_on {
using ret = D(D(f, args...), Id(T));
};
};
namespace is {
template<typename T>
concept stream =
list<T> &&
delayed<Ret(peek_back, T)>;
}
struct make::stream {
FUNC_HEAD_THROW(stream);
template<is::function next, typename... Ts>
struct apply_on<L(next, Ts...) > {
struct find_next {
template<typename l>
struct apply_on {
using next_item = Ret(next, l);
using next_args = Ret(pop_front, Ret(push_back, next_item, l));
using ret = L(next_item, D(find_next, next_args));
};
};
using ret = L(Ts..., D(find_next, Ts...));
};
template<is::function next, typename T>
struct apply_on<L(next, T) > {
struct find_next {
template<typename l>
struct apply_on {
using next_item = Ret(next, l);
using ret = L(next_item, D(find_next, next_item));
};
};
using ret = L(T, D(find_next, T));
};
};
struct calc {
FUNC_HEAD_THROW(calc);
WAIT_FOR_n_DELAYED_ARGS(calc, 2);
template<is::not_delayed s, is::not_delayed times> requires (is::stream<s> && is::arg<times>)
struct apply_on<L(s, times) > {
using ret = Ret(
calc,
Ret(
Select(Ret(is_greater, Len(s), A(2)), join, push_front),
L(Ret(pop_back, s), Id(F(Ret(peek_back, s))))),
DEC(times));
};
template<is::stream s>
struct apply_on<L(s, A(0))> {
using ret = s;
};
};
struct map_s {
FUNC_HEAD_THROW(map_s);
WAIT_FOR_n_DELAYED_ARGS(map_s, 2);
template<is::function f, is::stream s>
struct apply_on<L(f, s) > {
using ret = Ret(
Select(Ret(is_greater, Len(s), A(2)), push_back, swap),
Id(Ret(map_s, f, Ret(peek_back, s))), Ret(map, f, Ret(pop_back, s)));
};
};
struct filter_s {
private:
struct find_filtered_next {
FUNC_HEAD_THROW_NO_WAIT;
template<is::function f, typename find_next>
struct apply_on<L(f, force::avoid<find_next>)> {
using next_list = F(find_next);
using next_item = Ret(peek_front, next_list);
using next_finder = force::avoid<Ret(peek_back, next_list)>;
using allowed = Ret(cast, Ret(f, next_item), bool);
using ret = Ret(do_if, allowed,
id, L(next_item, D(find_filtered_next, f, next_finder)),
find_filtered_next, L(f, next_finder));
};
};
public:
FUNC_HEAD_THROW(filter_s);
template<is::function f, is::stream s>
struct apply_on<L(f, s) > {
using filtered = Ret(filter, f, Ret(pop_back, s));
using finder = Ret(peek_back, s);
using ret = Ret(do_if, Ret(is_same, filtered, filter::Null),
force, Ret(find_filtered_next, f, force::avoid<finder>),
replace_l, L(D(find_filtered_next, f, force::avoid<finder>), s, DEC(Len(s))));
};
};