Sprout/sprout/algorithm/fixed/generate.hpp

103 lines
3.8 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
Copyright (c) 2011-2013 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)
=============================================================================*/
2011-09-01 02:48:32 +00:00
#ifndef SPROUT_ALGORITHM_FIXED_GENERATE_HPP
#define SPROUT_ALGORITHM_FIXED_GENERATE_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/container/traits.hpp>
#include <sprout/container/functions.hpp>
#include <sprout/algorithm/fixed/results.hpp>
#include <sprout/generator/functions.hpp>
2013-02-07 14:12:57 +00:00
#include <sprout/pit/pit.hpp>
#include <sprout/detail/container_complate.hpp>
2011-09-01 02:48:32 +00:00
namespace sprout {
namespace fixed {
namespace detail {
template<typename Container, typename Next, typename... Args>
inline SPROUT_CONSTEXPR typename std::enable_if<
sprout::container_traits<Container>::static_size == sizeof...(Args) + 1,
typename sprout::fixed::results::algorithm<Container>::type
>::type
generate_impl_1(
Container const& cont, Next const& next,
typename sprout::container_traits<Container>::size_type size,
2011-09-01 02:48:32 +00:00
Args const&... args
)
{
return sprout::remake<Container>(cont, size, args..., sprout::generators::generated_value(next));
2011-09-01 02:48:32 +00:00
}
template<typename Container, typename Next, typename... Args>
inline SPROUT_CONSTEXPR typename std::enable_if<
sprout::container_traits<Container>::static_size != sizeof...(Args) + 1,
typename sprout::fixed::results::algorithm<Container>::type
>::type
generate_impl_1(
Container const& cont, Next const& next,
typename sprout::container_traits<Container>::size_type size,
2011-09-01 02:48:32 +00:00
Args const&... args
)
{
return sizeof...(Args) + 1 < size
? sprout::fixed::detail::generate_impl_1(
cont, sprout::generators::next_generator(next)(), size,
args..., sprout::generators::generated_value(next)
)
: sprout::detail::container_complate(cont, args..., sprout::generators::generated_value(next))
;
2011-09-01 02:48:32 +00:00
}
template<typename Container, typename Generator>
inline SPROUT_CONSTEXPR typename std::enable_if<
sprout::container_traits<Container>::static_size == 0,
typename sprout::fixed::results::algorithm<Container>::type
>::type
generate_impl(
2013-07-22 13:00:09 +00:00
Container const& cont, Generator const&,
typename sprout::container_traits<Container>::size_type size
2011-09-01 02:48:32 +00:00
)
{
return sprout::remake<Container>(cont, size);
2011-09-01 02:48:32 +00:00
}
template<typename Container, typename Generator>
inline SPROUT_CONSTEXPR typename std::enable_if<
sprout::container_traits<Container>::static_size != 0,
typename sprout::fixed::results::algorithm<Container>::type
>::type
generate_impl(
2012-09-27 13:15:29 +00:00
Container const& cont, Generator const& gen,
typename sprout::container_traits<Container>::size_type size
2011-09-01 02:48:32 +00:00
)
{
return size > 0
? sprout::fixed::detail::generate_impl_1(cont, gen(), size)
: sprout::detail::container_complate(cont)
;
2011-09-01 02:48:32 +00:00
}
} // namespace detail
//
// generate
//
template<typename Container, typename Generator>
inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm<Container>::type
2012-09-27 13:15:29 +00:00
generate(Container const& cont, Generator const& gen) {
return sprout::fixed::detail::generate_impl(cont, gen, sprout::size(cont));
2011-09-01 02:48:32 +00:00
}
template<typename Container, typename Generator>
inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm<Container>::type
generate(Generator const& gen) {
2013-04-11 06:50:14 +00:00
return sprout::fixed::generate(sprout::pit<Container>(), gen);
}
2011-09-01 02:48:32 +00:00
} // namespace fixed
2011-09-03 13:26:26 +00:00
using sprout::fixed::generate;
2011-09-01 02:48:32 +00:00
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_FIXED_GENERATE_HPP