From 40b95948a92654d8013b71911383f0e15a24b25c Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Wed, 9 Oct 2013 16:14:41 +0900 Subject: [PATCH] add example hanoi --- example/hanoi/main.cpp | 48 ++++++++++++++++++++++ sprout/integer.hpp | 15 +++++++ sprout/integer/static_pow.hpp | 76 +++++++++++++++++++++++++++++++++++ testspr/header_all.hpp | 2 +- tools/testspr/test.py | 9 +++-- 5 files changed, 145 insertions(+), 5 deletions(-) create mode 100644 example/hanoi/main.cpp create mode 100644 sprout/integer.hpp create mode 100644 sprout/integer/static_pow.hpp diff --git a/example/hanoi/main.cpp b/example/hanoi/main.cpp new file mode 100644 index 00000000..b60ea09a --- /dev/null +++ b/example/hanoi/main.cpp @@ -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 +#include +#include +#include +#include +#include +#include + +// +// hanoi +// +template< + std::size_t N, typename Id, + typename sprout::enabler_if<(N == 0)>::type = sprout::enabler +> +SPROUT_CONSTEXPR sprout::array, 0> +hanoi(Id, Id, Id) { + return sprout::array, 0>{{}}; +} +template< + std::size_t N, typename Id, + typename sprout::enabler_if<(N != 0)>::type = sprout::enabler +> +SPROUT_CONSTEXPR sprout::array, sprout::static_pow2m1::value> +hanoi(Id a, Id b, Id c) { + typedef sprout::tuple type; + return sprout::append_back( + sprout::push_back(hanoi(a, c, b), type(a, b, N)), + hanoi(c, b, a) + ); +} + +#include + +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; + } +} diff --git a/sprout/integer.hpp b/sprout/integer.hpp new file mode 100644 index 00000000..190530e0 --- /dev/null +++ b/sprout/integer.hpp @@ -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 +#include +#include + +#endif // #ifndef SPROUT_INTEGER_HPP diff --git a/sprout/integer/static_pow.hpp b/sprout/integer/static_pow.hpp new file mode 100644 index 00000000..e92f737c --- /dev/null +++ b/sprout/integer/static_pow.hpp @@ -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 +#include + +namespace sprout { + // + // static_pow2 + // + template + struct static_pow2 + : public std::integral_constant + {}; + + namespace detail { + template + struct static_pow2m1; + template + struct static_pow2m1 + : public std::integral_constant + {}; + template + struct static_pow2m1 + : public std::integral_constant + {}; + } // namespace detail + // + // static_pow2m1 + // + template + struct static_pow2m1 + : public sprout::detail::static_pow2m1 + {}; + + namespace detail { + template + struct static_pow; + template + struct static_pow + : public std::integral_constant + {}; + template + struct static_pow + : public std::integral_constant + {}; + template + struct static_pow + : public std::integral_constant + {}; + template + struct static_pow + : public sprout::detail::static_pow::value, 2> + {}; + template + struct static_pow + : public std::integral_constant::value> + {}; + } // namespace detail + // + // static_pow + // + template + struct static_pow + : public sprout::detail::static_pow + {}; +} // namespace sprout + +#endif // #ifndef SPROUT_INTEGER_STATIC_POW_HPP diff --git a/testspr/header_all.hpp b/testspr/header_all.hpp index d652fc4f..ae125a08 100644 --- a/testspr/header_all.hpp +++ b/testspr/header_all.hpp @@ -40,7 +40,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/tools/testspr/test.py b/tools/testspr/test.py index 52a8903f..44383772 100644 --- a/tools/testspr/test.py +++ b/tools/testspr/test.py @@ -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 )