7

range-v3 ライブラリ(@EricNiebler による) を使用すると、アルゴリズム コードの記述がはるかにコンパクトになります。たとえば、一連の乱数を生成する方法は次のとおりです。

#include <range/v3/all.hpp>
#include <iostream>
#include <vector>

int main() 
{
    using namespace ranges;

    auto const N = 10;
    std::vector<int> v; 
    v.reserve(N);

    v |= action::push_back(view::iota(0, N)); 
    random_shuffle(v);
    copy(v, ostream_iterator<>(std::cout, ","));
}

実例。

action::random_shuffle()ただし、このような仮説でパイプラインを拡張することをお勧めします

v |= action::push_back(view::iota(0, N)) | action::random_shuffle();

これは、そのようなアクションを書くための私の試みです (残念ながら、新しい range-v3 コードを書くことは、ライブラリを使用するよりもかなり冗長です)。

#include <functional> // bind, placeholders::_1

namespace ranges
{
    inline namespace v3
    {
        /// \addtogroup group-actions
        /// @{
        namespace action
        {
            struct random_shuffle_fn
            {
            private:
                friend action_access;

                static auto bind(random_shuffle_fn random_shuffle)
                RANGES_DECLTYPE_AUTO_RETURN
                (
                    std::bind(random_shuffle, std::placeholders::_1)
                )

                template<typename Gen>
                static auto bind(random_shuffle_fn random_shuffle, Gen && rand)
                RANGES_DECLTYPE_AUTO_RETURN
                (
                    std::bind(random_shuffle, std::placeholders::_1, bind_forward<Gen>(rand))
                )
            public:
                struct ConceptImpl
                {
                    template<typename Rng,
                        typename I = range_iterator_t<Rng>>
                    auto requires_(Rng&&) -> decltype(
                        concepts::valid_expr(
                            concepts::model_of<concepts::RandomAccessRange, Rng>(),
                            concepts::is_true(Permutable<I>())
                        ));
                };

                template<typename Rng>
                using Concept = concepts::models<ConceptImpl, Rng>;

                template<typename Rng,
                    CONCEPT_REQUIRES_(Concept<Rng>())>
                Rng operator()(Rng && rng) const
                {
                    ranges::random_shuffle(rng);
                    return std::forward<Rng>(rng);
                }

                template<typename Rng, typename Gen,
                    CONCEPT_REQUIRES_(Concept<Rng>())>
                Rng operator()(Rng && rng, Gen && rand) const
                {
                    ranges::random_shuffle(rng, std::forward<Gen>(rand));
                    return std::forward<Rng>(rng);
                }

                #ifndef RANGES_DOXYGEN_INVOKED
                template<typename Rng>
                void operator()(Rng &&) const
                {
                    CONCEPT_ASSERT_MSG(RandomAccessRange<Rng>(),
                        "The object on which action::random_shuffle operates must be a model of the "
                        "RandomAccessRange concept.");
                    using I = range_iterator_t<Rng>;
                    CONCEPT_ASSERT_MSG(Permutable<I>(),
                        "The iterator type of the range passed to action::random_shuffle must allow its "
                        "elements to be permuted; that is, the values must be movable and the "
                        "iterator must be mutable.");
                }
            #endif
            };

            /// \ingroup group-actions
            /// \relates sort_fn
            /// \sa `action`
            namespace
            {
                constexpr auto&& random_shuffle = static_const<action<random_shuffle_fn>>::value;
            }
        }
        /// @}
    }
}

operator()どこかに深く隠されているものが見つからないためにコンパイルに失敗するLive Example 。

私が見る限り、上記のコードは、action::sort(). 唯一の違いは、 にrandom_shuffle()は 2 つのオーバーロード (1 つはランダム ジェネレーターを取る) があるのに対し、他のすべてのアクション ( を含むsort) にはすべて、追加のパラメーター (コンパレーター、述語、プロジェクターなど) のデフォルト値を持つ単一のオーバーロードがあることです。bind()これは、上記の 2 つの静的メンバー関数に変換されますrandom_shuffle_fnが、他のすべてのアクションには 1 つのオーバーロードしかありませんbind()

質問: random_shuffle の range-v3 アクションの書き方を教えてください。

4

2 に答える 2

3

git の最新バージョンには、既にaction::shuffle. 次のように使用できます。

#include <random>
std::mt19937 gen;
...
v |= action::push_back(view::iota(0, N)) | action::shuffle(gen);
于 2015-05-26T15:39:45.790 に答える