Debian Codegraph @

Utilities/std/cm/bits/container_helpers.hxx

raw
1 // -*-c++-*-
2 // vim: set ft=cpp:
3
4 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
5 file LICENSE.rst or https://cmake.org/licensing for details. */
6
7 #pragma once
8
9 #include <iterator> // IWYU pragma: keep
10
11 #if __cplusplus < 201402L || defined(_MSVC_LANG) && _MSVC_LANG < 201402L
12 # include <initializer_list>
13 #endif
14 #if __cplusplus < 202002L || defined(_MSVC_LANG) && _MSVC_LANG < 202002L
15 # include <cstddef>
16 # include <type_traits>
17 #endif
18
19 namespace cm {
20
21 using std::begin;
22 using std::end;
23
24 #if __cplusplus < 201402L || defined(_MSVC_LANG) && _MSVC_LANG < 201402L
25
26 template <typename C>
27 # if defined(_MSC_VER) && _MSC_VER < 1900
28 inline auto cbegin(C const& c)
29 # else
30 inline constexpr auto cbegin(C const& c) noexcept(noexcept(std::begin(c)))
31 # endif
32 -> decltype(std::begin(c))
33 {
34 return std::begin(c);
35 }
36
37 template <typename C>
38 # if defined(_MSC_VER) && _MSC_VER < 1900
39 inline auto cend(C const& c)
40 # else
41 inline constexpr auto cend(C const& c) noexcept(noexcept(std::end(c)))
42 # endif
43 -> decltype(std::end(c))
44 {
45 return std::end(c);
46 }
47
48 template <typename C>
49 # if defined(_MSC_VER) && _MSC_VER < 1900
50 inline auto rbegin(C& c)
51 # else
52 inline constexpr auto rbegin(C& c)
53 # endif
54 -> decltype(c.rbegin())
55 {
56 return c.rbegin();
57 }
58 template <typename C>
59 # if defined(_MSC_VER) && _MSC_VER < 1900
60 inline auto rbegin(C const& c)
61 # else
62 inline constexpr auto rbegin(C const& c)
63 # endif
64 -> decltype(c.rbegin())
65 {
66 return c.rbegin();
67 }
68 template <typename T, std::size_t N>
69 # if defined(_MSC_VER) && _MSC_VER < 1900
70 inline std::reverse_iterator<T*> rbegin(T (&array)[N])
71 # else
72 inline constexpr std::reverse_iterator<T*> rbegin(T (&array)[N]) noexcept
73 # endif
74 {
75 return std::reverse_iterator<T*>(array + N);
76 }
77 template <typename T>
78 # if defined(_MSC_VER) && _MSC_VER < 1900
79 inline std::reverse_iterator<T const*> rbegin(std::initializer_list<T> il)
80 # else
81 inline constexpr std::reverse_iterator<T const*> rbegin(
82 std::initializer_list<T> il) noexcept
83 # endif
84 {
85 return std::reverse_iterator<T const*>(il.end());
86 }
87
88 template <typename C>
89 # if defined(_MSC_VER) && _MSC_VER < 1900
90 inline auto rend(C& c)
91 # else
92 inline constexpr auto rend(C& c)
93 # endif
94 -> decltype(c.rend())
95
96 {
97 return c.rend();
98 }
99 template <typename C>
100 # if defined(_MSC_VER) && _MSC_VER < 1900
101 inline auto rend(C const& c)
102 # else
103 inline constexpr auto rend(C const& c)
104 # endif
105 -> decltype(c.rend())
106 {
107 return c.rend();
108 }
109 template <typename T, std::size_t N>
110 # if defined(_MSC_VER) && _MSC_VER < 1900
111 inline std::reverse_iterator<T*> rend(T (&array)[N])
112 # else
113 inline constexpr std::reverse_iterator<T*> rend(T (&array)[N]) noexcept
114 # endif
115 {
116 return std::reverse_iterator<T*>(array);
117 }
118 template <typename T>
119 # if defined(_MSC_VER) && _MSC_VER < 1900
120 inline std::reverse_iterator<T const*> rend(std::initializer_list<T> il)
121 # else
122 inline constexpr std::reverse_iterator<T const*> rend(
123 std::initializer_list<T> il) noexcept
124 # endif
125 {
126 return std::reverse_iterator<T const*>(il.begin());
127 }
128
129 template <typename C>
130 # if defined(_MSC_VER) && _MSC_VER < 1900
131 inline auto crbegin(C const& c)
132 # else
133 inline constexpr auto crbegin(C const& c)
134 # endif
135 -> decltype(cm::rbegin(c))
136 {
137 return cm::rbegin(c);
138 }
139
140 template <typename C>
141 # if defined(_MSC_VER) && _MSC_VER < 1900
142 inline auto crend(C const& c)
143 # else
144 inline constexpr auto crend(C const& c)
145 # endif
146 -> decltype(cm::rend(c))
147 {
148 return cm::rend(c);
149 }
150
151 #else
152
153 using std::cbegin;
154 using std::cend;
155
156 using std::rbegin;
157 using std::rend;
158
159 using std::crbegin;
160 using std::crend;
161
162 #endif
163
164 #if __cplusplus < 201703L || defined(_MSVC_LANG) && _MSVC_LANG < 201703L
165
166 template <typename C>
167 # if defined(_MSC_VER) && _MSC_VER < 1900
168 inline auto size(C const& c)
169 # else
170 inline constexpr auto size(C const& c) noexcept(noexcept(c.size()))
171 # endif
172 -> decltype(c.size())
173 {
174 return c.size();
175 }
176
177 template <typename T, std::size_t N>
178 # if defined(_MSC_VER) && _MSC_VER < 1900
179 inline std::size_t size(T const (&)[N])
180 # else
181 inline constexpr std::size_t size(T const (&)[N]) noexcept
182 # endif
183 {
184 return N;
185 }
186
187 template <typename C>
188 # if defined(_MSC_VER) && _MSC_VER < 1900
189 inline auto empty(C const& c)
190 # else
191 inline constexpr auto empty(C const& c) noexcept(noexcept(c.empty()))
192 # endif
193 -> decltype(c.empty())
194 {
195 return c.empty();
196 }
197
198 template <typename T, std::size_t N>
199 # if defined(_MSC_VER) && _MSC_VER < 1900
200 inline bool empty(T const (&)[N])
201 # else
202 inline constexpr bool empty(T const (&)[N]) noexcept
203 # endif
204 {
205 return false;
206 }
207
208 template <typename E>
209 # if defined(_MSC_VER) && _MSC_VER < 1900
210 inline bool empty(std::initializer_list<E> il)
211 # else
212 inline constexpr bool empty(std::initializer_list<E> il) noexcept
213 # endif
214 {
215 return il.size() == 0;
216 }
217
218 template <typename C>
219 # if defined(_MSC_VER) && _MSC_VER < 1900
220 inline auto data(C& c) -> decltype(c.data())
221 # else
222 inline constexpr auto data(C& c) noexcept(noexcept(c.data()))
223 # endif
224 -> decltype(c.data())
225 {
226 return c.data();
227 }
228
229 template <typename C>
230 # if defined(_MSC_VER) && _MSC_VER < 1900
231 inline auto data(C const& c)
232 # else
233 inline constexpr auto data(C const& c) noexcept(noexcept(c.data()))
234 # endif
235 -> decltype(c.data())
236 {
237 return c.data();
238 }
239
240 template <typename T, std::size_t N>
241 # if defined(_MSC_VER) && _MSC_VER < 1900
242 inline T* data(T (&array)[N])
243 # else
244 inline constexpr T* data(T (&array)[N]) noexcept
245 # endif
246 {
247 return array;
248 }
249
250 template <typename E>
251 # if defined(_MSC_VER) && _MSC_VER < 1900
252 inline E const* data(std::initializer_list<E> il)
253 # else
254 inline constexpr E const* data(std::initializer_list<E> il) noexcept
255 # endif
256 {
257 return il.begin();
258 }
259
260 #else
261
262 using std::size;
263 using std::empty;
264 using std::data;
265
266 #endif
267
268 #if __cplusplus < 202002L || defined(_MSVC_LANG) && _MSVC_LANG < 202002L
269
270 template <typename C>
271 # if defined(_MSC_VER) && _MSC_VER < 1900
272 inline auto ssize(C const& c)
273 # else
274 inline constexpr auto ssize(C const& c)
275 # endif
276 -> typename std::common_type<
277 std::ptrdiff_t, typename std::make_signed<decltype(c.size())>::type>::type
278 {
279 using signed_type = typename std::make_signed<decltype(c.size())>::type;
280 using result_type =
281 typename std::common_type<std::ptrdiff_t, signed_type>::type;
282
283 return static_cast<result_type>(c.size());
284 }
285
286 template <typename T, std::ptrdiff_t N>
287 # if defined(_MSC_VER) && _MSC_VER < 1900
288 inline std::ptrdiff_t ssize(T const (&)[N])
289 # else
290 inline constexpr std::ptrdiff_t ssize(T const (&)[N]) noexcept
291 # endif
292 {
293 return N;
294 }
295
296 #else
297
298 using std::ssize;
299
300 #endif
301
302 } // namespace cm