Skip to content

Commit

Permalink
✨ Add cbegin shime
Browse files Browse the repository at this point in the history
— 🛠 Improve span implementation's deduction guides
  • Loading branch information
ThePhD committed Jul 16, 2022
1 parent 9fa88d1 commit 288dd28
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 27 deletions.
46 changes: 20 additions & 26 deletions include/ztd/idk/detail/span.implementation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ namespace nonstd { namespace span_lite {
#elif span_HAVE(CONSTRAINED_SPAN_CONTAINER_CTOR)

template <typename T, std::size_t N>
inline span_constexpr auto size(const T (&)[N]) span_noexcept -> size_t {
inline span_constexpr auto size(const T (&)[N]) span_noexcept->size_t {
return N;
}

Expand All @@ -678,7 +678,7 @@ namespace nonstd { namespace span_lite {
}

template <typename T, std::size_t N>
inline span_constexpr auto data(T (&arr)[N]) span_noexcept -> T* {
inline span_constexpr auto data(T (&arr)[N]) span_noexcept->T* {
return &arr[0];
}

Expand All @@ -693,7 +693,7 @@ namespace nonstd { namespace span_lite {
}

template <typename E>
inline span_constexpr auto data(std::initializer_list<E> il) span_noexcept -> E const* {
inline span_constexpr auto data(std::initializer_list<E> il) span_noexcept->E const* {
return il.begin();
}

Expand Down Expand Up @@ -940,25 +940,21 @@ namespace nonstd { namespace span_lite {

#if span_HAVE(ARRAY)

template <std::size_t N span_REQUIRES_T(((Extent == dynamic_extent || Extent == static_cast<extent_t>(N))
&& std::is_convertible<value_type (*)[], element_type (*)[]>::value))>
#if span_FEATURE(CONSTRUCTION_FROM_STDARRAY_ELEMENT_TYPE)
span_constexpr span(std::array<element_type, N>& arr) span_noexcept
#else
span_constexpr span(std::array<value_type, N>& arr) span_noexcept
#endif
: data_(arr.data()),
size_(to_size(arr.size())) {
template <typename U,
std::size_t N span_REQUIRES_T(((Extent == dynamic_extent || Extent == static_cast<extent_t>(N))
&& std::is_convertible<U (*)[], element_type (*)[]>::value))>
span_constexpr span(std::array<U, N>& arr) span_noexcept : data_(arr.data()), size_(to_size(arr.size())) {
}

template <std::size_t N
template <typename U,
std::size_t N
#if span_HAVE(DEFAULT_FUNCTION_TEMPLATE_ARG)
span_REQUIRES_T(((Extent == dynamic_extent || Extent == static_cast<extent_t>(N))
&& std::is_convertible<value_type (*)[], element_type (*)[]>::value))
&& std::is_convertible<U (*)[], element_type (*)[]>::value))
#endif
>
span_constexpr span(std::array<value_type, N> const& arr) span_noexcept : data_(arr.data()),
size_(to_size(arr.size())) {
span_constexpr span(std::array<U, N> const& arr) span_noexcept : data_(arr.data()),
size_(to_size(arr.size())) {
}

#endif // span_HAVE( ARRAY )
Expand Down Expand Up @@ -1451,28 +1447,26 @@ namespace nonstd { namespace span_lite {
#if span_USES_STD_SPAN

template <class Container, class EP = decltype(std::data(std::declval<Container&>()))>
inline span_constexpr auto make_span(Container& cont) span_noexcept
-> span<typename std::remove_pointer<EP>::type> {
inline span_constexpr auto make_span(Container& cont) span_noexcept->span<typename std::remove_pointer<EP>::type> {
return span<typename std::remove_pointer<EP>::type>(cont);
}

template <class Container, class EP = decltype(std::data(std::declval<Container&>()))>
inline span_constexpr auto make_span(Container const& cont) span_noexcept
-> span<const typename std::remove_pointer<EP>::type> {
inline span_constexpr auto make_span(
Container const& cont) span_noexcept->span<const typename std::remove_pointer<EP>::type> {
return span<const typename std::remove_pointer<EP>::type>(cont);
}

#elif span_HAVE(CONSTRAINED_SPAN_CONTAINER_CTOR) && span_HAVE(AUTO)

template <class Container, class EP = decltype(std17::data(std::declval<Container&>()))>
inline span_constexpr auto make_span(Container& cont) span_noexcept
-> span<typename std::remove_pointer<EP>::type> {
inline span_constexpr auto make_span(Container& cont) span_noexcept->span<typename std::remove_pointer<EP>::type> {
return span<typename std::remove_pointer<EP>::type>(cont);
}

template <class Container, class EP = decltype(std17::data(std::declval<Container&>()))>
inline span_constexpr auto make_span(Container const& cont) span_noexcept
-> span<const typename std::remove_pointer<EP>::type> {
inline span_constexpr auto make_span(
Container const& cont) span_noexcept->span<const typename std::remove_pointer<EP>::type> {
return span<const typename std::remove_pointer<EP>::type>(cont);
}

Expand Down Expand Up @@ -1608,12 +1602,12 @@ namespace nonstd {
namespace nonstd { namespace span_lite {

template <class T>
inline span_constexpr auto byte_span(T& t) span_noexcept -> span<std17::byte, span_sizeof(T)> {
inline span_constexpr auto byte_span(T& t) span_noexcept->span<std17::byte, span_sizeof(T)> {
return span<std17::byte, span_sizeof(t)>(reinterpret_cast<std17::byte*>(&t), span_sizeof(T));
}

template <class T>
inline span_constexpr auto byte_span(T const& t) span_noexcept -> span<const std17::byte, span_sizeof(T)> {
inline span_constexpr auto byte_span(T const& t) span_noexcept->span<const std17::byte, span_sizeof(T)> {
return span<const std17::byte, span_sizeof(t)>(reinterpret_cast<std17::byte const*>(&t), span_sizeof(T));
}

Expand Down
36 changes: 36 additions & 0 deletions include/ztd/idk/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ namespace ztd {
struct __is_specialization_of_impl : ::std::false_type { };
template <typename... T, template <typename...> class Templ>
struct __is_specialization_of_impl<Templ<T...>, Templ> : ::std::true_type { };

template<typename _It>
using __weakly_incrementable_test = decltype(++::std::declval<_It&>());

template<typename _It>
using __weakly_decrementable_test = decltype(--::std::declval<_It&>());

} // namespace __idk_detail

//////
Expand Down Expand Up @@ -117,6 +124,25 @@ namespace ztd {
::std::is_same_v<_Type, char32_t>
> {};

//////
/// @brief Checks if the given type is one of the types that is used as a code unit (unnamed char, wchar_t, char8_t, char16_t, and char32_t).
template <typename _Type>
class is_code_unit : public ::std::integral_constant<bool,
::std::is_same_v<_Type, char> ||
::std::is_same_v<_Type, wchar_t> ||
#if ZTD_IS_ON(ZTD_NATIVE_CHAR8_T)
::std::is_same_v<_Type, char8_t> ||
#endif
::std::is_same_v<_Type, char16_t> ||
::std::is_same_v<_Type, char32_t>
> {};
// clang-format on

//////
/// @brief An @c _v alias for ztd::is_code_unit.
template <typename _Type>
inline constexpr bool is_code_unit_v = is_code_unit<_Type>::value;

//////
/// @brief Checks if the given type is one of the types that is usable in the standard with the @c std::char_traits traits type that's used for @c std::string_view , @c std::string and others.
template <typename _Type>
Expand Down Expand Up @@ -277,6 +303,16 @@ namespace ztd {
template <typename _Left, typename _Right>
using detect_equality_comparable = decltype(::std::declval<_Left>() == ::std::declval<_Right>());

//////
/// @brief Detects whether a type has a pre-increment operation.
template<typename _It>
inline constexpr bool weakly_incrementable_v = ::ztd::is_detected_v<__idk_detail::__weakly_incrementable_test, _It>;

//////
/// @brief Detects whether a type has a post-increment operation.
template<typename _It>
inline constexpr bool weakly_decrementable_v = ::ztd::is_detected_v<__idk_detail::__weakly_decrementable_test, _It>;

ZTD_IDK_INLINE_ABI_NAMESPACE_CLOSE_I_
} // namespace ztd

Expand Down
18 changes: 17 additions & 1 deletion include/ztd/ranges/adl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,14 @@ namespace ztd { namespace ranges {
::std::forward<_ItLeft>(__left), ::std::forward<_ItRight>(__right))) {
::std::ranges::iter_swap(::std::forward<_ItLeft>(__left), ::std::forward<_ItRight>(__right));
}

template <typename _ItLeft, typename _ItRight>
constexpr auto adl_swap(_ItLeft&& __left, _ItRight&& __right) noexcept(
noexcept(::std::ranges::swap(::std::forward<_ItLeft>(__left), ::std::forward<_ItRight>(__right))))
-> decltype(::std::ranges::swap(
::std::forward<_ItLeft>(__left), ::std::forward<_ItRight>(__right))) {
::std::ranges::swap(::std::forward<_ItLeft>(__left), ::std::forward<_ItRight>(__right));
}
} // namespace ranges_adl
#else
// Blessed Overload Overlord Xeo,
Expand All @@ -277,6 +285,7 @@ namespace ztd { namespace ranges {
using ::std::rend;

using ::std::iter_swap;
using ::std::swap;

template <typename _Range,
::std::enable_if_t<::std::is_rvalue_reference_v<_Range> && !::std::is_const_v<_Range>>* = nullptr>
Expand Down Expand Up @@ -593,7 +602,14 @@ namespace ztd { namespace ranges {
-> decltype(iter_swap(::std::forward<_ItLeft>(__left), ::std::forward<_ItRight>(__right))) {
iter_swap(::std::forward<_ItLeft>(__left), ::std::forward<_ItRight>(__right));
}
} // namespace ranges_adl

template <typename _ItLeft, typename _ItRight>
constexpr auto adl_swap(_ItLeft&& __left, _ItRight&& __right) noexcept(
noexcept(swap(::std::forward<_ItLeft>(__left), ::std::forward<_ItRight>(__right))))
-> decltype(swap(::std::forward<_ItLeft>(__left), ::std::forward<_ItRight>(__right))) {
swap(::std::forward<_ItLeft>(__left), ::std::forward<_ItRight>(__right));
}
} // namespace ranges_adl
#endif

template <typename _Range>
Expand Down
18 changes: 18 additions & 0 deletions include/ztd/ranges/subrange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,24 @@ namespace ztd { namespace ranges {
return this->_M_it == this->_M_sen;
}

//////
/// @brief The stored begin iterator, const-ified.
///
/// @remarks This must be reimplemetned at some point.
/////
constexpr iterator cbegin() const noexcept {
return this->_M_it;
}

//////
/// @brief The stored end iterator.
///
/// @remarks This must be reimplemetned at some point.
/////
constexpr sentinel cend() const noexcept {
return this->_M_sen;
}

//////
/// @brief The size of the range.
///
Expand Down

0 comments on commit 288dd28

Please sign in to comment.