add example hanoi

This commit is contained in:
bolero-MURAKAMI 2013-10-09 16:14:41 +09:00
parent 3dfe41361f
commit 40b95948a9
5 changed files with 145 additions and 5 deletions

48
example/hanoi/main.cpp Normal file
View file

@ -0,0 +1,48 @@
/*=============================================================================
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)
=============================================================================*/
#include <cstddef>
#include <sprout/array.hpp>
#include <sprout/tuple/tuple.hpp>
#include <sprout/operation/push_back.hpp>
#include <sprout/operation/append_back.hpp>
#include <sprout/integer/static_pow.hpp>
#include <sprout/type_traits/enabler_if.hpp>
//
// hanoi
//
template<
std::size_t N, typename Id,
typename sprout::enabler_if<(N == 0)>::type = sprout::enabler
>
SPROUT_CONSTEXPR sprout::array<sprout::tuple<Id, Id, std::size_t>, 0>
hanoi(Id, Id, Id) {
return sprout::array<sprout::tuple<Id, Id, std::size_t>, 0>{{}};
}
template<
std::size_t N, typename Id,
typename sprout::enabler_if<(N != 0)>::type = sprout::enabler
>
SPROUT_CONSTEXPR sprout::array<sprout::tuple<Id, Id, std::size_t>, sprout::static_pow2m1<std::size_t, N>::value>
hanoi(Id a, Id b, Id c) {
typedef sprout::tuple<Id, Id, std::size_t> type;
return sprout::append_back(
sprout::push_back(hanoi<N - 1>(a, c, b), type(a, b, N)),
hanoi<N - 1>(c, b, a)
);
}
#include <iostream>
int main() {
constexpr auto answer = hanoi<8>('a', 'b', 'c');
for (auto const& e : answer) {
std::cout << "disk " << sprout::get<2>(e) << ": " << sprout::get<0>(e) << " -> " << sprout::get<1>(e) << std::endl;
}
}

15
sprout/integer.hpp Normal file
View file

@ -0,0 +1,15 @@
/*=============================================================================
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)
=============================================================================*/
#ifndef SPROUT_INTEGER_HPP
#define SPROUT_INTEGER_HPP
#include <sprout/config.hpp>
#include <sprout/integer/integer_digits.hpp>
#include <sprout/integer/static_pow.hpp>
#endif // #ifndef SPROUT_INTEGER_HPP

View file

@ -0,0 +1,76 @@
/*=============================================================================
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)
=============================================================================*/
#ifndef SPROUT_INTEGER_STATIC_POW_HPP
#define SPROUT_INTEGER_STATIC_POW_HPP
#include <type_traits>
#include <sprout/config.hpp>
namespace sprout {
//
// static_pow2
//
template<typename T, T X>
struct static_pow2
: public std::integral_constant<T, (1 << X)>
{};
namespace detail {
template<typename T, T X, bool IsZero = (X == 0)>
struct static_pow2m1;
template<typename T, T X>
struct static_pow2m1<T, X, true>
: public std::integral_constant<T, 1>
{};
template<typename T, T X>
struct static_pow2m1<T, X, false>
: public std::integral_constant<T, ((((T(1) << (X - 1)) - 1) << 1) + 1)>
{};
} // namespace detail
//
// static_pow2m1
//
template<typename T, T X>
struct static_pow2m1
: public sprout::detail::static_pow2m1<T, X>
{};
namespace detail {
template<typename T, T X, T Y, int K = (Y < 3 ? int(Y) : 3), bool IsOdd = (Y % 2 != 0)>
struct static_pow;
template<typename T, T X, T Y>
struct static_pow<T, X, Y, 0, false>
: public std::integral_constant<T, 1>
{};
template<typename T, T X, T Y>
struct static_pow<T, X, Y, 1, true>
: public std::integral_constant<T, X>
{};
template<typename T, T X, T Y>
struct static_pow<T, X, Y, 2, false>
: public std::integral_constant<T, X * X>
{};
template<typename T, T X, T Y, int K>
struct static_pow<T, X, Y, K, false>
: public sprout::detail::static_pow<T, sprout::detail::static_pow<T, X, Y / 2>::value, 2>
{};
template<typename T, T X, T Y, int K>
struct static_pow<T, X, Y, K, true>
: public std::integral_constant<T, X * sprout::detail::static_pow<T, X, Y - 1>::value>
{};
} // namespace detail
//
// static_pow
//
template<typename T, T X, T Y>
struct static_pow
: public sprout::detail::static_pow<T, X, Y>
{};
} // namespace sprout
#endif // #ifndef SPROUT_INTEGER_STATIC_POW_HPP

View file

@ -40,7 +40,7 @@
#include <sprout/generator.hpp>
#include <sprout/index_tuple.hpp>
#include <sprout/io.hpp>
#include <sprout/integer/integer_digits.hpp>
#include <sprout/integer.hpp>
#include <sprout/iterator.hpp>
#include <sprout/limits.hpp>
#include <sprout/logic.hpp>

View file

@ -30,6 +30,9 @@ def main():
parser.add_option('--max_procs', type='int', default=0)
(opts, args) = parser.parse_args()
std_options = eval(opts.serialized_std_options)
version_specific_options = eval(opts.serialized_version_specific_options)
def format_command(name, version, root):
base = "%s-%s" % (name, version) if version != "." else name
bin = "%s/test.%s.out" % (opts.stagedir, base.replace('.', ''))
@ -37,13 +40,11 @@ def main():
execute_log = "%s/test.%s.execute.log" % (opts.stagedir, base.replace('.', ''))
compiler = "%s/%s/bin/%s++" % (root, base, name.rstrip('c')) if version != "." else "%s++" % name.rstrip('c')
return "%s -o %s" \
" %s %s" \
" %s" \
" %s %s %s" \
" %s > %s 2>&1" \
" && %s > %s 2>&1" \
% (compiler, bin,
eval(opts.serialized_std_options).get(base, ''), opts.compile_options,
eval(opts.serialized_version_specific_options).get(base, ''),
std_options.get(base, ''), opts.compile_options, version_specific_options.get(base, ''),
opts.test_cpp, compile_log,
bin, execute_log
)