Backport
iterator.hpp
Go to the documentation of this file.
1 
7 /*
8  The MIT License (MIT)
9 
10  Copyright (c) 2020 Matthew Rodusek All rights reserved.
11 
12  Permission is hereby granted, free of charge, to any person obtaining a copy
13  of this software and associated documentation files (the "Software"), to deal
14  in the Software without restriction, including without limitation the rights
15  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the Software is
17  furnished to do so, subject to the following conditions:
18 
19  The above copyright notice and this permission notice shall be included in
20  all copies or substantial portions of the Software.
21 
22  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  SOFTWARE.
29 */
30 #ifndef BPSTD_ITERATOR_HPP
31 #define BPSTD_ITERATOR_HPP
32 
33 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
34 # pragma once
35 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
36 
37 #include "detail/config.hpp"
38 #include "type_traits.hpp" // common_type_t
39 
40 #include <iterator>
41 #include <cstddef> // std::size_t
42 #include <initializer_list>
43 
44 BPSTD_COMPILER_DIAGNOSTIC_PREAMBLE
45 
46 namespace bpstd {
47 
48  //============================================================================
49  // class : reverse_iterator
50  //============================================================================
51 
52  template <typename T>
53  using reverse_iterator = std::reverse_iterator<T>;
54 
55  //============================================================================
56  // non-member functions : class : reverse_iterator
57  //============================================================================
58 
67  template <typename Iterator>
68  constexpr reverse_iterator<Iterator> make_reverse_iterator(Iterator i);
69 
70  //============================================================================
71  // non-member functions
72  //============================================================================
73 
79  template <typename C>
80  constexpr auto data(C& c) -> decltype(c.data());
81  template <typename C>
82  constexpr auto data(const C& c) -> decltype(c.data());
84 
89  template <typename T, std::size_t N>
90  constexpr T* data(T (&array)[N]) noexcept;
91 
96  template <typename E>
97  constexpr const E* data(std::initializer_list<E> il) noexcept;
98 
99  //----------------------------------------------------------------------------
100 
105  template <typename C>
106  constexpr auto empty(const C& c) -> decltype(c.empty());
107 
112  template <typename T, std::size_t N>
113  constexpr bool empty(const T (&array)[N]) noexcept;
114 
119  template <typename E>
120  constexpr bool empty(std::initializer_list<E> il) noexcept;
121 
122  //----------------------------------------------------------------------------
123 
128  template <typename C>
129  constexpr auto size(const C& c) -> decltype(c.size());
130 
135  template <typename T, std::size_t N>
136  constexpr std::size_t size(const T (&array)[N]) noexcept;
137 
138  //----------------------------------------------------------------------------
139 
144  template <typename C>
145  constexpr auto ssize(const C& c)
146  -> common_type_t<std::ptrdiff_t, make_signed_t<decltype(c.size())>>;
147 
152  template <typename T, std::ptrdiff_t N>
153  constexpr std::ptrdiff_t ssize(const T (&array)[N]) noexcept;
154 
155 } // namespace bpstd
156 
157 //==============================================================================
158 // non-member functions : class : reverse_iterator
159 //==============================================================================
160 
161 //------------------------------------------------------------------------------
162 // Utilities
163 //------------------------------------------------------------------------------
164 
165 template <typename Iterator>
166 inline BPSTD_INLINE_VISIBILITY constexpr
167  bpstd::reverse_iterator<Iterator>
168  bpstd::make_reverse_iterator(Iterator i)
169 {
170  return reverse_iterator<Iterator>{i};
171 }
172 
173 //==============================================================================
174 // non-member functions
175 //==============================================================================
176 
177 //------------------------------------------------------------------------------
178 // Utilities
179 //------------------------------------------------------------------------------
180 
181 template <typename C>
182 inline BPSTD_INLINE_VISIBILITY constexpr
183  auto bpstd::data(C& c)
184  -> decltype(c.data())
185 {
186  return c.data();
187 }
188 
189 template <typename C>
190 inline BPSTD_INLINE_VISIBILITY constexpr
191  auto bpstd::data(const C& c)
192  -> decltype(c.data())
193 {
194  return c.data();
195 }
196 template <typename T, std::size_t N>
197 inline BPSTD_INLINE_VISIBILITY constexpr
198  T* bpstd::data(T (&array)[N])
199  noexcept
200 {
201  return array;
202 }
203 
204 template <typename E>
205 inline BPSTD_INLINE_VISIBILITY constexpr
206  const E* bpstd::data(std::initializer_list<E> il)
207  noexcept
208 {
209  return il.begin();
210 }
211 
212 //------------------------------------------------------------------------------
213 
214 template <typename C>
215 inline BPSTD_INLINE_VISIBILITY constexpr
216  auto bpstd::empty(const C& c)
217  -> decltype(c.empty())
218 {
219  return c.empty();
220 }
221 
222 template <typename T, std::size_t N>
223 inline BPSTD_INLINE_VISIBILITY constexpr
224  bool bpstd::empty(const T (&)[N])
225  noexcept
226 {
227  return N == 0;
228 }
229 
230 template <typename E>
231 inline BPSTD_INLINE_VISIBILITY constexpr
232  bool bpstd::empty(std::initializer_list<E> il)
233  noexcept
234 {
235  return il.begin() == il.end();
236 }
237 
238 //------------------------------------------------------------------------------
239 
240 template <typename C>
241 inline BPSTD_INLINE_VISIBILITY constexpr
242  auto bpstd::size(const C& c)
243  -> decltype(c.size())
244 {
245  return c.size();
246 }
247 
248 template <typename T, std::size_t N>
249 inline BPSTD_INLINE_VISIBILITY constexpr
250  std::size_t bpstd::size(const T (&)[N])
251  noexcept
252 {
253  return N;
254 }
255 
256 //------------------------------------------------------------------------------
257 
258 template <typename C>
259 inline BPSTD_INLINE_VISIBILITY constexpr
260  auto bpstd::ssize(const C& c)
261  -> bpstd::common_type_t<std::ptrdiff_t, bpstd::make_signed_t<decltype(c.size())>>
262 {
263  using type = bpstd::common_type_t<std::ptrdiff_t, bpstd::make_signed_t<decltype(c.size())>>;
264 
265  return static_cast<type>(c.size());
266 }
267 
268 template <typename T, std::ptrdiff_t N>
269 inline BPSTD_INLINE_VISIBILITY constexpr
270  std::ptrdiff_t bpstd::ssize(const T (&)[N])
271  noexcept
272 {
273  return N;
274 }
275 
276 BPSTD_COMPILER_DIAGNOSTIC_POSTAMBLE
277 
278 #endif /* BPSTD_ITERATOR_HPP */
bpstd::ssize
constexpr std::ptrdiff_t ssize(const T(&array)[N]) noexcept
Gets the signed size of an array.
type_traits.hpp
This header provides definitions from the C++ header <type_traits>
bpstd::size
constexpr auto size(const C &c) -> decltype(c.size())
Gets the size of a container.
bpstd::ssize
constexpr auto ssize(const C &c) -> common_type_t< std::ptrdiff_t, make_signed_t< decltype(c.size())>>
Gets the signed-size of a container.
bpstd::make_reverse_iterator
constexpr reverse_iterator< Iterator > make_reverse_iterator(Iterator i)
Makes a reverse_iterator given an iterator i.
bpstd::data
constexpr const E * data(std::initializer_list< E > il) noexcept
Gets a pointer to the start of an initializer list.
bpstd::empty
constexpr auto empty(const C &c) -> decltype(c.empty())
Queries whether c is empty.
bpstd::size
constexpr std::size_t size(const T(&array)[N]) noexcept
Gets the size of an array.
bpstd::data
constexpr auto data(C &c) -> decltype(c.data())
Gets a pointer to the data from c.
bpstd::empty
constexpr bool empty(std::initializer_list< E > il) noexcept
Queries whether an initializer list is empty.