diff --git a/ostd/internal/tuple.hh b/ostd/internal/tuple.hh index d4b619d..2e2a2c8 100644 --- a/ostd/internal/tuple.hh +++ b/ostd/internal/tuple.hh @@ -7,6 +7,8 @@ #ifndef OSTD_INTERNAL_TUPLE_HH #define OSTD_INTERNAL_TUPLE_HH +#include + #include "ostd/types.hh" #include "ostd/type_traits.hh" @@ -63,6 +65,39 @@ constexpr bool IsTupleLike = IsTupleLike; template constexpr bool IsTupleLike = IsTupleLike; +/* std::tuple specializations */ + +template +constexpr bool IsTupleLike> = true; + +template +constexpr Size TupleSize> = sizeof...(T); + +template +struct TupleElementBase> { + using Type = std::tuple_element_t>; +}; + +template +inline TupleElement> &get(std::tuple &t) noexcept { + return std::get(t); +} + +template +inline const TupleElement> &get(const std::tuple &t) noexcept { + return std::get(t); +} + +template +inline TupleElement> &&get(std::tuple &&t) noexcept { + return std::get(std::move(t)); +} + +template +inline const TupleElement> &&get(const std::tuple &&t) noexcept{ + return std::get(std::move(t)); +} + /* tuple specializations */ template diff --git a/ostd/range.hh b/ostd/range.hh index 6feb561..808c4c9 100644 --- a/ostd/range.hh +++ b/ostd/range.hh @@ -8,12 +8,14 @@ #include #include + #include +#include +#include #include "ostd/types.hh" #include "ostd/utility.hh" #include "ostd/type_traits.hh" -#include "ostd/tuple.hh" namespace ostd { @@ -687,16 +689,20 @@ inline auto chunks(T n) { namespace detail { template - inline auto join_proxy(T &&obj, Tuple &&tup, TupleIndices) { + inline auto join_proxy( + T &&obj, std::tuple &&tup, std::index_sequence + ) { return obj.join(std::forward( - get(std::forward>(tup)) + std::get(std::forward>(tup)) )...); } template - inline auto zip_proxy(T &&obj, Tuple &&tup, TupleIndices) { + inline auto zip_proxy( + T &&obj, std::tuple &&tup, std::index_sequence + ) { return obj.zip(std::forward( - get(std::forward>(tup)) + std::get(std::forward>(tup)) )...); } } @@ -711,12 +717,14 @@ inline auto join(R &&range) { template inline auto join(R1 &&r1, R &&...rr) { return [ - ranges = forward_as_tuple(std::forward(r1), std::forward(rr)...) + ranges = std::forward_as_tuple( + std::forward(r1), std::forward(rr)... + ) ] (auto &&obj) mutable { - using Index = detail::MakeTupleIndices; return detail::join_proxy( std::forward(obj), - std::forward(ranges), Index() + std::forward(ranges), + std::make_index_sequence() ); }; } @@ -731,12 +739,14 @@ inline auto zip(R &&range) { template inline auto zip(R1 &&r1, R &&...rr) { return [ - ranges = forward_as_tuple(std::forward(r1), std::forward(rr)...) + ranges = std::forward_as_tuple( + std::forward(r1), std::forward(rr)... + ) ] (auto &&obj) mutable { - using Index = detail::MakeTupleIndices; return detail::zip_proxy( std::forward(obj), - std::forward(ranges), Index() + std::forward(ranges), + std::make_index_sequence() ); }; } @@ -1452,7 +1462,7 @@ namespace detail { struct JoinRangeEmpty { template static bool empty(T const &tup) { - if (!ostd::get(tup).empty()) { + if (!std::get(tup).empty()) { return false; } return JoinRangeEmpty::empty(tup); @@ -1471,7 +1481,7 @@ namespace detail { struct TupleRangeEqual { template static bool equal(T const &tup1, T const &tup2) { - if (!ostd::get(tup1).equals_front(ostd::get(tup2))) { + if (!std::get(tup1).equals_front(std::get(tup2))) { return false; } return TupleRangeEqual::equal(tup1, tup2); @@ -1490,8 +1500,8 @@ namespace detail { struct JoinRangePop { template static bool pop(T &tup) { - if (!ostd::get(tup).empty()) { - return ostd::get(tup).pop_front(); + if (!std::get(tup).empty()) { + return std::get(tup).pop_front(); } return JoinRangePop::pop(tup); } @@ -1509,8 +1519,8 @@ namespace detail { struct JoinRangeFront { template static T front(U const &tup) { - if (!ostd::get(tup).empty()) { - return ostd::get(tup).front(); + if (!std::get(tup).empty()) { + return std::get(tup).front(); } return JoinRangeFront::front(tup); } @@ -1520,7 +1530,7 @@ namespace detail { struct JoinRangeFront { template static T front(U const &tup) { - return ostd::get<0>(tup).front(); + return std::get<0>(tup).front(); } }; } @@ -1531,7 +1541,7 @@ struct JoinRange: InputRange, CommonType...>, CommonType...>, CommonType...>, CommonType...>> { private: - Tuple p_ranges; + std::tuple p_ranges; public: JoinRange() = delete; JoinRange(R const &...ranges): p_ranges(ranges...) {} @@ -1573,7 +1583,7 @@ public: namespace detail { template struct ZipValueType { - using Type = Tuple; + using Type = std::tuple; }; template @@ -1588,7 +1598,7 @@ namespace detail { struct ZipRangeEmpty { template static bool empty(T const &tup) { - if (ostd::get(tup).empty()) { + if (std::get(tup).empty()) { return true; } return ZipRangeEmpty::empty(tup); @@ -1608,7 +1618,7 @@ namespace detail { template static bool pop(T &tup) { return ( - ostd::get(tup).pop_front() && ZipRangePop::pop(tup) + std::get(tup).pop_front() && ZipRangePop::pop(tup) ); } }; @@ -1624,14 +1634,15 @@ namespace detail { template struct ZipRangeFront { template - static ZipValue tup_get(U const &tup, detail::TupleIndices) { - return ZipValue(ostd::get(tup).front()...); + static ZipValue tup_get(U const &tup, std::index_sequence) { + return ZipValue(std::get(tup).front()...); } template static ZipValue front(U const &tup) { - using Index = detail::MakeTupleIndices; - return ZipRangeFront::tup_get(tup, Index()); + return ZipRangeFront::tup_get( + tup, std::make_index_sequence() + ); } }; } @@ -1643,7 +1654,7 @@ struct ZipRange: InputRange, detail::ZipValue...>, CommonType...>, CommonType...>> { private: - Tuple p_ranges; + std::tuple p_ranges; public: ZipRange() = delete; ZipRange(R const &...ranges): p_ranges(ranges...) {} diff --git a/ostd/string.hh b/ostd/string.hh index 40dd7ac..ce9ebcb 100644 --- a/ostd/string.hh +++ b/ostd/string.hh @@ -1020,6 +1020,18 @@ struct ToString> { } }; +template +struct ToString> { + using Argument = std::tuple; + using Result = String; + String operator()(Argument const &v) { + String ret("{"); + detail::TupleToString<0, sizeof...(T)>::append(ret, v); + ret += "}"; + return ret; + } +}; + template typename ToString::Result to_string(T const &v) { return ToString>>()(v);