Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a range-v3 action for random_shuffle?

Using the range-v3 library (by @EricNiebler), makes writing algorithmic code much more compact, e.g. here's how to generate a bunch of random numbers:

#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, ","));
}

Live Example.

However, I would prefer to extend the pipeline with a hypothetical action::random_shuffle() like this

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

Here's my attempt at writing such an action (unfortunately, writing new range-v3 code is quite a bit more verbose than using the library)

#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;
            }
        }
        /// @}
    }
}

Live Example which fails to compile because some operator() deeply hidden somewhere is not being found.

As far as I can see, I faithfully transled the above code from similar code for e.g. the action::sort(). The only difference is that the random_shuffle() has two overloads (one taking a random generator), whereas all the other actions (including sort) all have a single overload with default values for their extra parameters (comparators, predicates, projectors etc.). This translates into two bind() static member functions of random_shuffle_fn above, whereas all other actions only have a single bind() overload.

Question: how to write a range-v3 action for random_shuffle?

like image 915
TemplateRex Avatar asked May 24 '15 15:05

TemplateRex


People also ask

What is Range v3?

Range v3 is a generic library that augments the existing standard library with facilities for working with ranges. A range can be loosely thought of a pair of iterators, although they need not be implemented that way.

Is Range v3 header only?

However the freely available “range-v3” library contains much functionality not (yet) found in the Standard, and being header-only means there is little reason to not use it. (Two of the four examples in this article compile with C++20 supporting compilers, the other two need range-v3.)


2 Answers

You have two ambiguous overloads of random_shuffle_function::operator()(Rng&&), your "error catching" overload needs to be constrained to accept only those arguments that the proper overload rejects (we really need C++ Concepts so I never again have to SFINAE constrain overloads):

#ifndef RANGES_DOXYGEN_INVOKED
template<typename Rng,
    CONCEPT_REQUIRES_(!Concept<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

Also, you need to pipe through action::random_shuffle:

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

DEMO

like image 193
Casey Avatar answered Sep 29 '22 14:09

Casey


The latest version from git already contains action::shuffle. It can be used as follows:

#include <random>
std::mt19937 gen;
...
v |= action::push_back(view::iota(0, N)) | action::shuffle(gen);
like image 26
Floop Avatar answered Sep 29 '22 14:09

Floop