mercredi 13 décembre 2017

how to rewrite this code without using boost?

My task is to modify Sergiu Dotenco's Well Equidistributed Long-period Linear (WELL) algorithm code to not use boost (not saying boost is bad, but due to some company's policy i have to remove it).

now, Sergiu's WELL is using boost's mpl library, there are quite some logic behind it. So one way is to read up all those, then naturally i would be able to finish the task. The other way is, replacing bit by bit with some best guess.

I'm on the 2nd way to hope this try-and-error approach would be faster. So far I've successfully replaced boost::mpl::if_ and if_c with std::conditional, but hit error when try to update IsPowerOfTwo and Power2Modulo etc, that's why i'm seeking help there.

Below is the code, how to rewrite it without boost, but only c++17?

/**
    * Conditional expression of type (r & (r - 1)) == 0 which allows to check
    * whether a number @f$r@f$ is of type @f$2^n@f$.
    */
    typedef boost::mpl::equal_to<
        boost::mpl::bitand_<
        boost::mpl::_,
        boost::mpl::minus<boost::mpl::_, boost::mpl::int_<1>
        >
        >,
        boost::mpl::int_<0>
    > IsPowerOfTwo;

    template<class UIntType, UIntType r>
    struct Power2Modulo
    {
        typedef typename boost::mpl::apply<
            IsPowerOfTwo,
            boost::mpl::integral_c<UIntType, r>
        >::type type;

        BOOST_STATIC_ASSERT(type::value);

        template<class T>
        static T calc(T value)
        {
            return value & (r - 1);
        }
    };

If possible, pls give a short example on how to call it? I tried to instantiate IsPowerOfTwo or Power2Modulo in main with

Detail::IsPowerOfTwo p0;     

or

Detail::Power2Modulo<int, 3> p1;

but got compilation error.

I asked a relevant question before and got some suggestion. However, not familiar to metaprogramming and boost, I don't quite get it.




Aucun commentaire:

Enregistrer un commentaire