Console Library 4.7.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
compre.h
Go to the documentation of this file.
1
8
9/*
10Copyright (c) 2026 MrXie1109
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files (the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions:
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29*/
30
31#pragma once
32#include <vector>
33#include <utility>
34#include <iterator>
35
36namespace console
37{
46 template <class T>
48 {
49 protected:
50 std::vector<T> vec;
51
52 public:
55
61 template <class Cont>
62 Comprehension(const Cont &cont)
63 : vec(std::begin(cont), std::end(cont)) {}
64
70 template <class Cont>
71 Comprehension(Cont &&cont)
72 : vec(
73 std::make_move_iterator(std::begin(cont)),
74 std::make_move_iterator(std::end(cont))) {}
75
84 template <class Cont>
85 Comprehension(const Cont &cont, size_t start_pos, size_t end_pos)
86 : vec(std::next(std::begin(cont), start_pos),
87 std::next(std::begin(cont), end_pos)) {}
88
97 template <class Cont>
98 Comprehension(Cont &&cont, size_t start_pos, size_t end_pos)
99 : vec(std::make_move_iterator(
100 std::next(std::begin(cont), start_pos)),
101 std::make_move_iterator(
102 std::next(std::begin(cont), end_pos))) {}
103
110 template <class Iter>
111 Comprehension(Iter beg, Iter end) : vec(beg, end) {}
112
117 Comprehension(std::initializer_list<T> init) : vec(init) {}
118
123 std::vector<T> &get_vec()
124 {
125 return vec;
126 }
127
135 template <class F>
136 auto map(F &&f) const -> Comprehension<decltype(f(vec[0]))>
137 {
138 Comprehension<decltype(f(vec[0]))> tmp;
139 for (const T &item : vec)
140 tmp.get_vec().push_back(f(item));
141 return tmp;
142 }
143
150 template <class F>
152 {
154 for (const T &item : vec)
155 if (f(item))
156 tmp.vec.push_back(item);
157 return tmp;
158 }
159
166 template <class Cont>
167 Cont to()
168 {
169 return Cont(
170 std::make_move_iterator(vec.begin()),
171 std::make_move_iterator(vec.end()));
172 }
173
180 template <class Cont>
181 Cont make() const
182 {
183 return Cont(vec.begin(), vec.end());
184 }
185 };
186
193 template <class Cont>
194 auto compre(const Cont &cont)
196 {
198 }
199
206 template <class Cont>
207 auto compre(Cont &&cont)
209 {
210 return Comprehension<typename Cont::value_type>(std::move(cont));
211 }
212
221 template <class Cont>
222 auto compre(const Cont &cont, size_t start_pos, size_t end_pos)
224 {
226 cont, start_pos, end_pos);
227 }
228
237 template <class Cont>
238 auto compre(Cont &&cont, size_t start_pos, size_t end_pos)
240 {
242 std::move(cont), start_pos, end_pos);
243 }
244
252 template <class Iter>
253 auto compre(Iter beg, Iter end) -> Comprehension<decltype(*beg)>
254 {
255 return Comprehension<decltype(*beg)>(beg, end);
256 }
257
264 template <class T>
265 Comprehension<T> compre(std::initializer_list<T> init)
266 {
267 return Comprehension<T>(init);
268 }
269}
函数式风格的数据流处理容器。
Definition compre.h:48
Comprehension(Cont &&cont)
从容器(右值)构造,移动所有元素。
Definition compre.h:71
Comprehension(const Cont &cont, size_t start_pos, size_t end_pos)
从容器的子区间(左值)构造,拷贝指定范围的元素。
Definition compre.h:85
Comprehension< T > filter(F &&f) const
筛选满足谓词 f 的元素,返回新 Comprehension。
Definition compre.h:151
Comprehension()
默认构造函数,创建一个空 Comprehension。
Definition compre.h:54
Comprehension(Cont &&cont, size_t start_pos, size_t end_pos)
从容器的子区间(右值)构造,移动指定范围的元素。
Definition compre.h:98
Cont make() const
将内部元素拷贝构造到目标容器 Cont 中。
Definition compre.h:181
Comprehension(std::initializer_list< T > init)
从 std::initializer_list 构造。
Definition compre.h:117
auto map(F &&f) const -> Comprehension< decltype(f(vec[0]))>
对每个元素应用函数 f,返回新 Comprehension。
Definition compre.h:136
std::vector< T > & get_vec()
获取内部 vector 的引用。
Definition compre.h:123
Cont to()
将内部元素移动构造到目标容器 Cont 中。
Definition compre.h:167
Comprehension(Iter beg, Iter end)
从迭代器对构造,拷贝 [beg, end) 范围内的元素。
Definition compre.h:111
std::vector< T > vec
内部存储的 std::vector
Definition compre.h:50
Comprehension(const Cont &cont)
从容器(左值)构造,拷贝所有元素。
Definition compre.h:62
本库所有组件所在的顶层命名空间。
@ F
Definition kb.h:58
@ T
Definition kb.h:72
auto compre(const Cont &cont) -> Comprehension< typename Cont::value_type >
从容器(左值)创建 Comprehension。
Definition compre.h:194