From a7532930e3ffbe1bd730d1a984a499b130aba069 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Thu, 7 Apr 2016 00:37:46 +0900 Subject: [PATCH] adapt rational tuple and container interface --- sprout/container/std/complex.hpp | 13 +++ sprout/rational.hpp | 2 + sprout/rational/container.hpp | 127 +++++++++++++++++++++++++++++ sprout/rational/tuple.hpp | 134 +++++++++++++++++++++++++++++++ testspr/header_all.hpp | 1 + 5 files changed, 277 insertions(+) create mode 100644 sprout/rational/container.hpp create mode 100644 sprout/rational/tuple.hpp diff --git a/sprout/container/std/complex.hpp b/sprout/container/std/complex.hpp index 88175906..01816451 100644 --- a/sprout/container/std/complex.hpp +++ b/sprout/container/std/complex.hpp @@ -109,6 +109,19 @@ namespace sprout { return copied_type(SPROUT_FORWARD(Args, args)...); } }; + + // + // container_transform_traits + // + template + struct container_transform_traits > { + public: + template + struct rebind_type { + public: + typedef std::complex type; + }; + }; } // namespace sprout #endif // #ifndef SPROUT_CONTAINER_STD_COMPLEX_HPP diff --git a/sprout/rational.hpp b/sprout/rational.hpp index a5ceb11f..e60e6622 100644 --- a/sprout/rational.hpp +++ b/sprout/rational.hpp @@ -14,6 +14,8 @@ #include #include #include +#include +#include #include #include #include diff --git a/sprout/rational/container.hpp b/sprout/rational/container.hpp new file mode 100644 index 00000000..9d2bd1b2 --- /dev/null +++ b/sprout/rational/container.hpp @@ -0,0 +1,127 @@ +/*============================================================================= + Copyright (c) 2011-2016 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_RATIONAL_CONTAINER_HPP +#define SPROUT_RATIONAL_CONTAINER_HPP + +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace detail { + struct rational_at { + public: + template + SPROUT_CONSTEXPR typename Rational::value_type + operator()(Rational const& c, Index i) const { + return i == 0 ? c.numerator() + : i == 1 ? c.denominator() + : throw std::out_of_range("rational_at: index out of range") + ; + } + }; + } // namespace detail + + // + // container_traits + // + template + struct container_traits > { + public: + typedef T value_type; + typedef sprout::index_iterator&, true, sprout::detail::rational_at> iterator; + typedef sprout::index_iterator const&, true, sprout::detail::rational_at> const_iterator; + typedef T reference; + typedef T const_reference; + typedef std::size_t size_type; + typedef std::ptrdiff_t difference_type; + typedef T* pointer; + typedef T const* const_pointer; + public: + SPROUT_STATIC_CONSTEXPR size_type static_size = 2; + public: + static SPROUT_CONSTEXPR size_type + fixed_size() SPROUT_NOEXCEPT { + return static_size; + } + }; + template + SPROUT_CONSTEXPR_OR_CONST typename sprout::container_traits >::size_type + sprout::container_traits >::static_size; + + // + // container_range_traits + // + template + struct container_range_traits > + : public sprout::detail::container_range_traits_default > + { + public: + static SPROUT_CONSTEXPR typename sprout::container_traits >::iterator + range_begin(sprout::rational& cont) { + return typename sprout::container_traits >::iterator(cont, 0); + } + static SPROUT_CONSTEXPR typename sprout::container_traits const>::iterator + range_begin(sprout::rational const& cont) { + return typename sprout::container_traits const>::iterator(cont, 0); + } + + static SPROUT_CONSTEXPR typename sprout::container_traits >::iterator + range_end(sprout::rational& cont) { + return typename sprout::container_traits >::iterator(cont, 2); + } + static SPROUT_CONSTEXPR typename sprout::container_traits const>::iterator + range_end(sprout::rational const& cont) { + return typename sprout::container_traits const>::iterator(cont, 2); + } + }; + + // + // container_construct_traits + // + template + struct container_construct_traits > { + public: + typedef sprout::rational copied_type; + public: + template + static SPROUT_CONSTEXPR copied_type + deep_copy(Cont&& cont) { + return SPROUT_FORWARD(Cont, cont); + } + template + static SPROUT_CONSTEXPR copied_type + make(Args&&... args) { + return copied_type(SPROUT_FORWARD(Args, args)...); + } + template + static SPROUT_CONSTEXPR copied_type + remake(Cont&&, typename sprout::container_traits >::difference_type, Args&&... args) { + return copied_type(SPROUT_FORWARD(Args, args)...); + } + }; + + // + // container_transform_traits + // + template + struct container_transform_traits > { + public: + template + struct rebind_type { + public: + typedef sprout::rational type; + }; + }; +} // namespace sprout + +#endif // #ifndef SPROUT_RATIONAL_CONTAINER_HPP diff --git a/sprout/rational/tuple.hpp b/sprout/rational/tuple.hpp new file mode 100644 index 00000000..c0966c1a --- /dev/null +++ b/sprout/rational/tuple.hpp @@ -0,0 +1,134 @@ +/*============================================================================= + Copyright (c) 2011-2016 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_RATIONAL_TUPLE_HPP +#define SPROUT_RATIONAL_TUPLE_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace tuples { + namespace detail { + template + struct tuple_element_impl; + template + struct tuple_element_impl > + : public sprout::detail::nil_base + {}; + template + struct tuple_element_impl<0, sprout::rational > + : public sprout::identity + {}; + template + struct tuple_element_impl<1, sprout::rational > + : public sprout::identity + {}; + + template + struct get_impl; + template + struct get_impl<0, sprout::rational > { + public: + SPROUT_CONSTEXPR T operator()(sprout::rational const& t) const { + return t.numerator(); + } + }; + template + struct get_impl<1, sprout::rational > { + public: + SPROUT_CONSTEXPR T operator()(sprout::rational const& t) const { + return t.denominator(); + } + }; + } // namespace detail + } // namespace tuples +} // namespace sprout + +namespace std { +#if defined(__clang__) +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wmismatched-tags" +#endif + // + // tuple_size + // + template + struct tuple_size > + : public sprout::integral_constant + {}; + + // + // tuple_element + // + template + struct tuple_element > + : public sprout::tuples::detail::tuple_element_impl > + {}; +#if defined(__clang__) +# pragma clang diagnostic pop +#endif +} // namespace std + +namespace sprout { + namespace tuples { + // + // tuple_traits + // + template + struct tuple_traits > { + public: + SPROUT_STATIC_CONSTEXPR std::size_t size = sprout::tuples::tuple_size >::value; + public: + template + struct element + : public sprout::tuples::tuple_element > + {}; + template + struct lvalue_reference + : public element + {}; + template + struct const_lvalue_reference + : public element + {}; + template + struct rvalue_reference + : public element + {}; + template + struct const_rvalue_reference + : public element + {}; + }; + template + SPROUT_CONSTEXPR_OR_CONST std::size_t sprout::tuples::tuple_traits >::size; + + // + // tuple_access_traits + // + template + struct tuple_access_traits > { + public: + template + static SPROUT_CONSTEXPR T + tuple_get(sprout::rational const& t) SPROUT_NOEXCEPT { + static_assert(I < 2, "tuple_get: index out of range"); + return sprout::tuples::detail::get_impl >()(t); + } + }; + } // namespace tuples +} // namespace sprout + +#endif // #ifndef SPROUT_RATIONAL_TUPLE_HPP diff --git a/testspr/header_all.hpp b/testspr/header_all.hpp index b2c9fd3f..219acf4e 100644 --- a/testspr/header_all.hpp +++ b/testspr/header_all.hpp @@ -9,6 +9,7 @@ #define TESTSPR_HEADER_ALL_HPP #include +#include #include #include #include