Sprout

Sprout で乱数の配列を生成する

Sprout で乱数の配列を生成する場合、sprout::generate と sprout::random::combine を使用します。 [ソース] #include <sprout/random.hpp> #include <sprout/array.hpp> #include <sprout/algorithm.hpp> #include <iostream> int main(){ static constexpr sprout::default_random_engine engine; static constexpr sprout::unif</iostream></sprout/algorithm.hpp></sprout/array.hpp></sprout/random.hpp>…

Sprout.Random で次の乱数を取得する

constexpr 時の Sprout.Random は内部の値が変更できない為、標準ライブラリ とは違い operator() を何回呼び出しても同じ値しか返ってきません。 ではどうやって次の乱数値を取得するのかというと operator() を再度呼び出します。 [ソース] #include <sprout/random.hpp> #inc</sprout/random.hpp>…

Sprout.Random でコンパイル時に暗号化の鍵を生成

やってみました。 [ソース] #include <algorithm> struct ango{ explicit ango(int key) : key(key){} template<typename Range> Range operator ()(Range range) const{ typedef typename std::iterator_traits<decltype(std::begin(range))>::value_type value_type; auto result = std::move(range); std::transfor</decltype(std::begin(range))></typename></algorithm>…

Sprout.Random で乱数の種を設定

Sprout.Random で乱数の種を設定する場合、乱数エンジンのコンストラクタに整数値で種を渡します。 また、SPROUT_UNIQUE_SEED マクロを使用する事でコンパイル毎に違う値の種を設定する事が出来ます。 [ソース] #include <sprout/random.hpp> #include <iostream> #include <boost/mpl/print.hpp> int main(){ s</boost/mpl/print.hpp></iostream></sprout/random.hpp>…

Sprout.Random でコンパイル時乱数

少し使ってみました。 [ソース] #include <sprout/random.hpp> #include <iostream> int main(){ namespace random = sprout::random; static constexpr random::hellekalek1995 engine{}; // 100 ~ 999 static constexpr random::uniform_int_distribution<int> dist1(100, 999); static_asser</int></iostream></sprout/random.hpp>…

Sprout の make_array と make_common_array

最近 Boost ML で『make_array を用意しよう』みたいな提案がなされています。 Proposal: boost::make_array() 内容はあんまり読んでないんですが個人的にはあってもいいかなーと思っています。 最終的にどうなるんかな。 で、それはそれとして Sprout.Array…

constexpr which を get に渡す

そういえば、constexpr variant だと which の値を直接 get に渡すことが出来ますねーと。 [ソース] #include <sprout/variant.hpp> #include <sprout/string.hpp> int main(){ typedef sprout::variant<int, double, sprout::string<16>> var; { constexpr var v{42}; static_assert(sprout::get<v.which()>(v) == 42, ""); } { constexpr var v</v.which()></int,></sprout/string.hpp></sprout/variant.hpp>…

sprout::apply_visitor で static_visitor に依存しないアプローチ

Sprout.Variant の apply_visitor に『static_visitor を継承していない(result_type が定義されていない) Visitor を渡したい!』というムチャぶりをした結果、いくつかの代案が出たので覚書。 1.文字列にして返す 言葉通り文字列にして返してあとから復…

Sprout で長さの短い文字列型へ変換する

いつも忘れてしまうので覚書。 さて、Sprout.String では、長さの長い文字列型への型変換は暗黙的に行なってくれるのですが、長さの短い文字列型への変換は行えません。 constexpr sprout::string<16> homu = sprout::to_string("homu"); // error // conste…

【C++ Advent Calendar 2012】Sprout を使うたった一つの理由【2日目(前編)】

C++ Advent Calendar 2012 2日目の記事…なんですが、書きたいことが2つあったので2部作にしてしまいましたてへぺろ(・ω<) と、いう事で本記事は前編になります。 後編はこちら。 Boost.TypeErasure 概要 constexpr ライブラリである Sprout の布教を簡単…

Sprout に range adaptor が追加されていたので使ってみた

軽く触ってみました。 [ソース] #include <sprout/range/adaptor/jointed.hpp> #include <sprout/range/adaptor/dropped.hpp> #include <sprout/range/adaptor/taken.hpp> #include <sprout/range/adaptor/counting.hpp> #include <sprout/range/adaptor/filtered.hpp> #include <sprout/range/adaptor/copied.hpp> #include </sprout/range/adaptor/copied.hpp></sprout/range/adaptor/filtered.hpp></sprout/range/adaptor/counting.hpp></sprout/range/adaptor/taken.hpp></sprout/range/adaptor/dropped.hpp></sprout/range/adaptor/jointed.hpp>

Sprout.Weed でコンパイル時に __DATE__ のパース

久々に Sprout.Weed を触りたくなったので。 [ソース] #include <sprout/weed.hpp> #include <sprout/string.hpp> int main(){ namespace w = sprout::weed; static constexpr auto date = sprout::to_string(__DATE__); static constexpr auto parser = *w::lim<3>(w::char_ - w::space_) >> *w:</sprout/string.hpp></sprout/weed.hpp>…

Sprout で constexpr fused

最近 Sprout をあんまり触っていなかったんですが、いつの間にか実装されていました。 使い方は Boost.Fusion と同じですね。 [ソース] #include <sprout/functional.hpp> #include <sprout/tuple.hpp> #include <sprout/string.hpp> struct sum{ template<typename String> constexpr float operator ()(int a, String s, float f) const{ </typename></sprout/string.hpp></sprout/tuple.hpp></sprout/functional.hpp>…

template に float 値を渡す

こんな感じでどうか。 [ソース] #include <boost/preprocessor/repetition/enum.hpp> #include <sprout/string.hpp> #include <sprout/cstdlib.hpp> #include <sprout/operation.hpp> template<std::size_t Index, typename String> constexpr char at_c(String const& str){ return Index >= str.length() ? char('\0') : str[Index]; }…</std::size_t></sprout/operation.hpp></sprout/cstdlib.hpp></sprout/string.hpp></boost/preprocessor/repetition/enum.hpp>

Sprout に example を突っ込んでいる

ちょっと前からですが、id:boleros さんの constexpr ライブラリ Sprout を fork して example を pull request とかしています。 GitHubへpull requestする際のベストプラクティス - hnwの日記 まぁ単純にわたしが『Sprout の example 欲しい』っていうのも…

Sprout::Weed の lim を短くする

特定の文字列をパースする場合に Sprout::Weed の lim を使用して *w::lim<4>(w::char_ - ':') >> ":" >> *w::lim<2>(w::char_) の様なパーサを定義するんですが、これだと若干長くなってしまいます。 これはこれで汎用性があっていいのですが、簡単なパーサ…

Sprout.Weed で複数行のパース

[ソース] #include <sprout/weed.hpp> int main(){ namespace w = sprout::weed; using sprout::make_common_array; using sprout::to_string; static constexpr auto width = 16; static constexpr auto height = 16; static constexpr auto string = *w::lim<width>(w::char_ - w::</width></sprout/weed.hpp>…

AtCoder #002 の A を解いてみた

参加はしていなかったんですが、終わったあとに問題を見かけたので解いてみました。 [ソース] #include <cstddef> #include <boost/preprocessor/repetition/enum.hpp> // // static print // template<std::size_t Index, typename String> constexpr char at_c(String const& str){ return Index >= str.length() ? char('\0') : str[Index]; } te</std::size_t></boost/preprocessor/repetition/enum.hpp></cstddef>…

Sprout.IO

Sprout に IO を扱うライブラリが追加されたのでちょっと触ってみた。 ちなみにまだ Input はないらしい。 詳細については id:boleros さんの記事を参照して下さい。 Sprout.Io - とりあえず文字列へ変換/コンパイル時のパフォーマンスとバッファサイズの話…

constexpr で Brainfuck

とりあえず、Sprout を使って書いてみました。 Sprout まじ Sprout。 あんまり効率とかは考えないでベタが出来ですが、入力から出力までをコンパイル時に行います。 そういえば constexpr で Brainfuck の実装は見かけたことがないような? [ソース] #includ…

Sprout で配列の指定した位置に値を代入する

sprout::set({cont}, {inx}, {v}); を使用します。 [ソース] #include <sprout/operation/set.hpp> #include <sprout/array.hpp> int main(){ constexpr auto source = sprout::make_array<int>(1, 2, 3, 4); constexpr auto result = sprout::set(source, 3, -1); static_assert(result == sprout::make_array<int></int></int></sprout/array.hpp></sprout/operation/set.hpp>…

Sprout を使った constexpr stringstream

ちょっと欲しかったので簡単に書いてみました。 [ソース] #include <sprout/string.hpp> #include <sprout/operation.hpp> template<std::size_t Size, typename String = sprout::string<Size>> struct basic_stringstream{ constexpr String const& str() const{ return string; } String string; }; typedef basic_stringstream<0> stringstream; …</std::size_t></sprout/operation.hpp></sprout/string.hpp>

Sprout で指定した値で配列の初期化を行う

sprout::fill を使用します。 [ソース] #include <sprout/array.hpp> #include <sprout/algorithm/fill.hpp> #include <sprout/algorithm/fill_n.hpp> #include <sprout/pit.hpp> int main(){ // 今回は、初期化時に fill して欲しいので // 処理結果の型を sprout::pit でラップして引数に渡す static constexpr auto result = sprout::fill(sprout::pit</sprout/pit.hpp></sprout/algorithm/fill_n.hpp></sprout/algorithm/fill.hpp></sprout/array.hpp>

コンパイル時 FizzBuzz の出力

コンパイル時に FizzBuzz を処理して出力までするのは中3女子の嗜みですよね! ということでこれを使って、とりあえずコンパイル時にも出力してみた。 Sprout のコードは適当。 [ソース] #include <boost/preprocessor/repetition/enum.hpp> #include <boost/mpl/print.hpp> #include <sprout/algorithm/transform.hpp> #include <sprout/array.hpp> #include <sprout/string.hpp> #include </sprout/string.hpp></sprout/array.hpp></sprout/algorithm/transform.hpp></boost/mpl/print.hpp></boost/preprocessor/repetition/enum.hpp>

コンパイル時に Sprout.String を出力する

先日行ったやり方で、コンパイル時に Sprout.String の内容を出力してみました。 [ソース] #include <boost/preprocessor/repetition/enum.hpp> #include <sprout/string.hpp> template<std::size_t Index, typename String> constexpr char at_c(String const& str){ return Index >= str.length() ? char('\0') : str[Index]; } template<char... cs> struc…</char...></std::size_t></sprout/string.hpp></boost/preprocessor/repetition/enum.hpp>

Sprout が破壊的に変更された

Sprout の破壊的変更 - constexpr コンテナのトレイト周りの刷新 - Sprout の破壊的変更 - constexpr コンテナのトレイト周りの刷新 当ブログでもいくつか Sprout のコードを扱っているので参考にする方は注意して下さい。 まぁ constexpr 系中3女子の方はあ…

文字列ポインタから sprout::string へ変換

sprout::string_from_c_str を使えばいいみたいです。 [ソース] #include <sprout/string.hpp> int main(){ constexpr char const* c_str = "homu"; // sprout::to_string では文字列ポインタから変換出来ない // constexpr auto str = sprout::to_string(c_str); // sprout::st</sprout/string.hpp>…

コンパイル時乱数の生成

と、いうものは Sprout にも実装されているんですが、ちょっと自分でも書いてみました。 まぁちと怪しいですが。 [ソース] #include <utility> #include <iterator> struct random{ using seed_type = std::size_t; constexpr random() : seed(2531011L){} constexpr random(see</iterator></utility>…

Sprout の SPROUT_CONFIG_SUPPORT_TEMPORARY_CONTAINER_ITERATION

元々 Sprout を gcc 4.7 で使用する場合に #define SPROUT_CONFIG_SUPPORT_TEMPORARY_CONTAINER_ITERATION をユーザ側で定義しないとコンパイルがされないコード(一時オブジェクトを algorithm で使用する場合等)があったんですが、いつの間にかユーザ側で…

Sprout.Weed でセマンティックアクション

的ななにか。 Boost.Spirit.Qi とはちと挙動が違うと思いますが、あったらあったで便利そうな感じがします。 [ソース] #define SPROUT_CONFIG_SUPPORT_TEMPORARY_CONTAINER_ITERATION #include <sprout/weed.hpp> #include <sprout/string/alias.hpp> template<typename T> using decay = typename std::decay<T>::type</t></typename></sprout/string/alias.hpp></sprout/weed.hpp>…