YSTest: iterator_op.hpp 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

iterator_op.hpp
浏览该文件的文档.
1 /*
2 © 2013-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 YB_INC_ystdex_iterator_op_hpp_
29 #define YB_INC_ystdex_iterator_op_hpp_ 1
30
31 #include "range.hpp" // for "range.hpp", std::next, std::prev,
32 // std::make_move_iterator, std::reverse_iterator, std::make_pair, begin, end;
33 #include "cassert.h" // for yconstraint;
34 #include "deref_op.hpp" // for "deref_op.hpp", YB_VerifyIterator;
35 #include "operators.hpp" // for input_iteratable, output_iteratable,
36 // forward_iteratable, bidirectional_iteratable, random_access_iteratable, _t;
37
38 namespace ystdex
39 {
40
53 template<typename _tIn, typename _fPred>
54 _tIn
55 next_if(_tIn i, _fPred f,
56 typename std::iterator_traits<_tIn>::difference_type n = 1)
57 {
58 YB_VerifyIterator(i);
59 return f(*i) ? std::next(i, n) : i;
60 }
61 template<typename _tIn, typename _type>
62 _tIn
63 next_if_eq(_tIn i, const _type& val,
64 typename std::iterator_traits<_tIn>::difference_type n = 1)
65 {
66 YB_VerifyIterator(i);
67 return *i == val ? std::next(i, n) : i;
68 }
70
76 template<typename _tBi, typename _fPred>
77 _tBi
78 prev_if(_tBi i, _fPred f,
79 typename std::iterator_traits<_tBi>::difference_type n = 1)
80 {
81 return f(*i) ? std::prev(i, n) : i;
82 }
83 template<typename _tBi, typename _type>
84 _tBi
85 prev_if_eq(_tBi i, const _type& val,
86 typename std::iterator_traits<_tBi>::difference_type n = 1)
87 {
88 return *i == val ? std::prev(i, n) : i;
89 }
91
92
93
95 inline namespace cpp2014
96 {
97
104 #if __cpp_lib_make_reverse_iterator >= 201402L && YB_Use_std_reverse_iterator
105 using std::make_reverse_iterator;
106 #else
107 template<typename _tIter>
109 YB_ATTR_nodiscard YB_STATELESS reverse_iterator<_tIter>
110 make_reverse_iterator(_tIter i)
111 {
112 return reverse_iterator<_tIter>(i);
113 }
114 #endif
115
116 } // inline namespace cpp2014;
117
119
120
124 template<typename _tIter, typename _tRet = cond_t<is_throwing_move_copyable<
125 typename std::iterator_traits<_tIter>::value_type>, _tIter,
126 std::move_iterator<_tIter>>>
127 YB_ATTR_nodiscard YB_STATELESS yconstfn _tRet
128 make_move_if_noexcept_iterator(_tIter it)
129 {
130 return _tRet(it);
131 }
132
137 template<typename _tIter1, typename _tIter2>
138 YB_ATTR_nodiscard YB_STATELESS inline auto
139 make_move_iterator_pair(_tIter1 it1, _tIter2 it2) -> decltype(
140 std::make_pair(std::make_move_iterator(it1), std::make_move_iterator(it2)))
141 {
142 return std::make_pair(std::make_move_iterator(it1),
143 std::make_move_iterator(it2));
144 }
151 template<typename _tRange>
152 YB_ATTR_nodiscard YB_STATELESS inline auto
153 make_move_iterator_pair(_tRange& c)
154 -> decltype(ystdex::make_move_iterator_pair(c.begin(), c.end()))
155 {
156 return ystdex::make_move_iterator_pair(begin(c), end(c));
157 }
159
160
162 namespace details
163 {
164
165 template<typename _tIter, typename _tTraits,
166 typename _tTag = typename _tTraits::iterator_category>
167 struct iterator_operators;
168
169 template<typename _tIter, typename _tTraits>
170 struct iterator_operators<_tIter, _tTraits, std::input_iterator_tag>
171 {
172 using type = input_iteratable<_tIter, typename _tTraits::reference>;
173 };
174
175 template<typename _tIter, typename _tTraits>
176 struct iterator_operators<_tIter, _tTraits, std::output_iterator_tag>
177 {
178 using type = output_iteratable<_tIter, typename _tTraits::reference>;
179 };
180
181 template<typename _tIter, typename _tTraits>
182 struct iterator_operators<_tIter, _tTraits, std::forward_iterator_tag>
183 {
184 using type = forward_iteratable<_tIter, typename _tTraits::reference>;
185 };
186
187 template<typename _tIter, typename _tTraits>
188 struct iterator_operators<_tIter, _tTraits, std::bidirectional_iterator_tag>
189 {
190 using type = bidirectional_iteratable<_tIter, typename _tTraits::reference>;
191 };
192
193 template<typename _tIter, typename _tTraits>
194 struct iterator_operators<_tIter, _tTraits, std::random_access_iterator_tag>
195 {
196 using type = random_access_iteratable<_tIter,
197 typename _tTraits::difference_type, typename _tTraits::reference>;
198 };
199
200 } // namespace details;
201
202
210 template<typename _tIter, typename _tTraits = std::iterator_traits<_tIter>>
211 using iterator_operators_t = _t<details::iterator_operators<_tIter, _tTraits>>;
212
213 } // namespace ystdex;
214
215 #endif
216
YB_ATTR_nodiscard
#define YB_ATTR_nodiscard
标记返回值不被忽略的属性。
Definition: ydef.h:715
ystdex::cpp2014::make_reverse_iterator
reverse_iterator< _tIter > make_reverse_iterator(_tIter i)
构造反向迭代器。
Definition: iterator_op.hpp:110
CHRLib::c
c
Definition: CharacterProcessing.h:84
ystdex::cpp2017::reverse_iterator
Definition: range.hpp:116
YSLib::IO::_tTraits
const string &path const String &path class class _tTraits
Definition: YSLib/Service/FileSystem.h:582
ystdex::make_move_if_noexcept_iterator
_tRet make_move_if_noexcept_iterator(_tIter it)
按 std::move_if_noexcept 的条件构造转移迭代器或非转移迭代器。
Definition: iterator_op.hpp:128
next
tidy_ptr< slink > next
Definition: memory_resource.cpp:223
ystdex::make_move_iterator_pair
auto make_move_iterator_pair(_tIter1 it1, _tIter2 it2) -> decltype(std::make_pair(std::make_move_iterator(it1), std::make_move_iterator(it2)))
构造转移迭代器对。
Definition: iterator_op.hpp:139
CHRLib::f
f(d, s)
ystdex::next_if_eq
_tIn next_if_eq(_tIn i, const _type &val, typename std::iterator_traits< _tIn >::difference_type n=1)
Definition: iterator_op.hpp:63
ystdex::prev_if
_tBi prev_if(_tBi i, _fPred f, typename std::iterator_traits< _tBi >::difference_type n=1)
迭代器指向的值满足条件时取反向邻接迭代器，否则取原值。
Definition: iterator_op.hpp:78
ystdex::details::ebases
Definition: operators.hpp:108
ystdex::prev_if_eq
_tBi prev_if_eq(_tBi i, const _type &val, typename std::iterator_traits< _tBi >::difference_type n=1)
Definition: iterator_op.hpp:85
YB_VerifyIterator
#define YB_VerifyIterator(_expr)
验证表达式求值的迭代器的有效性。
Definition: deref_op.hpp:67
YB_STATELESS
#define YB_STATELESS
指示函数或函数模板实例为无状态函数。
Definition: ydef.h:941
ystdex::details::iterator_operators
Definition: iterator_op.hpp:167
ystdex::cpp2017::end
auto end(_tRange &c) -> decltype(c.end())
Definition: range.hpp:345
std
Definition: optional.h:982
operators.hpp
重载操作符。
YSLib::i
return i
Definition: YCoreUtilities.h:177
ystdex::iterator_operators_t
_t< details::iterator_operators< _tIter, _tTraits > > iterator_operators_t
按迭代器类别取可实现迭代器的重载操作符集合的实现。
Definition: iterator_op.hpp:211
range.hpp
范围操作。
ystdex::cond_t
_t< cond< _tCond, _tThen, _tElse > > cond_t
Definition: meta.hpp:794
ystdex
<memory> 特性测试宏。
Definition: ydef.h:1262
ystdex::next_if
_tIn next_if(_tIn i, _fPred f, typename std::iterator_traits< _tIn >::difference_type n=1)
迭代器指向的值满足条件时取邻接迭代器，否则取原值。
Definition: iterator_op.hpp:55
ystdex::_t
typename _type::type _t
嵌套类型别名。
Definition: meta.hpp:45
cassert.h
ISO C 断言/调试跟踪扩展。
deref_op.hpp
解引用操作。
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:1073
ystdex::cpp2017::begin
auto begin(_tRange &c) -> decltype(c.begin())
Definition: range.hpp:323