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

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

View file

@ -8,7 +8,8 @@
namespace sprout {
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());
}
} // namespace sprout

View file

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

View file

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