From c3636c10d28d072f8bfdbd0ced7bc2e1c3452234 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Mon, 23 Mar 2015 00:26:20 +0900 Subject: [PATCH] add non-member function nth(), index_of() --- sprout/assert.hpp | 4 +- sprout/container/container_range_traits.hpp | 194 +++++++++++++++++++- sprout/container/index_of.hpp | 87 +++++++++ sprout/container/nth.hpp | 87 +++++++++ sprout/container/range_functions.hpp | 2 + sprout/container/range_functions_fwd.hpp | 20 ++ testspr/assert.hpp | 2 +- 7 files changed, 390 insertions(+), 6 deletions(-) create mode 100644 sprout/container/index_of.hpp create mode 100644 sprout/container/nth.hpp diff --git a/sprout/assert.hpp b/sprout/assert.hpp index 795d3b21..99b9d937 100644 --- a/sprout/assert.hpp +++ b/sprout/assert.hpp @@ -26,9 +26,7 @@ # include # include #endif -#if !(defined(SPROUT_DISABLE_ASSERTS) || defined(NDEBUG)) -# include -#endif +#include // // SPROUT_ASSERTION_FAILED_FORMAT diff --git a/sprout/container/container_range_traits.hpp b/sprout/container/container_range_traits.hpp index 0b6b3c67..c1bda2df 100644 --- a/sprout/container/container_range_traits.hpp +++ b/sprout/container/container_range_traits.hpp @@ -337,6 +337,150 @@ namespace sprout { } }; + template + struct has_mem_nth_test { + public: + template< + typename U = T, + typename = typename sprout::identity().nth(std::declval::size_type>()))>::type + > + static sprout::true_type test(int); + static sprout::false_type test(...); + }; +#if defined(_MSC_VER) + template::test(0))>::type> + struct has_mem_nth + : public Base_ + {}; +#else + template + struct has_mem_nth + : public sprout::identity::test(0))>::type + {}; +#endif + + template + struct container_range_traits_range_nth_impl; + template + struct container_range_traits_range_nth_impl< + Container, + typename std::enable_if::value>::type + > { + public: + static SPROUT_CONSTEXPR typename sprout::container_traits::iterator + range_nth(Container& cont, typename sprout::container_traits::size_type i) { + return cont.nth(i); + } + }; + template + struct container_range_traits_range_nth_impl< + Container, + typename std::enable_if::value>::type + > { + public: + static SPROUT_CONSTEXPR typename sprout::container_traits::iterator + range_nth(Container& cont, typename sprout::container_traits::size_type i) { + return sprout::next(sprout::begin(cont), i); + } + }; + + template + struct container_range_traits_range_nth_const_impl; + template + struct container_range_traits_range_nth_const_impl< + Container, + typename std::enable_if::value>::type + > { + public: + static SPROUT_CONSTEXPR typename sprout::container_traits::iterator + range_nth(Container const& cont, typename sprout::container_traits::size_type i) { + return cont.nth(i); + } + }; + template + struct container_range_traits_range_nth_const_impl< + Container, + typename std::enable_if::value>::type + > { + public: + static SPROUT_CONSTEXPR typename sprout::container_traits::iterator + range_nth(Container const& cont, typename sprout::container_traits::size_type i) { + return sprout::next(sprout::begin(cont), i); + } + }; + + template + struct has_mem_index_of_test { + public: + template< + typename U = T, + typename = typename sprout::identity().index_of(std::declval::iterator>()))>::type + > + static sprout::true_type test(int); + static sprout::false_type test(...); + }; +#if defined(_MSC_VER) + template::test(0))>::type> + struct has_mem_index_of + : public Base_ + {}; +#else + template + struct has_mem_index_of + : public sprout::identity::test(0))>::type + {}; +#endif + + template + struct container_range_traits_range_index_of_impl; + template + struct container_range_traits_range_index_of_impl< + Container, + typename std::enable_if::value>::type + > { + public: + static SPROUT_CONSTEXPR typename sprout::container_traits::size_type + range_index_of(Container& cont, typename sprout::container_traits::iterator p) { + return cont.index_of(p); + } + }; + template + struct container_range_traits_range_index_of_impl< + Container, + typename std::enable_if::value>::type + > { + public: + static SPROUT_CONSTEXPR typename sprout::container_traits::size_type + range_index_of(Container& cont, typename sprout::container_traits::iterator p) { + return sprout::distance(sprout::begin(cont), p); + } + }; + + template + struct container_range_traits_range_index_of_const_impl; + template + struct container_range_traits_range_index_of_const_impl< + Container, + typename std::enable_if::value>::type + > { + public: + static SPROUT_CONSTEXPR typename sprout::container_traits::size_type + range_index_of(Container const& cont, typename sprout::container_traits::iterator p) { + return cont.index_of(p); + } + }; + template + struct container_range_traits_range_index_of_const_impl< + Container, + typename std::enable_if::value>::type + > { + public: + static SPROUT_CONSTEXPR typename sprout::container_traits::size_type + range_index_of(Container const& cont, typename sprout::container_traits::iterator p) { + return sprout::distance(sprout::begin(cont), p); + } + }; + // // container_range_traits_default // @@ -350,6 +494,10 @@ namespace sprout { , public sprout::detail::container_range_traits_range_back_const_impl , public sprout::detail::container_range_traits_range_at_impl , public sprout::detail::container_range_traits_range_at_const_impl + , public sprout::detail::container_range_traits_range_nth_impl + , public sprout::detail::container_range_traits_range_nth_const_impl + , public sprout::detail::container_range_traits_range_index_of_impl + , public sprout::detail::container_range_traits_range_index_of_const_impl { using sprout::detail::container_range_traits_range_front_impl::range_front; using sprout::detail::container_range_traits_range_front_const_impl::range_front; @@ -357,6 +505,10 @@ namespace sprout { using sprout::detail::container_range_traits_range_back_const_impl::range_back; using sprout::detail::container_range_traits_range_at_impl::range_at; using sprout::detail::container_range_traits_range_at_const_impl::range_at; + using sprout::detail::container_range_traits_range_nth_impl::range_nth; + using sprout::detail::container_range_traits_range_nth_const_impl::range_nth; + using sprout::detail::container_range_traits_range_index_of_impl::range_index_of; + using sprout::detail::container_range_traits_range_index_of_const_impl::range_index_of; public: // iterators: static SPROUT_CONSTEXPR typename sprout::container_traits::iterator @@ -422,6 +574,14 @@ namespace sprout { range_at(Container const& cont, typename sprout::container_traits::size_type i) { return sprout::container_range_traits::range_at(cont, i); } + static SPROUT_CONSTEXPR typename sprout::container_traits::iterator + range_nth(Container const& cont, typename sprout::container_traits::size_type i) { + return sprout::container_range_traits::range_nth(cont, i); + } + static SPROUT_CONSTEXPR typename sprout::container_traits::size_type + range_index_of(Container const& cont, typename sprout::container_traits::iterator p) { + return sprout::container_range_traits::range_index_of(cont, p); + } // data access: static SPROUT_CONSTEXPR typename sprout::container_traits::pointer range_data(Container const& cont) { @@ -500,14 +660,36 @@ namespace sprout { range_at(T const (& arr)[N], typename sprout::container_traits::size_type i) { return arr[i]; } + static SPROUT_CONSTEXPR typename sprout::container_traits::iterator + range_nth(T (& arr)[N], typename sprout::container_traits::size_type i) { + typedef typename sprout::container_traits::iterator type; + return type(arr) + i; + } + static SPROUT_CONSTEXPR typename sprout::container_traits::iterator + range_nth(T const (& arr)[N], typename sprout::container_traits::size_type i) { + typedef typename sprout::container_traits::iterator type; + return type(arr) + i; + } + static SPROUT_CONSTEXPR typename sprout::container_traits::size_type + range_index_of(T (& arr)[N], typename sprout::container_traits::iterator p) { + typedef typename sprout::container_traits::iterator type; + return sprout::distance(type(arr), p); + } + static SPROUT_CONSTEXPR typename sprout::container_traits::size_type + range_index_of(T const (& arr)[N], typename sprout::container_traits::iterator p) { + typedef typename sprout::container_traits::iterator type; + return sprout::distance(type(arr), p); + } // data access: static SPROUT_CONSTEXPR typename sprout::container_traits::pointer range_data(T (& arr)[N]) { - return &arr[0]; + typedef typename sprout::container_traits::pointer type; + return type(arr); } static SPROUT_CONSTEXPR typename sprout::container_traits::pointer range_data(T const (& arr)[N]) { - return &arr[0]; + typedef typename sprout::container_traits::pointer type; + return type(arr); } }; template @@ -544,6 +726,14 @@ namespace sprout { range_at(T const (& arr)[N], typename sprout::container_traits::size_type i) { return sprout::container_range_traits::range_at(arr, i); } + static SPROUT_CONSTEXPR typename sprout::container_traits::iterator + range_nth(T const (& arr)[N], typename sprout::container_traits::size_type i) { + return sprout::container_range_traits::range_nth(arr, i); + } + static SPROUT_CONSTEXPR typename sprout::container_traits::size_type + range_index_of(T const (& arr)[N], typename sprout::container_traits::iterator p) { + return sprout::container_range_traits::range_index_of(arr, p); + } // data access: static SPROUT_CONSTEXPR typename sprout::container_traits::pointer range_data(T const (& arr)[N]) { diff --git a/sprout/container/index_of.hpp b/sprout/container/index_of.hpp new file mode 100644 index 00000000..4de98676 --- /dev/null +++ b/sprout/container/index_of.hpp @@ -0,0 +1,87 @@ +/*============================================================================= + Copyright (c) 2011-2015 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_CONTAINER_INDEX_OF_HPP +#define SPROUT_CONTAINER_INDEX_OF_HPP + +#include +#include +#include +#include +#include + +namespace sprout_adl { + sprout::not_found_via_adl range_index_of(...); +} // namespace sprout_adl + +namespace sprout { + namespace container_detail { + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::size_type + range_index_of(Container& cont, typename sprout::container_traits::iterator p) { + return sprout::container_range_traits::range_index_of(cont, p); + } + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::size_type + range_index_of(Container const& cont, typename sprout::container_traits::iterator p) { + return sprout::container_range_traits::range_index_of(cont, p); + } + } // namespace container_detail + + // + // index_of + // + // effect: + // ADL callable range_index_of(cont, p) -> range_index_of(cont, p) + // otherwise -> sprout::container_range_traits::range_index_of(cont, p) + // [default] + // callable cont.index_of(p) -> cont.index_of(p) + // otherwise -> *next(begin(cont), p) + // + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::size_type + index_of(Container& cont, typename sprout::container_traits::iterator p) { + using sprout::container_detail::range_index_of; + using sprout_adl::range_index_of; + return range_index_of(cont, p); + } + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::size_type + index_of(Container const& cont, typename sprout::container_traits::iterator p) { + using sprout::container_detail::range_index_of; + using sprout_adl::range_index_of; + return range_index_of(cont, p); + } + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::size_type + index_of(T (& arr)[N], typename sprout::container_traits::iterator p) { + return sprout::container_detail::range_index_of(arr, p); + } + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::size_type + index_of(T const (& arr)[N], typename sprout::container_traits::iterator p) { + return sprout::container_detail::range_index_of(arr, p); + } + + // + // cindex_of + // + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::size_type + cindex_of(Container const& cont, typename sprout::container_traits::iterator p) { + return sprout::index_of(cont, p); + } + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::size_type + cindex_of(T const (& arr)[N], typename sprout::container_traits::iterator p) { + return sprout::index_of(arr, p); + } +} // namespace sprout + +#include + +#endif // #ifndef SPROUT_CONTAINER_INDEX_OF_HPP diff --git a/sprout/container/nth.hpp b/sprout/container/nth.hpp new file mode 100644 index 00000000..e4ff234f --- /dev/null +++ b/sprout/container/nth.hpp @@ -0,0 +1,87 @@ +/*============================================================================= + Copyright (c) 2011-2015 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_CONTAINER_NTH_HPP +#define SPROUT_CONTAINER_NTH_HPP + +#include +#include +#include +#include +#include + +namespace sprout_adl { + sprout::not_found_via_adl range_nth(...); +} // namespace sprout_adl + +namespace sprout { + namespace container_detail { + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::iterator + range_nth(Container& cont, typename sprout::container_traits::size_type i) { + return sprout::container_range_traits::range_nth(cont, i); + } + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::iterator + range_nth(Container const& cont, typename sprout::container_traits::size_type i) { + return sprout::container_range_traits::range_nth(cont, i); + } + } // namespace container_detail + + // + // nth + // + // effect: + // ADL callable range_nth(cont, i) -> range_nth(cont, i) + // otherwise -> sprout::container_range_traits::range_nth(cont, i) + // [default] + // callable cont.nth(i) -> cont.nth(i) + // otherwise -> *next(begin(cont), i) + // + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::iterator + nth(Container& cont, typename sprout::container_traits::size_type i) { + using sprout::container_detail::range_nth; + using sprout_adl::range_nth; + return range_nth(cont, i); + } + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::iterator + nth(Container const& cont, typename sprout::container_traits::size_type i) { + using sprout::container_detail::range_nth; + using sprout_adl::range_nth; + return range_nth(cont, i); + } + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::iterator + nth(T (& arr)[N], typename sprout::container_traits::size_type i) { + return sprout::container_detail::range_nth(arr, i); + } + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::iterator + nth(T const (& arr)[N], typename sprout::container_traits::size_type i) { + return sprout::container_detail::range_nth(arr, i); + } + + // + // cnth + // + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::iterator + cnth(Container const& cont, typename sprout::container_traits::size_type i) { + return sprout::nth(cont, i); + } + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::iterator + cnth(T const (& arr)[N], typename sprout::container_traits::size_type i) { + return sprout::nth(arr, i); + } +} // namespace sprout + +#include + +#endif // #ifndef SPROUT_CONTAINER_NTH_HPP diff --git a/sprout/container/range_functions.hpp b/sprout/container/range_functions.hpp index 2dbd0f44..ee95f2fe 100644 --- a/sprout/container/range_functions.hpp +++ b/sprout/container/range_functions.hpp @@ -17,6 +17,8 @@ #include #include #include +#include +#include #include #endif // #ifndef SPROUT_CONTAINER_RANGE_FUNCTIONS_HPP diff --git a/sprout/container/range_functions_fwd.hpp b/sprout/container/range_functions_fwd.hpp index 37444477..3da3f93e 100644 --- a/sprout/container/range_functions_fwd.hpp +++ b/sprout/container/range_functions_fwd.hpp @@ -76,6 +76,26 @@ namespace sprout { SPROUT_CONSTEXPR typename sprout::container_traits::reference at(Container const& cont, typename sprout::container_traits::size_type i); + // + // nth + // + template + SPROUT_CONSTEXPR typename sprout::container_traits::iterator + nth(Container& cont, typename sprout::container_traits::size_type i); + template + SPROUT_CONSTEXPR typename sprout::container_traits::iterator + nth(Container const& cont, typename sprout::container_traits::size_type i); + + // + // index_of + // + template + SPROUT_CONSTEXPR typename sprout::container_traits::size_type + index_of(Container& cont, typename sprout::container_traits::iterator p); + template + SPROUT_CONSTEXPR typename sprout::container_traits::size_type + index_of(Container const& cont, typename sprout::container_traits::iterator p); + // // data // diff --git a/testspr/assert.hpp b/testspr/assert.hpp index 11a12ac2..57f6d38e 100644 --- a/testspr/assert.hpp +++ b/testspr/assert.hpp @@ -54,7 +54,7 @@ namespace testspr { #define TESTSPR_STATIC_ASSERT(expr) \ static_assert(expr, #expr) #define TESTSPR_ASSERT(expr) \ - ((void)sprout::detail::assertion_check((expr), SPROUT_ASSERTION_FAILED_FORMAT(expr, __FILE__, __LINE__))) + SPROUT_ASSERT(expr) // // TESTSPR_BOTH_ASSERT //