From 2905965f240399503784907b7477fc4b6d39792e Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Thu, 20 Feb 2014 08:04:36 +0900 Subject: [PATCH] fix iota --- example/fizzbuzz/main.cpp | 25 +++--- sprout/numeric/fixed/iota.hpp | 2 +- sprout/preprocessor/unique_string.hpp | 8 +- testspr/print.hpp | 115 ++++++++++++++++++++++---- 4 files changed, 116 insertions(+), 34 deletions(-) diff --git a/example/fizzbuzz/main.cpp b/example/fizzbuzz/main.cpp index 0737c9f0..ee4b219d 100644 --- a/example/fizzbuzz/main.cpp +++ b/example/fizzbuzz/main.cpp @@ -16,7 +16,6 @@ #include #include #include -#include #include @@ -24,7 +23,7 @@ struct fizzbuzz{ typedef sprout::string<12> result_type; constexpr result_type - operator ()(int n) const{ + operator()(int n) const { return n % 15 == 0 ? sprout::to_string("FizzBuzz") : n % 3 == 0 ? sprout::to_string("Fizz") : n % 5 == 0 ? sprout::to_string("Buzz") @@ -40,29 +39,25 @@ main(){ // // Test // - static_assert(fizzbuzz()( 1) == "1", ""); - static_assert(fizzbuzz()( 2) == "2", ""); - static_assert(fizzbuzz()( 3) == "Fizz", ""); - static_assert(fizzbuzz()( 5) == "Buzz", ""); + static_assert(fizzbuzz()( 1) == "1", ""); + static_assert(fizzbuzz()( 2) == "2", ""); + static_assert(fizzbuzz()( 3) == "Fizz", ""); + static_assert(fizzbuzz()( 5) == "Buzz", ""); static_assert(fizzbuzz()(15) == "FizzBuzz", ""); // // Sequence [1..15] // - constexpr auto source = sprout::iota( - sprout::pit >(), - 1 - ); + constexpr auto source = sprout::iota >(1); // // Transform to FizzBuzz // - constexpr auto result = sprout::transform( + constexpr auto result = sprout::transform >( sprout::begin(source), sprout::end(source), - sprout::pit >(), fizzbuzz() - ); + ); // // Check result @@ -83,14 +78,14 @@ main(){ sprout::to_string("13"), sprout::to_string("14"), sprout::to_string("FizzBuzz") - ); + ); // Equal result sequence static_assert(result == fizzbuzz_result, ""); // // Output // - for(auto&& str : result){ + for (auto&& str : result){ std::cout << str << ", "; } std::cout << std::endl; diff --git a/sprout/numeric/fixed/iota.hpp b/sprout/numeric/fixed/iota.hpp index 17722c5c..e0160bc7 100644 --- a/sprout/numeric/fixed/iota.hpp +++ b/sprout/numeric/fixed/iota.hpp @@ -34,7 +34,7 @@ namespace sprout { cont, sprout::size(cont), (Indexes >= offset && Indexes < offset + size - ? value + (Indexes - offset) + ? static_cast::value_type>(value + (Indexes - offset)) : *sprout::next(sprout::internal_begin(cont), Indexes) )... ); diff --git a/sprout/preprocessor/unique_string.hpp b/sprout/preprocessor/unique_string.hpp index 9997fe79..014a79a5 100644 --- a/sprout/preprocessor/unique_string.hpp +++ b/sprout/preprocessor/unique_string.hpp @@ -20,19 +20,19 @@ // SPROUT_PP_UNIQUE_U32STRING // #define SPROUT_PP_UNIQUE_STRING \ - SPROUT_PP_STRINGIZE(SPROUT_PP_SOME_NUMBER_OR_EMPTY()) " " \ + SPROUT_PP_STRINGIZE(SPROUT_PP_SOME_NUMBER()) " " \ __DATE__ " " __TIME__ \ " : " __FILE__ ":" SPROUT_PP_STRINGIZE(__LINE__) #define SPROUT_PP_UNIQUE_WSTRING \ - SPROUT_PP_WSTRINGIZE(SPROUT_PP_SOME_NUMBER_OR_EMPTY()) SPROUT_PP_WSTR(" ") \ + SPROUT_PP_WSTRINGIZE(SPROUT_PP_SOME_NUMBER()) SPROUT_PP_WSTR(" ") \ SPROUT_PP_WSTR(__DATE__) SPROUT_PP_WSTR(" ") SPROUT_PP_WSTR(__TIME__) \ SPROUT_PP_WSTR(" : ") SPROUT_PP_WSTR(__FILE__) SPROUT_PP_WSTR(":") SPROUT_PP_WSTRINGIZE(__LINE__) #define SPROUT_PP_UNIQUE_U16STRING \ - SPROUT_PP_U16STRINGIZE(SPROUT_PP_SOME_NUMBER_OR_EMPTY()) SPROUT_PP_U16STR(" ") \ + SPROUT_PP_U16STRINGIZE(SPROUT_PP_SOME_NUMBER()) SPROUT_PP_U16STR(" ") \ SPROUT_PP_U16STR(__DATE__) SPROUT_PP_U16STR(" ") SPROUT_PP_U16STR(__TIME__) \ SPROUT_PP_U16STR(" : ") SPROUT_PP_U16STR(__FILE__) SPROUT_PP_U16STR(":") SPROUT_PP_U16STRINGIZE(__LINE__) #define SPROUT_PP_UNIQUE_U32STRING \ - SPROUT_PP_U32STRINGIZE(SPROUT_PP_SOME_NUMBER_OR_EMPTY()) SPROUT_PP_U32STR(" ") \ + SPROUT_PP_U32STRINGIZE(SPROUT_PP_SOME_NUMBER()) SPROUT_PP_U32STR(" ") \ SPROUT_PP_U32STR(__DATE__) SPROUT_PP_U32STR(" ") SPROUT_PP_U32STR(__TIME__) \ SPROUT_PP_U32STR(" : ") SPROUT_PP_U32STR(__FILE__) SPROUT_PP_U32STR(":") SPROUT_PP_U32STRINGIZE(__LINE__) diff --git a/testspr/print.hpp b/testspr/print.hpp index 557b36f8..b3204038 100644 --- a/testspr/print.hpp +++ b/testspr/print.hpp @@ -11,22 +11,22 @@ #include #include #include +#include #include +#include #include +#include #include namespace testspr { // // print // - template - void print(InputIterator first, InputIterator last) { - std::for_each(first, last, [](typename std::iterator_traits::value_type const& e){ std::cout << e << ' '; }); - std::cout << std::endl; - } - template - void print(InputRange const& range) { - testspr::print(sprout::begin(range), sprout::end(range)); + void print() {} + template + void print(Head const& head, Tail const&... tail) { + std::cout << head; + testspr::print(tail...); } // @@ -35,14 +35,53 @@ namespace testspr { void print_ln() { std::cout << std::endl; } - template - void print_ln(T const& t) { - std::cout << t << std::endl; + template + void print_ln(Args const&... args) { + sprout::detail::io::ios_all_saver saver(std::cout); + testspr::print(args...); + std::cout << std::endl; + } + + // + // print_tokens + // + void print_tokens() { + testspr::print_ln(); } template - void print_ln(Head const& head, Tail const&... tail) { - std::cout << head; - testspr::print_ln(tail...); + void print_tokens(Head const& head, Tail const&... tail) { + sprout::detail::io::ios_all_saver saver(std::cout); + testspr::print(head, ' '); + testspr::print_tokens(tail...); + } + + // + // print_quotes + // + void print_quotes() { + testspr::print_ln(); + } + template + void print_quotes(Head const& head, Tail const&... tail) { + sprout::detail::io::ios_all_saver saver(std::cout); + testspr::print('\"', head, "\" "); + testspr::print_quotes(tail...); + } + + // + // print_range + // + template + void print_range(InputIterator first, InputIterator last) { + sprout::detail::io::ios_all_saver saver(std::cout); + for (; first != last; ++first) { + std::cout << *first << ' '; + } + std::cout << std::endl; + } + template + void print_range(InputRange const& range) { + testspr::print_range(sprout::begin(range), sprout::end(range)); } // @@ -75,6 +114,54 @@ namespace testspr { void print_hl() { testspr::print_ln("--------------------------------------------------------------------------------"); } + + // + // manip_holder + // + template + class manip_holder { + public: + typedef T value_type; + private: + value_type m_; + public: + manip_holder(value_type const& m) + : m_(m) + {} + value_type const& get() const { + return m_; + } + }; + template + std::basic_ostream& operator<<(std::basic_ostream& lhs, testspr::manip_holder const& rhs) { + return lhs << rhs.get(); + } + template + std::basic_istream& operator>>(std::basic_istream& lhs, testspr::manip_holder const& rhs) { + return lhs >> rhs.get(); + } + // + // manip + // + template + T&& + manip(T&& t) { + return std::forward(t); + } + template + testspr::manip_holder& (*)(std::basic_ostream&)> + manip(std::basic_ostream& (*pf)(std::basic_ostream&)) { + return pf; + } + template + testspr::manip_holder& (*)(std::basic_ios&)> + manip(std::basic_ios& (*pf)(std::basic_ios&)) { + return pf; + } + testspr::manip_holder + manip(std::ios_base& (*pf)(std::ios_base&)) { + return pf; + } } // namespace testspr #endif // #ifndef TESTSPR_PRINT_HPP