rewrite string construction (performance improved)

This commit is contained in:
bolero-MURAKAMI 2012-04-18 00:58:34 +09:00
parent ece20bf8ef
commit 165b9ee1cd
6 changed files with 48 additions and 68 deletions

View file

@ -8,7 +8,7 @@
namespace sprout { namespace sprout {
// //
// strtol // strtoimax
// //
inline SPROUT_CONSTEXPR std::intmax_t strtoimax(char const* str, char** endptr, int base = 10){ inline SPROUT_CONSTEXPR std::intmax_t strtoimax(char const* str, char** endptr, int base = 10){
return sprout::str_to_int<std::intmax_t>(str, endptr, base); return sprout::str_to_int<std::intmax_t>(str, endptr, base);

View file

@ -8,7 +8,7 @@
namespace sprout { namespace sprout {
// //
// strtoul // strtoumax
// //
inline SPROUT_CONSTEXPR std::uintmax_t strtoumax(char const* str, char** endptr, int base = 10){ inline SPROUT_CONSTEXPR std::uintmax_t strtoumax(char const* str, char** endptr, int base = 10){
return sprout::str_to_int<std::uintmax_t>(str, endptr, base); return sprout::str_to_int<std::uintmax_t>(str, endptr, base);

View file

@ -23,7 +23,7 @@ namespace sprout {
template<typename... Args> template<typename... Args>
static SPROUT_CONSTEXPR copied_type make(Args&&... args) { static SPROUT_CONSTEXPR copied_type make(Args&&... args) {
typedef sprout::detail::make_construct_impl<copied_type> impl_type; typedef sprout::detail::make_construct_impl<copied_type> impl_type;
return impl_type::make(impl_type::length(sprout::forward<Args>(args)...), sprout::forward<Args>(args)...); return impl_type::make(sprout::forward<Args>(args)...);
} }
template<typename Cont, typename... Args> template<typename Cont, typename... Args>
static SPROUT_CONSTEXPR copied_type remake( static SPROUT_CONSTEXPR copied_type remake(
@ -33,7 +33,7 @@ namespace sprout {
) )
{ {
typedef sprout::detail::make_construct_impl<copied_type> impl_type; typedef sprout::detail::make_construct_impl<copied_type> impl_type;
return impl_type::make(size, sprout::forward<Args>(args)...); return impl_type::make(static_cast<typename copied_type::size_type>(size), sprout::forward<Args>(args)...);
} }
}; };

View file

@ -4,8 +4,6 @@
#include <cstddef> #include <cstddef>
#include <type_traits> #include <type_traits>
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/index_tuple.hpp>
#include <sprout/array.hpp>
#include <sprout/string/char_traits.hpp> #include <sprout/string/char_traits.hpp>
#include <sprout/string/string.hpp> #include <sprout/string/string.hpp>
#include <sprout/utility/forward.hpp> #include <sprout/utility/forward.hpp>
@ -14,40 +12,13 @@ namespace sprout {
// //
// make_string // make_string
// //
namespace detail {
template<typename T, std::size_t N, sprout::index_t... Indexes>
inline SPROUT_CONSTEXPR sprout::basic_string<T, N - 1> make_string_impl_1(
sprout::array<T, N> const& arr,
std::size_t n,
sprout::index_tuple<Indexes...>
)
{
return sprout::basic_string<T, N - 1>{{(Indexes < n ? arr[Indexes] : T())...}, n};
}
template<typename T, std::size_t N, sprout::index_t... Indexes>
inline SPROUT_CONSTEXPR sprout::basic_string<T, N - 1> make_string_impl(
sprout::array<T, N> const& arr,
sprout::index_tuple<Indexes...>
)
{
return sprout::detail::make_string_impl_1(
arr,
sprout::char_traits<T>::length(arr.begin()),
sprout::index_tuple<Indexes...>()
);
}
} // namespace detail
template<typename T, typename... Types> template<typename T, typename... Types>
inline SPROUT_CONSTEXPR sprout::basic_string<typename std::decay<T>::type, 1 + sizeof...(Types)> inline SPROUT_CONSTEXPR sprout::basic_string<typename std::decay<T>::type, 1 + sizeof...(Types)>
make_string(T&& t, Types&&... args) { make_string(T&& t, Types&&... args) {
return sprout::detail::make_string_impl( typedef sprout::detail::make_construct_impl<
sprout::make_array<typename std::decay<T>::type>( sprout::basic_string<typename std::decay<T>::type, 1 + sizeof...(Types)>
sprout::forward<T>(t), > impl_type;
sprout::forward<Types>(args)..., return impl_type::make(sprout::forward<T>(t), sprout::forward<Types>(args)...);
typename std::decay<T>::type()
),
typename sprout::index_range<0, 1 + sizeof...(Types)>::type()
);
} }
// //
@ -61,13 +32,10 @@ namespace sprout {
template<typename T, typename... Types> template<typename T, typename... Types>
inline SPROUT_CONSTEXPR sprout::basic_string<typename std::decay<T>::type, sizeof...(Types)> inline SPROUT_CONSTEXPR sprout::basic_string<typename std::decay<T>::type, sizeof...(Types)>
make_string_as(Types&&... args) { make_string_as(Types&&... args) {
return sprout::detail::make_string_impl( typedef sprout::detail::make_construct_impl<
sprout::make_array<typename std::decay<T>::type>( sprout::basic_string<typename std::decay<T>::type, sizeof...(Types)>
sprout::forward<Types>(args)..., > impl_type;
typename std::decay<T>::type() return impl_type::make(sprout::forward<Types>(args)...);
),
typename sprout::index_range<0, 1 + sizeof...(Types)>::type()
);
} }
} // namespace sprout } // namespace sprout

View file

@ -8,10 +8,12 @@
#include <type_traits> #include <type_traits>
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/index_tuple.hpp> #include <sprout/index_tuple.hpp>
#include <sprout/array.hpp>
#include <sprout/iterator/reverse_iterator.hpp> #include <sprout/iterator/reverse_iterator.hpp>
#include <sprout/utility/forward.hpp> #include <sprout/utility/forward.hpp>
#include <sprout/string/char_traits.hpp> #include <sprout/string/char_traits.hpp>
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT #include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT
#if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION #if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION
# include <sprout/iterator/index_iterator.hpp> # include <sprout/iterator/index_iterator.hpp>
#endif #endif
@ -453,35 +455,45 @@ namespace sprout {
private: private:
typedef sprout::basic_string<T, N, Traits> copied_type; typedef sprout::basic_string<T, N, Traits> copied_type;
private: private:
template<std::size_t S> template<std::size_t M>
static SPROUT_CONSTEXPR copied_type make_impl(typename copied_type::size_type size) { static SPROUT_CONSTEXPR typename copied_type::size_type length_impl(sprout::array<T, M> const& arr) {
return copied_type{{}, size}; return NS_SSCRISK_CEL_OR_SPROUT::distance(
arr.begin(),
NS_SSCRISK_CEL_OR_SPROUT::find(arr.begin(), arr.end(), T())
);
} }
template<std::size_t S, typename Head, typename... Tail> template<typename... Args, sprout::index_t... Indexes>
static SPROUT_CONSTEXPR typename std::enable_if< static SPROUT_CONSTEXPR copied_type make_impl(
S == sizeof...(Tail), typename copied_type::size_type size,
copied_type sprout::index_tuple<Indexes...>,
>::type make_impl(typename copied_type::size_type size, Head&& head, Tail&&... tail) { Args&&... args
return copied_type{{sprout::forward<Tail>(tail)..., sprout::forward<Head>(head)}, size}; )
} {
template<std::size_t S, typename Head, typename... Tail> return copied_type{
static SPROUT_CONSTEXPR typename std::enable_if< {(Indexes < size ? sprout::forward<Args>(args) : T())...},
S != sizeof...(Tail), size
copied_type };
>::type make_impl(typename copied_type::size_type size, Head&& head, Tail&&... tail) {
return make_impl<S + 1>(size, sprout::forward<Tail>(tail)..., S >= size ? T() : sprout::forward<Head>(head));
} }
public: public:
static SPROUT_CONSTEXPR typename copied_type::size_type length() { template<typename... Args>
return 0; static SPROUT_CONSTEXPR typename copied_type::size_type length(Args&&... args) {
return length_impl(sprout::make_array<T>(sprout::forward<Args>(args)...));
} }
template<typename... Tail> template<typename... Args>
static SPROUT_CONSTEXPR typename copied_type::size_type length(T const& head, Tail&&... tail) { static SPROUT_CONSTEXPR copied_type make(Args&&... args) {
return !head ? 0 : 1 + length(sprout::forward<Tail>(tail)...); return make_impl(
length(args...),
sprout::index_range<0, sizeof...(Args)>::make(),
sprout::forward<Args>(args)...
);
} }
template<typename... Args> template<typename... Args>
static SPROUT_CONSTEXPR copied_type make(typename copied_type::size_type size, Args&&... args) { static SPROUT_CONSTEXPR copied_type make(typename copied_type::size_type size, Args&&... args) {
return make_impl<0>(size, sprout::forward<Args>(args)...); return make_impl(
size,
sprout::index_range<0, sizeof...(Args)>::make(),
sprout::forward<Args>(args)...
);
} }
}; };
} // namespace detail } // namespace detail