From bd307bd52ec402af4a265387467fc4a7f0c5a70c Mon Sep 17 00:00:00 2001 From: q66 Date: Tue, 14 Feb 2017 18:32:51 +0100 Subject: [PATCH] standard iter() for any std container type without ranged_traits --- examples/range_pipe.cc | 4 ++-- ostd/array.hh | 31 -------------------------- ostd/filesystem.hh | 1 - ostd/range.hh | 49 +++++++++++++++++++++++++++++++++++------- ostd/unordered_map.hh | 20 ----------------- ostd/vector.hh | 14 ------------ 6 files changed, 43 insertions(+), 76 deletions(-) delete mode 100644 ostd/array.hh diff --git a/examples/range_pipe.cc b/examples/range_pipe.cc index 34b4481..31a11eb 100644 --- a/examples/range_pipe.cc +++ b/examples/range_pipe.cc @@ -1,6 +1,6 @@ -#include +#include +#include -#include #include #include #include diff --git a/ostd/array.hh b/ostd/array.hh deleted file mode 100644 index f50a17f..0000000 --- a/ostd/array.hh +++ /dev/null @@ -1,31 +0,0 @@ -/* OctaSTD extensions for std::array. - * - * This file is part of OctaSTD. See COPYING.md for futher information. - */ - -#ifndef OSTD_ARRAY_HH -#define OSTD_ARRAY_HH - -#include - -#include "ostd/range.hh" - -namespace ostd { - -template -struct ranged_traits> { - static IteratorRange iter(std::array &v) { - return IteratorRange{v.data(), v.data() + N}; - } -}; - -template -struct ranged_traits const> { - static IteratorRange iter(std::array const &v) { - return IteratorRange{v.data(), v.data() + N}; - } -}; - -} /* namespace ostd */ - -#endif diff --git a/ostd/filesystem.hh b/ostd/filesystem.hh index 97863fe..9c2274a 100644 --- a/ostd/filesystem.hh +++ b/ostd/filesystem.hh @@ -23,7 +23,6 @@ #include "ostd/range.hh" #include "ostd/vector.hh" #include "ostd/string.hh" -#include "ostd/array.hh" #include "ostd/algorithm.hh" namespace ostd { diff --git a/ostd/range.hh b/ostd/range.hh index 9a1c423..8af04c9 100644 --- a/ostd/range.hh +++ b/ostd/range.hh @@ -815,9 +815,6 @@ inline auto zip(R1 &&r1, R &&...rr) { }; } -template -struct ranged_traits; - namespace detail { template static std::true_type test_direct_iter(decltype(std::declval().iter()) *); @@ -827,14 +824,20 @@ namespace detail { template constexpr bool direct_iter_test = decltype(test_direct_iter(0))::value; + + template + struct ranged_traits_core {}; + + template + struct ranged_traits_core>> { + static auto iter(C &r) -> decltype(r.iter()) { + return r.iter(); + } + }; } template -struct ranged_traits>> { - static auto iter(C &r) -> decltype(r.iter()) { - return r.iter(); - } -}; +struct ranged_traits: detail::ranged_traits_core {}; template inline auto iter(T &r) -> decltype(ranged_traits::iter(r)) { @@ -1842,6 +1845,36 @@ inline IteratorRange iter(T *a, size_t b) { return IteratorRange(a, a + b); } +/* iter on standard containers */ + +namespace detail { + template + static std::true_type test_std_iter( + decltype(std::begin(std::declval())) *, + decltype(std::end(std::declval())) * + ); + + template + static std::false_type test_std_iter(...); + + template + constexpr bool std_iter_test = decltype(test_std_iter(0, 0))::value; + + template + struct ranged_traits_core && !detail::direct_iter_test + >> { + using range_type = std::conditional_t< + std::is_const_v, + IteratorRange, + IteratorRange + >; + static range_type iter(C &r) { + return range_type{r.begin(), r.end()}; + } + }; +} + } /* namespace ostd */ #endif diff --git a/ostd/unordered_map.hh b/ostd/unordered_map.hh index 0531dc5..edbe046 100644 --- a/ostd/unordered_map.hh +++ b/ostd/unordered_map.hh @@ -13,26 +13,6 @@ namespace ostd { -template -struct ranged_traits> { - using Range = IteratorRange< - typename std::unordered_map::iterator - >; - static Range iter(std::unordered_map &v) { - return Range{v.begin(), v.end()}; - } -}; - -template -struct ranged_traits const> { - using Range = IteratorRange< - typename std::unordered_map::const_iterator - >; - static Range iter(std::unordered_map const &v) { - return Range{v.cbegin(), v.cend()}; - } -}; - namespace detail { template std::integral_constant< diff --git a/ostd/vector.hh b/ostd/vector.hh index 72857cb..f393ca1 100644 --- a/ostd/vector.hh +++ b/ostd/vector.hh @@ -14,20 +14,6 @@ namespace ostd { -template -struct ranged_traits> { - static IteratorRange iter(std::vector &v) { - return IteratorRange{v.data(), v.data() + v.size()}; - } -}; - -template -struct ranged_traits const> { - static IteratorRange iter(std::vector const &v) { - return IteratorRange{v.data(), v.data() + v.size()}; - } -}; - template, typename R> inline std::vector make_vector(R range, A const &alloc = A{}) { std::vector ret{alloc};