add array constexpr member-functions fill, assign

This commit is contained in:
bolero-MURAKAMI 2012-10-05 01:53:39 +09:00
parent d76d714816
commit a660478548
10 changed files with 110 additions and 70 deletions

View file

@ -90,17 +90,19 @@ namespace testspr {
} }
// assign // assign
TESTSPR_BOTH_ASSERT(testspr::equal(arr1.assign(-1), array<int, 10>{{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}));
{ {
auto arr = arr1; auto arr = arr1;
arr.assign(-1); arr.assign(-1);
TESTSPR_ASSERT(arr[0] == -1); TESTSPR_ASSERT(testspr::equal(arr, array<int, 10>{{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}));
} }
// fill // fill
TESTSPR_BOTH_ASSERT(testspr::equal(arr1.fill(-1), array<int, 10>{{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}));
{ {
auto arr = arr1; auto arr = arr1;
arr.fill(-1); arr.fill(-1);
TESTSPR_ASSERT(arr[0] == -1); TESTSPR_ASSERT(testspr::equal(arr, array<int, 10>{{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}));
} }
// swap // swap

View file

@ -17,6 +17,19 @@
#endif #endif
namespace sprout { namespace sprout {
namespace detail {
template<sprout::index_t Index, typename T>
inline SPROUT_CONSTEXPR T const&
array_dummy_get(T const& value) {
return value;
}
template<typename Array, typename T, sprout::index_t... Indexes>
inline SPROUT_CONSTEXPR Array
array_fill_impl(T const& value, sprout::index_tuple<Indexes...>) {
return Array{{array_dummy_get<Indexes>(value)...}};
}
} // namespace detail
// //
// array // array
// //
@ -47,6 +60,9 @@ namespace sprout {
void fill(const_reference value) { void fill(const_reference value) {
std::fill_n(begin(), size(), value); std::fill_n(begin(), size(), value);
} }
SPROUT_CONSTEXPR array fill(const_reference value) const {
return sprout::detail::array_fill_impl<array>(value, sprout::index_n<0, N>::make());
}
void swap(array<T, N>& other) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(std::swap(std::declval<T&>(), std::declval<T&>()))) { void swap(array<T, N>& other) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(std::swap(std::declval<T&>(), std::declval<T&>()))) {
std::swap_ranges(other.begin(), other.end(), begin()); std::swap_ranges(other.begin(), other.end(), begin());
} }
@ -176,6 +192,9 @@ namespace sprout {
void assign(const_reference value) { void assign(const_reference value) {
fill(value); fill(value);
} }
SPROUT_CONSTEXPR array assign(const_reference value) const {
return fill(value);
}
void rangecheck(size_type i) const { void rangecheck(size_type i) const {
if (i >= size()) { if (i >= size()) {
throw std::out_of_range("array<>: index out of range"); throw std::out_of_range("array<>: index out of range");
@ -189,17 +208,16 @@ namespace sprout {
// swap // swap
// //
template<typename T, std::size_t N> template<typename T, std::size_t N>
inline void swap(sprout::array<T, N>& lhs, sprout::array<T, N>& rhs) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs))) { inline void swap(sprout::array<T, N>& lhs, sprout::array<T, N>& rhs)
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs)))
{
lhs.swap(rhs); lhs.swap(rhs);
} }
namespace detail { namespace detail {
template<typename T, std::size_t N, sprout::index_t... Indexes> template<typename T, std::size_t N, sprout::index_t... Indexes>
inline SPROUT_CONSTEXPR sprout::array<T, N> to_array_impl( inline SPROUT_CONSTEXPR sprout::array<T, N>
T const (& arr)[N], to_array_impl(T const (& arr)[N], sprout::index_tuple<Indexes...>) {
sprout::index_tuple<Indexes...>
)
{
return sprout::array<T, N>{{arr[Indexes]...}}; return sprout::array<T, N>{{arr[Indexes]...}};
} }
} // namespace detail } // namespace detail
@ -207,7 +225,8 @@ namespace sprout {
// to_array // to_array
// //
template<typename T, std::size_t N> template<typename T, std::size_t N>
inline SPROUT_CONSTEXPR sprout::array<T, N> to_array(T const (& arr)[N]) { inline SPROUT_CONSTEXPR sprout::array<T, N>
to_array(T const (& arr)[N]) {
return sprout::detail::to_array_impl(arr, sprout::index_range<0, N>::make()); return sprout::detail::to_array_impl(arr, sprout::index_range<0, N>::make());
} }
} // namespace sprout } // namespace sprout

View file

@ -8,7 +8,8 @@
namespace sprout { namespace sprout {
template<typename T, std::size_t N> template<typename T, std::size_t N>
inline SPROUT_CONSTEXPR std::size_t hash_value(sprout::array<T, N> const& v) { inline SPROUT_CONSTEXPR std::size_t
hash_value(sprout::array<T, N> const& v) {
return sprout::hash_range(v.begin(), v.end()); return sprout::hash_range(v.begin(), v.end());
} }
} // namespace sprout } // namespace sprout

View file

@ -13,7 +13,8 @@ namespace sprout {
// make_array // make_array
// //
template<typename T, typename... Types> template<typename T, typename... Types>
inline SPROUT_CONSTEXPR sprout::array<T, sizeof...(Types)> make_array(Types&&... args) { inline SPROUT_CONSTEXPR sprout::array<T, sizeof...(Types)>
make_array(Types&&... args) {
return sprout::array<T, sizeof...(Types)>{{sprout::forward<Types>(args)...}}; return sprout::array<T, sizeof...(Types)>{{sprout::forward<Types>(args)...}};
} }
@ -24,11 +25,13 @@ namespace sprout {
inline SPROUT_CONSTEXPR sprout::array< inline SPROUT_CONSTEXPR sprout::array<
typename std::decay<typename std::common_type<typename std::decay<Types>::type...>::type>::type, typename std::decay<typename std::common_type<typename std::decay<Types>::type...>::type>::type,
sizeof...(Types) sizeof...(Types)
> make_common_array(Types&&... args) { >
return sprout::array< make_common_array(Types&&... args) {
typedef sprout::array<
typename std::decay<typename std::common_type<typename std::decay<Types>::type...>::type>::type, typename std::decay<typename std::common_type<typename std::decay<Types>::type...>::type>::type,
sizeof...(Types) sizeof...(Types)
>{{sprout::forward<Types>(args)...}}; > type;
return type{{sprout::forward<Types>(args)...}};
} }
} // namespace sprout } // namespace sprout

View file

@ -4,11 +4,12 @@
#include <cstddef> #include <cstddef>
#include <tuple> #include <tuple>
#include <type_traits> #include <type_traits>
#include <sprout/config.hpp>
#include <sprout/array/array.hpp> #include <sprout/array/array.hpp>
#include <sprout/utility/move.hpp> #include <sprout/utility/move.hpp>
#include <sprout/tuple/tuple/get.hpp> #include <sprout/tuple/tuple/get.hpp>
namespace sprout { namespace sprout_adl {
// //
// tuple_get // tuple_get
// //
@ -29,7 +30,7 @@ namespace sprout {
tuple_get(sprout::array<T, N>&& t) SPROUT_NOEXCEPT { tuple_get(sprout::array<T, N>&& t) SPROUT_NOEXCEPT {
return sprout::move(sprout::tuples::get<I>(t)); return sprout::move(sprout::tuples::get<I>(t));
} }
} // namespace sprout } // namespace sprout_adl
namespace std { namespace std {
// //

View file

@ -7,6 +7,7 @@
// //
// SPROUT_HAS_XXX_TYPE_DEF // SPROUT_HAS_XXX_TYPE_DEF
// SPROUT_HAS_XXX_TYPE_DEF_LAZY
// //
#define SPROUT_HAS_XXX_TYPE_DEF(NAME, TYPE) \ #define SPROUT_HAS_XXX_TYPE_DEF(NAME, TYPE) \
template<typename T, typename = typename T::TYPE> \ template<typename T, typename = typename T::TYPE> \
@ -17,15 +18,12 @@
struct NAME \ struct NAME \
: decltype(SPROUT_PP_CAT(SPROUT_PP_CAT(sprout_has_xxx_impl_check_type_, TYPE), __LINE__)<T>(0)) \ : decltype(SPROUT_PP_CAT(SPROUT_PP_CAT(sprout_has_xxx_impl_check_type_, TYPE), __LINE__)<T>(0)) \
{} {}
//
// SPROUT_HAS_XXX_TYPE_DEF_LAZY
//
#define SPROUT_HAS_XXX_TYPE_DEF_LAZY(TYPE) \ #define SPROUT_HAS_XXX_TYPE_DEF_LAZY(TYPE) \
SPROUT_HAS_XXX_TYPE_DEF(SPROUT_PP_CAT(has_, TYPE), TYPE) SPROUT_HAS_XXX_TYPE_DEF(SPROUT_PP_CAT(has_, TYPE), TYPE)
// //
// SPROUT_HAS_XXX_VALUE_DEF // SPROUT_HAS_XXX_VALUE_DEF
// SPROUT_HAS_XXX_VALUE_DEF_LAZY
// //
#define SPROUT_HAS_XXX_VALUE_DEF(NAME, VALUE) \ #define SPROUT_HAS_XXX_VALUE_DEF(NAME, VALUE) \
template<typename T, decltype(&T::VALUE) = &T::VALUE> \ template<typename T, decltype(&T::VALUE) = &T::VALUE> \
@ -36,10 +34,6 @@
struct NAME \ struct NAME \
: decltype(SPROUT_PP_CAT(SPROUT_PP_CAT(sprout_has_xxx_impl_check_value_, VALUE), __LINE__)<T>(0)) \ : decltype(SPROUT_PP_CAT(SPROUT_PP_CAT(sprout_has_xxx_impl_check_value_, VALUE), __LINE__)<T>(0)) \
{} {}
//
// SPROUT_HAS_XXX_VALUE_DEF_LAZY
//
#define SPROUT_HAS_XXX_VALUE_DEF_LAZY(VALUE) \ #define SPROUT_HAS_XXX_VALUE_DEF_LAZY(VALUE) \
SPROUT_HAS_XXX_VALUE_DEF(SPROUT_PP_CAT(has_, VALUE), VALUE) SPROUT_HAS_XXX_VALUE_DEF(SPROUT_PP_CAT(has_, VALUE), VALUE)

View file

@ -3,6 +3,7 @@
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/variant/variant.hpp> #include <sprout/variant/variant.hpp>
#include <sprout/variant/tuple.hpp>
#include <sprout/variant/get.hpp> #include <sprout/variant/get.hpp>
#include <sprout/variant/static_visitor.hpp> #include <sprout/variant/static_visitor.hpp>
#include <sprout/variant/apply_visitor.hpp> #include <sprout/variant/apply_visitor.hpp>

View file

@ -11,35 +11,15 @@ namespace sprout {
// get // get
// //
template<typename U, typename... Types> template<typename U, typename... Types>
inline SPROUT_CONSTEXPR U const& get(sprout::variant<Types...> const& operand) { inline SPROUT_CONSTEXPR U const&
get(sprout::variant<Types...> const& operand) {
return operand.template get<U>(); return operand.template get<U>();
} }
template<typename U, typename... Types> template<typename U, typename... Types>
inline SPROUT_CONSTEXPR U& get(sprout::variant<Types...>& operand) { inline SPROUT_CONSTEXPR U&
get(sprout::variant<Types...>& operand) {
return operand.template get<U>(); return operand.template get<U>();
} }
} // namespace sprout } // namespace sprout
namespace sprout_adl {
//
// tuple_get
//
template<std::size_t I, typename... Types>
inline SPROUT_CONSTEXPR typename sprout::tuples::tuple_element<
I,
sprout::variant<Types...>
>::type const&
tuple_get(sprout::variant<Types...> const& operand) {
return operand.template get_at<I>();
}
template<std::size_t I, typename... Types>
inline SPROUT_CONSTEXPR typename sprout::tuples::tuple_element<
I,
sprout::variant<Types...>
>::type&
tuple_get(sprout::variant<Types...>& operand) {
return operand.template get_at<I>();
}
} // namespace sprout_adl
#endif // #ifndef SPROUT_VARIANT_GET_HPP #endif // #ifndef SPROUT_VARIANT_GET_HPP

59
sprout/variant/tuple.hpp Normal file
View file

@ -0,0 +1,59 @@
#ifndef SPROUT_VARIANT_TUPLE_HPP
#define SPROUT_VARIANT_TUPLE_HPP
#include <cstddef>
#include <tuple>
#include <sprout/config.hpp>
#include <sprout/variant/variant.hpp>
#include <sprout/utility/move.hpp>
#include <sprout/tuple/tuple/get.hpp>
namespace sprout_adl {
//
// tuple_get
//
template<std::size_t I, typename... Types>
inline SPROUT_CONSTEXPR typename sprout::tuples::tuple_element<
I,
sprout::variant<Types...>
>::type const&
tuple_get(sprout::variant<Types...> const& t) {
return t.template get_at<I>();
}
template<std::size_t I, typename... Types>
inline SPROUT_CONSTEXPR typename sprout::tuples::tuple_element<
I,
sprout::variant<Types...>
>::type&
tuple_get(sprout::variant<Types...>& t) {
return t.template get_at<I>();
}
template<std::size_t I, typename... Types>
inline SPROUT_CONSTEXPR typename sprout::tuples::tuple_element<
I,
sprout::variant<Types...>
>::type&&
tuple_get(sprout::variant<Types...>&& t) {
return sprout::move(sprout::tuples::get<I>(t));
}
} // namespace sprout_adl
namespace std {
//
// tuple_size
//
template<typename... Types>
struct tuple_size<sprout::variant<Types...> >
: public std::tuple_size<typename sprout::variant<Types...>::tuple_type>
{};
//
// tuple_element
//
template<std::size_t I, typename... Types>
struct tuple_element<I, sprout::variant<Types...> >
: public std::tuple_element<I, typename sprout::variant<Types...>::tuple_type>
{};
} // namespace std
#endif // #ifndef SPROUT_VARIANT_TUPLE_HPP

View file

@ -252,31 +252,11 @@ namespace sprout {
// swap // swap
// //
template<typename... Types> template<typename... Types>
inline void swap( inline void swap(sprout::variant<Types...>& lhs, sprout::variant<Types...>& rhs)
sprout::variant<Types...>& lhs, SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs)))
sprout::variant<Types...>& rhs
) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs)))
{ {
lhs.swap(rhs); lhs.swap(rhs);
} }
} // namespace sprout } // namespace sprout
namespace std {
//
// tuple_size
//
template<typename... Types>
struct tuple_size<sprout::variant<Types...> >
: public std::tuple_size<typename sprout::variant<Types...>::tuple_type>
{};
//
// tuple_element
//
template<std::size_t I, typename... Types>
struct tuple_element<I, sprout::variant<Types...> >
: public std::tuple_element<I, typename sprout::variant<Types...>::tuple_type>
{};
} // namespace std
#endif // #ifndef SPROUT_VARIANT_VARIANT_HPP #endif // #ifndef SPROUT_VARIANT_VARIANT_HPP