2014-02-01から1ヶ月間の記事一覧

C++の剰余の定義

例えば負の数の絡むC++の剰余の計算結果は?そもそもC++の剰余演算の定義は。 調べたところstackoverflowに回答がありました。 http://stackoverflow.com/questions/7594508/modulo-operator-with-negative-values C++03では実装依存のようですが、C++11では…

プリプロセッサで基数変換

10進数→2進数 実装は非常に簡単(http://melpon.org/wandbox/permlink/NKeqHAbRSTsYap0a) #include <boost/preprocessor.hpp> #define PRED(n, state) BOOST_PP_TUPLE_ELEM(2, 1, state) #define OP(d, state) \ (BOOST_PP_CAT(BOOST_PP_MOD(BOOST_PP_TUPLE_ELEM(2, 1, state), 2), \ </boost/preprocessor.hpp>…

zipのような

前の記事でunzipのようなものを書いたので逆の、sprout::tuple<sprout::array<Types>...>からsprout::array<sprout::tuple<Types...>>への変換も書きました。長さが違う場合、短い方に合わせます。最も要素数の少ない配列の要素数探索を行う際のオーダーがO(N)ですが、対数オーダーでソートしてから二分探</sprout::tuple<types...></sprout::array<types>…

unzipのような

車輪の再発明だと思いますが、sprout::array<sprout::tuple<Types...>>からsprout::tuple<sprout::array<Types>...>を作って返す関数を実装してみました。アイデアはHaskellのunzip関数です。 Wandbox http://melpon.org/wandbox/permlink/aRxrOWetWilTbwZ3 #include <sprout/tuple.hpp> #include <sprout/array.hpp> #include <sprout/index_tuple.hpp> template </sprout/index_tuple.hpp></sprout/array.hpp></sprout/tuple.hpp></sprout::array<types></sprout::tuple<types...>

CプリプロセッサによるIFの実装

愚直な実装。 #define TRUE 1 #define FALSE 0 #define IF_1(x, y) x #define IF_0(x, y) y #define IF_I(cond, x, y) IF_##cond(x, y) #define IF(cond, x, y) IF_I(cond, x, y) IF(TRUE, hello, world) IF(FALSE, hello, world)