YSTest: Container.h 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

Container.h
浏览该文件的文档.
1 /*
2 © 2010-2016, 2018-2020 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #ifndef YCL_INC_Container_h_
29 #define YCL_INC_Container_h_ 1
30
31 #include "YModules.h"
32 #include YFM_YCLib_YCommon // for YAssertNonnull;
33 #include <ystdex/memory_resource.h> // for ystdex::pmr;
34 #include <ystdex/functor.hpp> // for ystdex::less, ystdex::equal_to;
35 //#include <ext/vstring.h>
36 #include <ystdex/tstring_view.hpp>
37 #include <ystdex/string.hpp> // for ystdex::basic_string,
38 // ystdex::basic_string_view, ystdex::make_string_view, std::to_string,
39 // std::basic_string;
40 #include <ystdex/array.hpp>
41 #include <deque>
42 #include <forward_list>
43 #include <ystdex/list.hpp> // for ystdex::list;
44 #include <vector>
45 #include <ystdex/map.hpp> // for ystdex::map;
46 #include <set>
47 #include <unordered_set>
48 #include <unordered_map>
49 #include <queue>
50 #include <stack>
51 #include <iosfwd> // for std::basic_istringstream, std::basic_ostringstream,
52 // std::basic_stringstream;
53 #include <ystdex/hash.hpp> // for std::hash, ystdex::hash_range;
54
55 namespace platform
56 {
57
59 namespace pmr = ystdex::pmr;
60
62 inline namespace basic_utilities
63 {
64
65 using std::forward_as_tuple;
66 using std::get;
67 using std::ignore;
68 using std::make_pair;
69 using std::make_tuple;
70 using std::pair;
71 using std::tie;
72 using std::tuple;
73 using std::tuple_cat;
74
76 using std::begin;
78 using std::end;
79
81 using ystdex::size;
82
83 } // inline namespace utilities;
84
86 inline namespace containers
87 {
88
89 using std::array;
91
92 template<typename _type, class _tAlloc = pmr::polymorphic_allocator<_type>>
93 using deque = std::deque<_type, _tAlloc>;
94
95 template<typename _type, class _tAlloc = pmr::polymorphic_allocator<_type>>
96 using forward_list = std::forward_list<_type, _tAlloc>;
97
98 template<typename _type, class _tAlloc = pmr::polymorphic_allocator<_type>>
99 using list = ystdex::list<_type, _tAlloc>;
100
101 template<typename _type, class _tAlloc = pmr::polymorphic_allocator<_type>>
102 using vector = std::vector<_type, _tAlloc>;
103
104
105 // NOTE: Since ISO C++17 node extraction of standard containers is not otherwise
106 // supported, solely using of %ystdex::map does not lead to troubles about
107 // extracted node handle incompatibilities. This also makes less templates need
108 // to be instantiated, in the expense of no debug support in libstdc++, which
109 // is an implementation detail.
110 template<typename _tKey, typename _tMapped, typename _fComp
111 = ystdex::less<_tKey>, class _tAlloc
112 = pmr::polymorphic_allocator<std::pair<const _tKey, _tMapped>>>
113 using map = ystdex::map<_tKey, _tMapped, _fComp, _tAlloc>;
114
115 template<typename _tKey, typename _tMapped, typename _fComp
116 = ystdex::less<_tKey>, class _tAlloc
117 = pmr::polymorphic_allocator<std::pair<const _tKey, _tMapped>>>
118 using multimap = std::multimap<_tKey, _tMapped, _fComp, _tAlloc>;
119
120 template<typename _tKey, typename _fComp = ystdex::less<_tKey>,
121 class _tAlloc = pmr::polymorphic_allocator<_tKey>>
122 using multiset = std::multiset<_tKey, _fComp, _tAlloc>;
123
124 template<typename _tKey, typename _fComp = ystdex::less<_tKey>,
125 class _tAlloc = pmr::polymorphic_allocator<_tKey>>
126 using set = std::set<_tKey, _fComp, _tAlloc>;
127
128
129 template<typename _tKey, typename _tMapped, typename _fHash = std::hash<_tKey>,
130 typename _fEqual = ystdex::equal_to<_tKey>, class _tAlloc
131 = pmr::polymorphic_allocator<std::pair<const _tKey, _tMapped>>>
132 using unordered_map
133 = std::unordered_map<_tKey, _tMapped, _fHash, _fEqual, _tAlloc>;
134
135 template<typename _tKey, typename _tMapped, typename _fHash = std::hash<_tKey>,
136 typename _fEqual = ystdex::equal_to<_tKey>, class _tAlloc
137 = pmr::polymorphic_allocator<std::pair<const _tKey, _tMapped>>>
138 using unordered_multimap
139 = std::unordered_multimap<_tKey, _tMapped, _fHash, _fEqual, _tAlloc>;
140
141 template<typename _tKey, typename _fHash = std::hash<_tKey>,
142 typename _fEqual = ystdex::equal_to<_tKey>,
143 class _tAlloc = pmr::polymorphic_allocator<_tKey>>
144 using unordered_multiset
145 = std::unordered_multiset<_tKey, _fHash, _fEqual, _tAlloc>;
146
147 template<typename _tKey, typename _fHash = std::hash<_tKey>,
148 typename _fEqual = ystdex::equal_to<_tKey>,
149 class _tAlloc = pmr::polymorphic_allocator<_tKey>>
150 using unordered_set = std::unordered_set<_tKey, _fHash, _fEqual, _tAlloc>;
151
152 template<typename _type, class _tSeqCon = deque<_type>>
153 using stack = std::stack<_type, _tSeqCon>;
154
155 template<typename _tKey, class _tSeqCon = vector<_tKey>,
156 class _fComp = ystdex::less<_tKey>>
157 using priority_queue = std::priority_queue<_tKey, _tSeqCon, _fComp>;
158
159 template<typename _type, class _tSeqCon = deque<_type>>
160 using queue = std::queue<_type, _tSeqCon>;
162
163 } // inline namespace containers;
164
165 inline namespace strings
166 {
167
173 template<typename _tChar, typename _tTraits = std::char_traits<_tChar>,
174 class _tAlloc = pmr::polymorphic_allocator<_tChar>>
175 using basic_string = ystdex::basic_string<_tChar, _tTraits, _tAlloc>;
176 // using versa_string = __gnu_cxx::__versa_string<_tChar>;
177
178 using string = basic_string<char>;
180 using u16string = basic_string<char16_t>;
182 using u32string = basic_string<char32_t>;
184 using wstring = basic_string<wchar_t>;
185
187
188 using ystdex::basic_string_view;
189 using ystdex::string_view;
190 using ystdex::u16string_view;
191 using ystdex::wstring_view;
193 using ystdex::string_view_t;
196
197 using ystdex::basic_tstring_view;
198 using ystdex::tstring_view;
199 using ystdex::u16tstring_view;
200 using ystdex::wtstring_view;
202
204 using ystdex::make_string_view;
205
207 static_assert(ystdex::is_implicitly_nothrow_constructible<const std::string&,
208 const basic_string<char, std::char_traits<char>, std::allocator<char>>&>(),
209 "Invalid basic string type (which is incompatible to exception"
210 " the standard string type in exception types) found.");
212 static_assert(ystdex::is_implicitly_nothrow_constructible<string_view,
213 string>(), "Invalid string view type found.");
214
216 using ystdex::sfmt;
218 using ystdex::vsfmt;
219
220 // XXX: This set of overloads is sometimes useful in conversions between values
221 // of %basic_string and %std::basic_string instances.
223
224 template<typename _tChar, class _tString = basic_string<_tChar>>
225 inline _tString
226 to_pmr_string(basic_string_view<_tChar> sv)
227 {
228 YAssertNonnull(sv.data());
229 return _tString(sv.data(), sv.size());
230 }
231 template<typename _tChar, class _tString = basic_string<_tChar>,
232 class _tTraits, class _tAlloc,
233 typename = yimpl(ystdex::enable_if_inconvertible_t)<
234 const std::basic_string<_tChar, _tTraits, _tAlloc>&, _tString>>
235 inline _tString
236 to_pmr_string(const std::basic_string<_tChar, _tTraits, _tAlloc>& str)
237 {
238 return _tString(str.data(), str.size());
239 }
240 template<class _tString = string, class _tParam = _tString,
241 yimpl(typename = ystdex::enable_if_convertible_t<_tParam, _tString>)>
242 inline _tString
243 to_pmr_string(_tParam&& str)
244 {
245 return yforward(str);
246 }
248
249 // XXX: The overloads %to_std_string is provided to get %std::string values, in
250 // particular for arguments to %std::exception. No %std::wstring and other
251 // types are required like this.
253
254 template<typename _tChar, class _tString = std::basic_string<_tChar>>
255 inline _tString
256 to_std_string(basic_string_view<_tChar> sv)
257 {
258 YAssertNonnull(sv.data());
259 return _tString(sv.data(), sv.size());
260 }
262 template<typename _tChar, class _tString = std::basic_string<_tChar>,
263 class _tTraits, class _tAlloc,
264 typename = yimpl(ystdex::enable_if_inconvertible_t)<
265 const basic_string<_tChar, _tTraits, _tAlloc>&, _tString>>
266 inline _tString
267 to_std_string(const basic_string<_tChar, _tTraits, _tAlloc>& str)
268 {
269 return _tString(str.data(), str.size());
270 }
272 template<class _tString = std::string, class _tParam = _tString,
273 yimpl(typename = ystdex::enable_if_convertible_t<_tParam, _tString>)>
274 inline _tString
275 to_std_string(_tParam&& str)
276 {
277 return yforward(str);
278 }
280
282 using ystdex::to_string;
284 using ystdex::to_wstring;
285
286 } // inline namespace strings;
287
289
290 inline namespace ios
291 {
292
293 template<typename _tChar,
294 class _tTraits = typename basic_string<_tChar>::traits_type,
295 class _tAlloc = typename basic_string<_tChar>::allocator_type>
296 using basic_stringbuf = std::basic_stringbuf<_tChar, _tTraits, _tAlloc>;
297 template<class _tString>
298 using stringbuf_for = basic_stringbuf<typename _tString::value_type,
299 typename _tString::traits_type, typename _tString::allocator_type>;
300 using stringbuf = stringbuf_for<string>;
301 using wstringbuf = stringbuf_for<wstring>;
302
303 template<typename _tChar,
304 class _tTraits = typename basic_string<_tChar>::traits_type,
305 class _tAlloc = typename basic_string<_tChar>::allocator_type>
306 using basic_istringstream = std::basic_istringstream<_tChar, _tTraits, _tAlloc>;
307 template<class _tString>
308 using istringstream_for = basic_istringstream<typename _tString::value_type,
309 typename _tString::traits_type, typename _tString::allocator_type>;
310 using istringstream = istringstream_for<string>;
311 using wistringstream = istringstream_for<wstring>;
312
313 template<typename _tChar,
314 class _tTraits = typename basic_string<_tChar>::traits_type,
315 class _tAlloc = typename basic_string<_tChar>::allocator_type>
316 using basic_ostringstream = std::basic_ostringstream<_tChar, _tTraits, _tAlloc>;
317 template<class _tString>
318 using ostringstream_for = basic_ostringstream<typename _tString::value_type,
319 typename _tString::traits_type, typename _tString::allocator_type>;
320 using ostringstream = ostringstream_for<string>;
321 using wostringstream = ostringstream_for<wstring>;
322
323 template<typename _tChar,
324 class _tTraits = typename basic_string<_tChar>::traits_type,
325 class _tAlloc = typename basic_string<_tChar>::allocator_type>
326 using basic_stringstream = std::basic_stringstream<_tChar, _tTraits, _tAlloc>;
327 template<class _tString>
328 using stringstream_for = basic_stringstream<typename _tString::value_type,
329 typename _tString::traits_type, typename _tString::allocator_type>;
330 using stringstream = stringstream_for<string>;
331 using wstringstream = stringstream_for<wstring>;
332
333 } // inline namespace ios;
335
336 } // namespace platform;
337
338 namespace platform_ex
339 {
340
342 using namespace platform::basic_utilities;
344 using namespace platform::containers;
346 using namespace platform::strings;
347
348 } // namespace platform_ex;
349
350 // XXX: Currently %ystdex::polymorphic_allocator is always separatedly defined.
351 #if !YB_Impl_String_has_P0254R2
352 namespace std
354 {
355
362 template<typename _tChar, class _tTraits>
363 struct hash<ystdex::basic_string<_tChar, _tTraits,
364 ystdex::pmr::polymorphic_allocator<_tChar>>>
365 {
366 size_t
367 operator()(const ystdex::basic_string<_tChar, _tTraits,
368 ystdex::pmr::polymorphic_allocator<_tChar>>& k) const ynothrow
369 {
370 // NOTE: This is similar to %ystdex::basic_string_view.
371 return ystdex::hash_range(k.data(), k.data() + k.size());
372 }
373 };
374
375 } // namespace std;
376 #endif
377
378 #endif
379
tstring_view.hpp
指定结束字符的只读字符串视图。
string.hpp
ISO C++ 标准字符串扩展。
list.hpp
列表容器。
ystdex::cpp2017::basic_string
Definition: string.hpp:77
platform::basic_utilities
Definition: Container.h:63
platform_ex
平台扩展命名空间。
Definition: Android.h:46
platform::ios::istringstream_for
basic_istringstream< typename _tString::value_type, typename _tString::traits_type, typename _tString::allocator_type > istringstream_for
Definition: Container.h:309
ystdex::cpp2017::basic_string_view
Definition: string_view.hpp:95
ystdex::cpp2017::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:437
YSLib::IO::_tTraits
const string &path const String &path class class _tTraits
Definition: YSLib/Service/FileSystem.h:582
ystdex::cpp2017::wstring_view
basic_string_view< wchar_t > wstring_view
Definition: string_view.hpp:440
platform::containers
Definition: Container.h:87
ystdex::string_view_t
basic_string_view< typename _tString::value_type > string_view_t
取字符串类类型的值类型对应的字符串视图类型。
Definition: string_view.hpp:450
std::hash< ystdex::basic_string< _tChar, _tTraits, ystdex::pmr::polymorphic_allocator< _tChar > > >::operator()
size_t operator()(const ystdex::basic_string< _tChar, _tTraits, ystdex::pmr::polymorphic_allocator< _tChar >> &k) const ynothrow
Definition: Container.h:367
map.hpp
映射容器。
ystdex::cpp2014::less
小于关系仿函数。
Definition: functor.hpp:398
hash.hpp
散列接口。
platform::ios::wstringstream
stringstream_for< wstring > wstringstream
Definition: Container.h:331
ystdex::is_implicitly_nothrow_constructible
判断指定类型是否可被指定参数类型无异常抛出构造且可隐式构造。
Definition: meta.hpp:746
ystdex::pmr
Definition: memory_resource.h:158
platform::containers::unordered_multimap
std::unordered_multimap< _tKey, _tMapped, _fHash, _fEqual, _tAlloc > unordered_multimap
Definition: Container.h:139
ystdex::hash_range
size_t hash_range(size_t seed, _tIn first, _tIn last)
重复对范围计算散列。
Definition: hash.hpp:90
platform::containers::queue
std::queue< _type, _tSeqCon > queue
Definition: Container.h:160
ystdex::u16tstring_view
basic_tstring_view< char16_t > u16tstring_view
Definition: tstring_view.hpp:293
platform::strings::to_std_string
_tString to_std_string(basic_string_view< _tChar > sv)
Definition: Container.h:256
platform::ios::ostringstream_for
basic_ostringstream< typename _tString::value_type, typename _tString::traits_type, typename _tString::allocator_type > ostringstream_for
Definition: Container.h:319
ystdex::map
映射容器。
Definition: map.hpp:79
ystdex::vsfmt
_tString vsfmt(const typename string_traits< _tString >::value_type *fmt, std::va_list args)
以 C 标准输出格式的输出字符串类型的对象。
Definition: string.hpp:1998
ystdex::cpp2017::basic_string_view::data
const_pointer data() const
Definition: string_view.hpp:234
YSLib::IO::_tString
const string &path const String &path class class class _tString
Definition: YSLib/Service/FileSystem.h:612
platform::containers::stack
std::stack< _type, _tSeqCon > stack
Definition: Container.h:153
platform::containers::unordered_set
std::unordered_set< _tKey, _fHash, _fEqual, _tAlloc > unordered_set
Definition: Container.h:150
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 转发对应参数。
Definition: ydef.h:1508
ystdex::cpp2017::size
auto size(const _tRange &c) -> decltype(c.size())
Definition: range.hpp:540
platform::ios::basic_stringstream
std::basic_stringstream< _tChar, _tTraits, _tAlloc > basic_stringstream
Definition: Container.h:326
platform::containers::multiset
std::multiset< _tKey, _fComp, _tAlloc > multiset
Definition: Container.h:122
platform::ios::basic_istringstream
std::basic_istringstream< _tChar, _tTraits, _tAlloc > basic_istringstream
Definition: Container.h:306
ystdex::cpp2017::end
auto end(_tRange &c) -> decltype(c.end())
Definition: range.hpp:345
ystdex::to_wstring
enable_if_t< is_arithmetic< _type >::value, wstring > to_wstring(_type val)
Definition: string.hpp:1880
platform::ios::stringbuf_for
basic_stringbuf< typename _tString::value_type, typename _tString::traits_type, typename _tString::allocator_type > stringbuf_for
Definition: Container.h:299
std
Definition: optional.h:982
platform::containers::priority_queue
std::priority_queue< _tKey, _tSeqCon, _fComp > priority_queue
Definition: Container.h:157
ystdex::sfmt
_tString sfmt(const typename string_traits< _tString >::value_type *fmt,...)
以 C 标准输出格式的输出 basic_string 实例的对象。
Definition: string.hpp:2028
YAssertNonnull
#define YAssertNonnull(_expr)
Definition: cassert.h:165
platform::strings::to_pmr_string
_tString to_pmr_string(basic_string_view< _tChar > sv)
Definition: Container.h:226
platform::containers::unordered_map
std::unordered_map< _tKey, _tMapped, _fHash, _fEqual, _tAlloc > unordered_map
Definition: Container.h:133
platform::containers::unordered_multiset
std::unordered_multiset< _tKey, _fHash, _fEqual, _tAlloc > unordered_multiset
Definition: Container.h:145
platform::ios::stringstream
stringstream_for< string > stringstream
Definition: Container.h:330
platform::containers::set
std::set< _tKey, _fComp, _tAlloc > set
Definition: Container.h:126
platform::ios::basic_ostringstream
std::basic_ostringstream< _tChar, _tTraits, _tAlloc > basic_ostringstream
Definition: Container.h:316
platform::containers::vector
std::vector< _type, _tAlloc > vector
Definition: Container.h:102
ystdex::to_string
std::string to_string(const std::thread::id &)
转换 thread::id 为字符串表示。
platform::strings
Definition: Container.h:166
memory_resource.h
存储资源。
ystdex::cpp2017::u16string_view
basic_string_view< char16_t > u16string_view
Definition: string_view.hpp:438
platform
默认平台命名空间。
Definition: Container.h:56
platform::containers::multimap
std::multimap< _tKey, _tMapped, _fComp, _tAlloc > multimap
Definition: Container.h:118
ystdex::basic_tstring_view
指定结束字符的只读字符串视图。
Definition: tstring_view.hpp:50
YModules.h
YSLib 用户界面模块配置文件。
platform::containers::deque
std::deque< _type, _tAlloc > deque
Definition: Container.h:93
platform::ios::wistringstream
istringstream_for< wstring > wistringstream
Definition: Container.h:311
ystdex::enable_if_convertible_t
enable_if_t< is_convertible< _tFrom, _tTo >::value, _type > enable_if_convertible_t
Definition: meta.hpp:837
ystdex
<memory> 特性测试宏。
Definition: ydef.h:1262
ystdex::cpp2017::basic_string_view::size
size_type size() const
Definition: string_view.hpp:184
platform::ios::basic_stringbuf
std::basic_stringbuf< _tChar, _tTraits, _tAlloc > basic_stringbuf
Definition: Container.h:296
platform::ios::ostringstream
ostringstream_for< string > ostringstream
Definition: Container.h:320
platform::ios::stringstream_for
basic_stringstream< typename _tString::value_type, typename _tString::traits_type, typename _tString::allocator_type > stringstream_for
Definition: Container.h:329
YSLib::Text::str
YB_ATTR_nodiscard YB_PURE const char const char * str
Definition: TextFile.h:83
platform::ios::stringbuf
stringbuf_for< string > stringbuf
Definition: Container.h:300
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:413
ystdex::make_string_view
basic_string_view< typename string_traits< _tString >::value_type, typename string_traits< _tString >::traits_type > make_string_view(const _tString &str)
从字符串创建 basic_string_view 对象。
Definition: string.hpp:814
platform::ios::wstringbuf
stringbuf_for< wstring > wstringbuf
Definition: Container.h:301
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:1199
functor.hpp
通用仿函数。
platform::containers::forward_list
std::forward_list< _type, _tAlloc > forward_list
Definition: Container.h:96
ystdex::wtstring_view
basic_tstring_view< wchar_t > wtstring_view
Definition: tstring_view.hpp:295
ystdex::tstring_view
basic_tstring_view< char > tstring_view
Definition: tstring_view.hpp:292
NPL::get
PDefH(void, LiftTermRef, TermNode &term, const TermNode &tm) ImplExpr(NPL ref get()
Definition: NPLA.h:1737
platform::ios::istringstream
istringstream_for< string > istringstream
Definition: Container.h:310
ystdex::list
列表容器。
Definition: list.hpp:1051
array.hpp
数组操作。
ystdex::cpp2017::begin
auto begin(_tRange &c) -> decltype(c.begin())
Definition: range.hpp:323
platform::ios::wostringstream
ostringstream_for< wstring > wostringstream
Definition: Container.h:321
ystdex::pmr::polymorphic_allocator
多态分配器。
Definition: memory_resource.h:276
NPL::_tParam
_tParam
Definition: NPLA.h:2367
platform::strings::string
basic_string< char > string
Definition: Container.h:178