1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00

adapt rational tuple and container interface

This commit is contained in:
bolero-MURAKAMI 2016-04-07 00:37:46 +09:00
parent 35305c18fb
commit a7532930e3
5 changed files with 277 additions and 0 deletions

View file

@ -109,6 +109,19 @@ namespace sprout {
return copied_type(SPROUT_FORWARD(Args, args)...);
}
};
//
// container_transform_traits
//
template<typename T>
struct container_transform_traits<std::complex<T> > {
public:
template<typename Type>
struct rebind_type {
public:
typedef std::complex<Type> type;
};
};
} // namespace sprout
#endif // #ifndef SPROUT_CONTAINER_STD_COMPLEX_HPP

View file

@ -14,6 +14,8 @@
#include <sprout/rational/arithmetic.hpp>
#include <sprout/rational/io.hpp>
#include <sprout/rational/hash.hpp>
#include <sprout/rational/tuple.hpp>
#include <sprout/rational/container.hpp>
#include <sprout/rational/values.hpp>
#include <sprout/rational/conversion.hpp>
#include <sprout/rational/exceptions.hpp>

View file

@ -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 <stdexcept>
#include <type_traits>
#include <sprout/workaround/std/cstddef.hpp>
#include <sprout/rational/rational.hpp>
#include <sprout/utility/forward.hpp>
#include <sprout/container/traits.hpp>
#include <sprout/iterator/index_iterator.hpp>
namespace sprout {
namespace detail {
struct rational_at {
public:
template<typename Rational, typename Index>
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<typename T>
struct container_traits<sprout::rational<T> > {
public:
typedef T value_type;
typedef sprout::index_iterator<sprout::rational<T>&, true, sprout::detail::rational_at> iterator;
typedef sprout::index_iterator<sprout::rational<T> 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<typename T>
SPROUT_CONSTEXPR_OR_CONST typename sprout::container_traits<sprout::rational<T> >::size_type
sprout::container_traits<sprout::rational<T> >::static_size;
//
// container_range_traits
//
template<typename T>
struct container_range_traits<sprout::rational<T> >
: public sprout::detail::container_range_traits_default<sprout::rational<T> >
{
public:
static SPROUT_CONSTEXPR typename sprout::container_traits<sprout::rational<T> >::iterator
range_begin(sprout::rational<T>& cont) {
return typename sprout::container_traits<sprout::rational<T> >::iterator(cont, 0);
}
static SPROUT_CONSTEXPR typename sprout::container_traits<sprout::rational<T> const>::iterator
range_begin(sprout::rational<T> const& cont) {
return typename sprout::container_traits<sprout::rational<T> const>::iterator(cont, 0);
}
static SPROUT_CONSTEXPR typename sprout::container_traits<sprout::rational<T> >::iterator
range_end(sprout::rational<T>& cont) {
return typename sprout::container_traits<sprout::rational<T> >::iterator(cont, 2);
}
static SPROUT_CONSTEXPR typename sprout::container_traits<sprout::rational<T> const>::iterator
range_end(sprout::rational<T> const& cont) {
return typename sprout::container_traits<sprout::rational<T> const>::iterator(cont, 2);
}
};
//
// container_construct_traits
//
template<typename T>
struct container_construct_traits<sprout::rational<T> > {
public:
typedef sprout::rational<T> copied_type;
public:
template<typename Cont>
static SPROUT_CONSTEXPR copied_type
deep_copy(Cont&& cont) {
return SPROUT_FORWARD(Cont, cont);
}
template<typename... Args>
static SPROUT_CONSTEXPR copied_type
make(Args&&... args) {
return copied_type(SPROUT_FORWARD(Args, args)...);
}
template<typename Cont, typename... Args>
static SPROUT_CONSTEXPR copied_type
remake(Cont&&, typename sprout::container_traits<sprout::rational<T> >::difference_type, Args&&... args) {
return copied_type(SPROUT_FORWARD(Args, args)...);
}
};
//
// container_transform_traits
//
template<typename T>
struct container_transform_traits<sprout::rational<T> > {
public:
template<typename Type>
struct rebind_type {
public:
typedef sprout::rational<Type> type;
};
};
} // namespace sprout
#endif // #ifndef SPROUT_RATIONAL_CONTAINER_HPP

134
sprout/rational/tuple.hpp Normal file
View file

@ -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 <sprout/config.hpp>
#include <sprout/workaround/std/cstddef.hpp>
#include <sprout/rational/rational.hpp>
#include <sprout/tuple/tuple/tuple_traits.hpp>
#include <sprout/tuple/tuple/tuple_access_traits.hpp>
#include <sprout/tuple/tuple/get.hpp>
#include <sprout/type_traits/integral_constant.hpp>
#include <sprout/type_traits/identity.hpp>
#include <sprout/detail/nil_base.hpp>
namespace sprout {
namespace tuples {
namespace detail {
template<std::size_t I, typename T>
struct tuple_element_impl;
template<std::size_t I, typename T>
struct tuple_element_impl<I, sprout::rational<T> >
: public sprout::detail::nil_base
{};
template<typename T>
struct tuple_element_impl<0, sprout::rational<T> >
: public sprout::identity<T>
{};
template<typename T>
struct tuple_element_impl<1, sprout::rational<T> >
: public sprout::identity<T>
{};
template<std::size_t I, typename T>
struct get_impl;
template<typename T>
struct get_impl<0, sprout::rational<T> > {
public:
SPROUT_CONSTEXPR T operator()(sprout::rational<T> const& t) const {
return t.numerator();
}
};
template<typename T>
struct get_impl<1, sprout::rational<T> > {
public:
SPROUT_CONSTEXPR T operator()(sprout::rational<T> 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<typename T>
struct tuple_size<sprout::rational<T> >
: public sprout::integral_constant<std::size_t, 2>
{};
//
// tuple_element
//
template<std::size_t I, typename T>
struct tuple_element<I, sprout::rational<T> >
: public sprout::tuples::detail::tuple_element_impl<I, sprout::rational<T> >
{};
#if defined(__clang__)
# pragma clang diagnostic pop
#endif
} // namespace std
namespace sprout {
namespace tuples {
//
// tuple_traits
//
template<typename T>
struct tuple_traits<sprout::rational<T> > {
public:
SPROUT_STATIC_CONSTEXPR std::size_t size = sprout::tuples::tuple_size<sprout::rational<T> >::value;
public:
template<std::size_t I>
struct element
: public sprout::tuples::tuple_element<I, sprout::rational<T> >
{};
template<std::size_t I>
struct lvalue_reference
: public element<I>
{};
template<std::size_t I>
struct const_lvalue_reference
: public element<I>
{};
template<std::size_t I>
struct rvalue_reference
: public element<I>
{};
template<std::size_t I>
struct const_rvalue_reference
: public element<I>
{};
};
template<typename T>
SPROUT_CONSTEXPR_OR_CONST std::size_t sprout::tuples::tuple_traits<sprout::rational<T> >::size;
//
// tuple_access_traits
//
template<typename T>
struct tuple_access_traits<sprout::rational<T> > {
public:
template<std::size_t I>
static SPROUT_CONSTEXPR T
tuple_get(sprout::rational<T> const& t) SPROUT_NOEXCEPT {
static_assert(I < 2, "tuple_get: index out of range");
return sprout::tuples::detail::get_impl<I, sprout::rational<T> >()(t);
}
};
} // namespace tuples
} // namespace sprout
#endif // #ifndef SPROUT_RATIONAL_TUPLE_HPP

View file

@ -9,6 +9,7 @@
#define TESTSPR_HEADER_ALL_HPP
#include <sprout/adapt/std/array.hpp>
#include <sprout/adapt/std/complex.hpp>
#include <sprout/adapt/std/utility.hpp>
#include <sprout/adl/not_found.hpp>
#include <sprout/algorithm.hpp>