Console Library 4.7.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
view.h
Go to the documentation of this file.
1
11
12/*
13Copyright (c) 2026 MrXie1109
14
15Permission is hereby granted, free of charge, to any person obtaining a copy
16of this software and associated documentation files (the "Software"), to deal
17in the Software without restriction, including without limitation the rights
18to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19copies of the Software, and to permit persons to whom the Software is
20furnished to do so, subject to the following conditions:
21
22The above copyright notice and this permission notice shall be included in all
23copies or substantial portions of the Software.
24
25THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31SOFTWARE.
32*/
33
34#pragma once
35#include <iostream>
36#include <cstddef>
37#include <iterator>
38#include <string>
39#include <vector>
40#include "csexc.h"
41
42namespace console
43{
52 template <class Container>
53 class View
54 {
55 using Iterator = typename Container::iterator;
56 using cIterator = typename Container::const_iterator;
57 Iterator begin_, end_;
58
59 public:
60 typedef Iterator iterator;
61 typedef cIterator const_iterator;
62
64 Iterator begin() const { return begin_; }
66 Iterator end() const { return end_; }
68 cIterator cbegin() const { return begin_; }
70 cIterator cend() const { return end_; }
71
76 View(Container &container)
77 : begin_(std::begin(container)), end_(std::end(container)) {}
78
85 View(Container &container, size_t start_pos, size_t end_pos)
86 : begin_(std::next(std::begin(container), start_pos)),
87 end_(std::next(std::begin(container), end_pos)) {}
88
94 View(Iterator begin, Iterator end) : begin_(begin), end_(end) {}
95
97 size_t size() const
98 {
99 return std::distance(begin_, end_);
100 }
101
107 auto operator[](size_t pos) const -> decltype(*begin_)
108 {
109 return *std::next(begin_, pos);
110 }
111
118 auto at(size_t pos) const -> decltype(*begin_)
119 {
120 if (pos >= size())
121 throw index_error(std::to_string(pos) +
122 " out of 0 ~ " +
123 std::to_string(size() - 1));
124 return (*this)[pos];
125 }
126
131 Container collect() const { return Container(begin_, end_); }
132 };
133
139 template <class Container>
140 class View<const Container>
141 {
142 using cIterator = typename Container::const_iterator;
143 cIterator begin_, end_;
144
145 public:
146 typedef cIterator iterator;
147 typedef cIterator const_iterator;
148
149 cIterator begin() const { return begin_; }
150 cIterator end() const { return end_; }
151 cIterator cbegin() const { return begin_; }
152 cIterator cend() const { return end_; }
153
158 View(const Container &container)
159 : begin_(std::begin(container)), end_(std::end(container)) {}
160
167 View(const Container &container,
168 size_t start_pos, size_t end_pos)
169 : begin_(std::next(std::begin(container), start_pos)),
170 end_(std::next(std::begin(container), end_pos)) {}
171
177 View(cIterator begin, cIterator end) : begin_(begin), end_(end) {}
178
179 size_t size() const
180 {
181 return std::distance(begin_, end_);
182 }
183
184 auto operator[](size_t pos) const -> decltype(*begin_)
185 {
186 return *std::next(begin_, pos);
187 }
188
189 auto at(size_t pos) const -> decltype(*begin_)
190 {
191 if (pos >= size())
192 throw index_error(std::to_string(pos) +
193 " out of 0 ~ " +
194 std::to_string(size() - 1));
195 return (*this)[pos];
196 }
197
202 Container collect() const { return Container(begin_, end_); }
203 };
204
211 std::ostream &operator<<(std::ostream &os, const View<std::string> &sv)
212 {
213 for (auto it = sv.begin(); it != sv.end(); ++it)
214 {
215 os << *it;
216 }
217 return os;
218 }
219
228 template <class T>
229 std::ostream &operator<<(std::ostream &os, const View<T> &v)
230 {
231 return os << v.collect();
232 }
233
234 // ========================== 工厂函数 ==========================
235
241
248 template <class Container>
249 View<Container> make_view(Container &cont)
250 {
251 return {cont};
252 }
253
260 template <class Container>
261 View<const Container> make_view(const Container &cont)
262 {
263 return {cont};
264 }
265
274 template <class Container>
275 View<Container> make_view(Container &cont, size_t start, size_t end)
276 {
277 return {cont, start, end};
278 }
279
288 template <class Container>
289 View<const Container> make_view(const Container &cont, size_t start, size_t end)
290 {
291 return {cont, start, end};
292 }
293
301 template <class Container>
302 View<Container> make_view(typename Container::iterator begin,
303 typename Container::iterator end)
304 {
305 return {begin, end};
306 }
307
315 template <class Container>
316 View<const Container> make_view(typename Container::const_iterator begin,
317 typename Container::const_iterator end)
318 {
319 return {begin, end};
320 }
321
329 template <class T>
331 T *end)
332 {
333 return {begin, end};
334 }
335
343 template <class T>
345 const T *end)
346 {
347 return {begin, end};
348 }
349 // end of view_factories
351}
View(const Container &container, size_t start_pos, size_t end_pos)
构造常量容器子区间的视图。
Definition view.h:167
cIterator end() const
Definition view.h:150
Container collect() const
将常量视图中的元素复制到新容器。
Definition view.h:202
auto operator[](size_t pos) const -> decltype(*begin_)
Definition view.h:184
cIterator begin() const
Definition view.h:149
View(const Container &container)
构造整个常量容器的视图。
Definition view.h:158
cIterator cend() const
Definition view.h:152
View(cIterator begin, cIterator end)
从常量迭代器对构造视图。
Definition view.h:177
cIterator iterator
Definition view.h:146
auto at(size_t pos) const -> decltype(*begin_)
Definition view.h:189
cIterator const_iterator
Definition view.h:147
size_t size() const
Definition view.h:179
cIterator cbegin() const
Definition view.h:151
容器的非拥有视图(可变版本)。
Definition view.h:54
Iterator begin() const
返回起始迭代器。
Definition view.h:64
cIterator cbegin() const
返回常量起始迭代器。
Definition view.h:68
Container collect() const
将视图中的元素复制到一个新的容器中。
Definition view.h:131
auto operator[](size_t pos) const -> decltype(*begin_)
无边界检查的下标访问。
Definition view.h:107
View(Iterator begin, Iterator end)
从迭代器对构造视图。
Definition view.h:94
auto at(size_t pos) const -> decltype(*begin_)
带边界检查的下标访问。
Definition view.h:118
cIterator const_iterator
Definition view.h:61
View(Container &container)
构造整个容器的视图。
Definition view.h:76
size_t size() const
返回视图中的元素个数。
Definition view.h:97
Iterator end() const
返回结束迭代器。
Definition view.h:66
View(Container &container, size_t start_pos, size_t end_pos)
构造容器子区间的视图。
Definition view.h:85
cIterator cend() const
返回常量结束迭代器。
Definition view.h:70
Iterator iterator
Definition view.h:60
表示索引越界错误。
Definition csexc.h:150
定义 console 库使用的自定义异常类层次结构。
MultiArray< T, Dims... > operator<<(const MultiArray< T, Dims... > &a, const MultiArray< T, Dims... > &b)
左移(数组 << 数组)。
Definition multiarray.h:1227
View< Container > make_view(Container &cont)
创建整个容器的视图(可变版本)。
Definition view.h:249
本库所有组件所在的顶层命名空间。
@ T
Definition kb.h:72