Changeset 87f625f in mainline


Ignore:
Timestamp:
2018-07-05T21:41:22Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
dcd7804
Parents:
6b81ca5
git-author:
Dzejrou <dzejrou@…> (2018-05-02 20:43:11)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:22)
Message:

cpp: added discard_block_engine adaptor and WIP versions of independent_bits_engine and shuffle_order_engine adaptors

Location:
uspace/lib/cpp/include
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/include/impl/random.hpp

    r6b81ca5 r87f625f  
    327327            bool operator==(const mersenne_twister_engine& rhs) const
    328328            {
     329                if (i_ != rhs.i_)
     330                    return false;
     331
    329332                for (size_t i = 0; i < n; ++i)
    330333                {
     
    532535            bool operator==(const subtract_with_carry_engine& rhs) const
    533536            {
     537                if (i_ != rhs.i_)
     538                    return false;
     539
    534540                for (size_t i = 0; i < r; ++i)
    535541                {
     
    619625
    620626    template<class Engine, size_t p, size_t r>
    621     class discard_block_engine;
     627    class discard_block_engine
     628    {
     629        static_assert(0 < r);
     630        static_assert(r <= p);
     631
     632        public:
     633            using result_type = typename Engine::result_type;
     634
     635            static constexpr size_t block_size = p;
     636            static constexpr size_t used_block = r;
     637
     638            static constexpr result_type min()
     639            {
     640                return Engine::min();
     641            }
     642
     643            static constexpr result_type max()
     644            {
     645                return Engine::max();
     646            }
     647
     648            discard_block_engine()
     649                : engine_{}, n_{}
     650            { /* DUMMY BODY */ }
     651
     652            explicit discard_block_engine(const Engine& e)
     653                : engine_{e}, n_{}
     654            { /* DUMMY BODY */ }
     655
     656            explicit discard_block_engine(Engine&& e)
     657                : engine_{move(e)}, n_{}
     658            { /* DUMMY BODY */ }
     659
     660            explicit discard_block_engine(result_type s)
     661                : engine_{s}, n_{}
     662            { /* DUMMY BODY */ }
     663
     664            template<class Seq>
     665            explicit discard_block_engine(
     666                enable_if_t<aux::is_seed_sequence_v<Seq, result_type>, Seq&> q
     667            )
     668                : engine_{q}, n_{}
     669            { /* DUMMY BODY */ }
     670
     671            void seed()
     672            {
     673                engine_.seed();
     674            }
     675
     676            void seed(result_type s)
     677            {
     678                engine_.seed(s);
     679            }
     680
     681            template<class Seq>
     682            void seed(
     683                enable_if_t<aux::is_seed_sequence_v<Seq, result_type>, Seq&> q
     684            )
     685            {
     686                engine_.seed(q);
     687            }
     688
     689            result_type operator()()
     690            {
     691                if (n_ > static_cast<int>(r))
     692                {
     693                    auto count = p - r;
     694                    for (size_t i = 0; i < count; ++i)
     695                        engine_();
     696                    n_ = 0;
     697                }
     698                ++n_;
     699
     700                return engine_();
     701            }
     702
     703            void discard(unsigned long long z)
     704            {
     705                for (unsigned long long i = 0ULL; i < z; ++i)
     706                    operator()(); // We need to discard our (), not engine's.
     707            }
     708
     709            const Engine& base() const noexcept
     710            {
     711                return engine_;
     712            }
     713
     714            bool operator==(const discard_block_engine& rhs) const
     715            {
     716                return engine_ == rhs.engine_ && n_ == rhs.n_;
     717            }
     718
     719            bool operator!=(const discard_block_engine& rhs) const
     720            {
     721                return !(*this == rhs);
     722            }
     723
     724            template<class Char, class Traits>
     725            basic_ostream<Char, Traits>& operator<<(basic_ostream<Char, Traits>& os) const
     726            {
     727                auto flags = os.flags();
     728                os.flags(ios_base::dec | ios_base::left);
     729
     730                os << n_ << os.widen(' ') << engine_;
     731
     732                os.flags(flags);
     733                return os;
     734            }
     735
     736            template<class Char, class Traits>
     737            basic_istream<Char, Traits>& operator>>(basic_istream<Char, Traits>& is) const
     738            {
     739                auto flags = is.flags();
     740                is.flags(ios_base::dec);
     741
     742                if (!(is >> n_) || !(is >> engine_))
     743                    is.setstate(ios::failbit);
     744
     745                is.flags(flags);
     746                return is;
     747            }
     748
     749        private:
     750            Engine engine_;
     751            int n_;
     752    };
    622753
    623754    /**
     
    626757
    627758    template<class Engine, size_t w, class UIntType>
    628     class independent_bits_engine;
     759    class independent_bits_engine
     760    {
     761        static_assert(0U < w);
     762        /* static_assert(w <= numeric_limits<result_type>::digits); */
     763
     764        public:
     765            using result_type = UIntType;
     766
     767            static constexpr result_type min()
     768            {
     769                return result_type{};
     770            }
     771
     772            static constexpr result_type max()
     773            {
     774                return aux::pow2u(w) - 1;
     775            }
     776
     777            independent_bits_engine()
     778                : engine_{}
     779            { /* DUMMY BODY */ }
     780
     781            explicit independent_bits_engine(const Engine& e)
     782                : engine_{e}
     783            { /* DUMMY BODY */ }
     784
     785            explicit independent_bits_engine(Engine&& e)
     786                : engine_{move(e)}
     787            { /* DUMMY BODY */ }
     788
     789            explicit independent_bits_engine(result_type s)
     790                : engine_{s}
     791            { /* DUMMY BODY */ }
     792
     793            template<class Seq>
     794            explicit independent_bits_engine(
     795                enable_if_t<aux::is_seed_sequence_v<Seq, result_type>, Seq&> q
     796            )
     797                : engine_{q}
     798            { /* DUMMY BODY */ }
     799
     800            void seed()
     801            {
     802                engine_.seed();
     803            }
     804
     805            void seed(result_type s)
     806            {
     807                engine_.seed(s);
     808            }
     809
     810            template<class Seq>
     811            void seed(
     812                enable_if_t<aux::is_seed_sequence_v<Seq, result_type>, Seq&> q
     813            )
     814            {
     815                engine_.seed(q);
     816            }
     817
     818            result_type operator()()
     819            {
     820                /* auto r = engine_.max() - engine_.min() + 1; */
     821                /* auto m = aux::floor(aux::log2(r)); */
     822                // TODO:
     823
     824                return engine_();
     825            }
     826
     827            void discard(unsigned long long z)
     828            {
     829                for (unsigned long long i = 0ULL; i < z; ++i)
     830                    operator()();
     831            }
     832
     833            const Engine& base() const noexcept
     834            {
     835                return engine_;
     836            }
     837
     838            bool operator==(const independent_bits_engine& rhs) const
     839            {
     840                return engine_ == rhs.engine_;
     841            }
     842
     843            bool operator!=(const independent_bits_engine& rhs) const
     844            {
     845                return !(*this == rhs);
     846            }
     847
     848            template<class Char, class Traits>
     849            basic_ostream<Char, Traits>& operator<<(basic_ostream<Char, Traits>& os) const
     850            {
     851                return os << engine_;
     852            }
     853
     854            template<class Char, class Traits>
     855            basic_istream<Char, Traits>& operator>>(basic_istream<Char, Traits>& is) const
     856            {
     857                return is >> engine_;
     858            }
     859
     860        private:
     861            Engine engine_;
     862    };
    629863
    630864    /**
     
    633867
    634868    template<class Engine, size_t k>
    635     class shuffle_order_engine;
     869    class shuffle_order_engine
     870    {
     871        static_assert(0U < k);
     872
     873        public:
     874            using result_type = typename Engine::result_type;
     875
     876            static constexpr size_t table_size = k;
     877
     878            static constexpr result_type min()
     879            {
     880                return Engine::min();
     881            }
     882
     883            static constexpr result_type max()
     884            {
     885                return Engine::max();
     886            }
     887
     888            shuffle_order_engine()
     889                : engine_{}
     890            { /* DUMMY BODY */ }
     891
     892            explicit shuffle_order_engine(const Engine& e)
     893                : engine_{e}
     894            { /* DUMMY BODY */ }
     895
     896            explicit shuffle_order_engine(Engine&& e)
     897                : engine_{move(e)}
     898            { /* DUMMY BODY */ }
     899
     900            explicit shuffle_order_engine(result_type s)
     901                : engine_{s}
     902            { /* DUMMY BODY */ }
     903
     904            template<class Seq>
     905            explicit shuffle_order_engine(
     906                enable_if_t<aux::is_seed_sequence_v<Seq, result_type>, Seq&> q
     907            )
     908                : engine_{q}
     909            { /* DUMMY BODY */ }
     910
     911            void seed()
     912            {
     913                engine_.seed();
     914            }
     915
     916            void seed(result_type s)
     917            {
     918                engine_.seed(s);
     919            }
     920
     921            template<class Seq>
     922            void seed(
     923                enable_if_t<aux::is_seed_sequence_v<Seq, result_type>, Seq&> q
     924            )
     925            {
     926                engine_.seed(q);
     927            }
     928
     929            result_type operator()()
     930            {
     931                // TODO:
     932
     933                return engine_();
     934            }
     935
     936            void discard(unsigned long long z)
     937            {
     938                for (unsigned long long i = 0ULL; i < z; ++i)
     939                    operator()();
     940            }
     941
     942            const Engine& base() const noexcept
     943            {
     944                return engine_;
     945            }
     946
     947            bool operator==(const shuffle_order_engine& rhs) const
     948            {
     949                return engine_ == rhs.engine_;
     950            }
     951
     952            bool operator!=(const shuffle_order_engine& rhs) const
     953            {
     954                return !(*this == rhs);
     955            }
     956
     957            template<class Char, class Traits>
     958            basic_ostream<Char, Traits>& operator<<(basic_ostream<Char, Traits>& os) const
     959            {
     960                return os << engine_;
     961            }
     962
     963            template<class Char, class Traits>
     964            basic_istream<Char, Traits>& operator>>(basic_istream<Char, Traits>& is) const
     965            {
     966                return is >> engine_;
     967            }
     968
     969        private:
     970            Engine engine_;
     971            result_type y_;
     972            result_type table_[k];
     973    };
    636974
    637975    /**
  • uspace/lib/cpp/include/internal/builtins.hpp

    r6b81ca5 r87f625f  
    6969        return static_cast<size_t>(__builtin_ceil(static_cast<double>(val)));
    7070    }
     71
     72    template<class T>
     73    constexpr size_t floor(T val)
     74    {
     75        return static_cast<size_t>(__builtin_floor(static_cast<double>(val)));
     76    }
    7177}
    7278
Note: See TracChangeset for help on using the changeset viewer.