Path: blob/main/contrib/llvm-project/libcxx/include/__random/independent_bits_engine.h
35233 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___RANDOM_INDEPENDENT_BITS_ENGINE_H9#define _LIBCPP___RANDOM_INDEPENDENT_BITS_ENGINE_H1011#include <__config>12#include <__fwd/istream.h>13#include <__fwd/ostream.h>14#include <__random/is_seed_sequence.h>15#include <__random/log2.h>16#include <__type_traits/conditional.h>17#include <__type_traits/enable_if.h>18#include <__type_traits/is_convertible.h>19#include <__utility/move.h>20#include <cstddef>21#include <limits>2223#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)24# pragma GCC system_header25#endif2627_LIBCPP_PUSH_MACROS28#include <__undef_macros>2930_LIBCPP_BEGIN_NAMESPACE_STD3132template <class _Engine, size_t __w, class _UIntType>33class _LIBCPP_TEMPLATE_VIS independent_bits_engine {34template <class _UInt, _UInt _R0, size_t _Wp, size_t _Mp>35class __get_n {36static _LIBCPP_CONSTEXPR const size_t _Dt = numeric_limits<_UInt>::digits;37static _LIBCPP_CONSTEXPR const size_t _Np = _Wp / _Mp + (_Wp % _Mp != 0);38static _LIBCPP_CONSTEXPR const size_t _W0 = _Wp / _Np;39static _LIBCPP_CONSTEXPR const _UInt _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;4041public:42static _LIBCPP_CONSTEXPR const size_t value = _R0 - _Y0 > _Y0 / _Np ? _Np + 1 : _Np;43};4445public:46// types47typedef _UIntType result_type;4849private:50_Engine __e_;5152static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;53static_assert(0 < __w, "independent_bits_engine invalid parameters");54static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");5556typedef typename _Engine::result_type _Engine_result_type;57typedef __conditional_t<sizeof(_Engine_result_type) <= sizeof(result_type), result_type, _Engine_result_type>58_Working_result_type;59#ifdef _LIBCPP_CXX03_LANG60static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min + _Working_result_type(1);61#else62static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min() + _Working_result_type(1);63#endif64static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;65static _LIBCPP_CONSTEXPR const size_t __n = __get_n<_Working_result_type, _Rp, __w, __m>::value;66static _LIBCPP_CONSTEXPR const size_t __w0 = __w / __n;67static _LIBCPP_CONSTEXPR const size_t __n0 = __n - __w % __n;68static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;69static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;70static _LIBCPP_CONSTEXPR const _Working_result_type __y0 = __w0 >= _WDt ? 0 : (_Rp >> __w0) << __w0;71static _LIBCPP_CONSTEXPR const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 : (_Rp >> (__w0 + 1)) << (__w0 + 1);72static _LIBCPP_CONSTEXPR const73_Engine_result_type __mask0 = __w0 > 0 ? _Engine_result_type(~0) >> (_EDt - __w0) : _Engine_result_type(0);74static _LIBCPP_CONSTEXPR const _Engine_result_type __mask1 =75__w0 < _EDt - 1 ? _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) : _Engine_result_type(~0);7677public:78static _LIBCPP_CONSTEXPR const result_type _Min = 0;79static _LIBCPP_CONSTEXPR const result_type _Max =80__w == _Dt ? result_type(~0) : (result_type(1) << __w) - result_type(1);81static_assert(_Min < _Max, "independent_bits_engine invalid parameters");8283// engine characteristics84_LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type min() { return _Min; }85_LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type max() { return _Max; }8687// constructors and seeding functions88_LIBCPP_HIDE_FROM_ABI independent_bits_engine() {}89_LIBCPP_HIDE_FROM_ABI explicit independent_bits_engine(const _Engine& __e) : __e_(__e) {}90#ifndef _LIBCPP_CXX03_LANG91_LIBCPP_HIDE_FROM_ABI explicit independent_bits_engine(_Engine&& __e) : __e_(std::move(__e)) {}92#endif // _LIBCPP_CXX03_LANG93_LIBCPP_HIDE_FROM_ABI explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}94template <95class _Sseq,96__enable_if_t<__is_seed_sequence<_Sseq, independent_bits_engine>::value && !is_convertible<_Sseq, _Engine>::value,97int> = 0>98_LIBCPP_HIDE_FROM_ABI explicit independent_bits_engine(_Sseq& __q) : __e_(__q) {}99_LIBCPP_HIDE_FROM_ABI void seed() { __e_.seed(); }100_LIBCPP_HIDE_FROM_ABI void seed(result_type __sd) { __e_.seed(__sd); }101template <class _Sseq, __enable_if_t<__is_seed_sequence<_Sseq, independent_bits_engine>::value, int> = 0>102_LIBCPP_HIDE_FROM_ABI void seed(_Sseq& __q) {103__e_.seed(__q);104}105106// generating functions107_LIBCPP_HIDE_FROM_ABI result_type operator()() { return __eval(integral_constant<bool, _Rp != 0>()); }108_LIBCPP_HIDE_FROM_ABI void discard(unsigned long long __z) {109for (; __z; --__z)110operator()();111}112113// property functions114_LIBCPP_HIDE_FROM_ABI const _Engine& base() const _NOEXCEPT { return __e_; }115116template <class _Eng, size_t _Wp, class _UInt>117friend bool operator==(const independent_bits_engine<_Eng, _Wp, _UInt>& __x,118const independent_bits_engine<_Eng, _Wp, _UInt>& __y);119120template <class _Eng, size_t _Wp, class _UInt>121friend bool operator!=(const independent_bits_engine<_Eng, _Wp, _UInt>& __x,122const independent_bits_engine<_Eng, _Wp, _UInt>& __y);123124template <class _CharT, class _Traits, class _Eng, size_t _Wp, class _UInt>125friend basic_ostream<_CharT, _Traits>&126operator<<(basic_ostream<_CharT, _Traits>& __os, const independent_bits_engine<_Eng, _Wp, _UInt>& __x);127128template <class _CharT, class _Traits, class _Eng, size_t _Wp, class _UInt>129friend basic_istream<_CharT, _Traits>&130operator>>(basic_istream<_CharT, _Traits>& __is, independent_bits_engine<_Eng, _Wp, _UInt>& __x);131132private:133_LIBCPP_HIDE_FROM_ABI result_type __eval(false_type);134_LIBCPP_HIDE_FROM_ABI result_type __eval(true_type);135136template <size_t __count,137__enable_if_t<__count< _Dt, int> = 0> _LIBCPP_HIDE_FROM_ABI static result_type __lshift(result_type __x) {138return __x << __count;139}140141template <size_t __count, __enable_if_t<(__count >= _Dt), int> = 0>142_LIBCPP_HIDE_FROM_ABI static result_type __lshift(result_type) {143return result_type(0);144}145};146147template <class _Engine, size_t __w, class _UIntType>148inline _UIntType independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type) {149return static_cast<result_type>(__e_() & __mask0);150}151152template <class _Engine, size_t __w, class _UIntType>153_UIntType independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type) {154result_type __sp = 0;155for (size_t __k = 0; __k < __n0; ++__k) {156_Engine_result_type __u;157do {158__u = __e_() - _Engine::min();159} while (__u >= __y0);160__sp = static_cast<result_type>(__lshift<__w0>(__sp) + (__u & __mask0));161}162for (size_t __k = __n0; __k < __n; ++__k) {163_Engine_result_type __u;164do {165__u = __e_() - _Engine::min();166} while (__u >= __y1);167__sp = static_cast<result_type>(__lshift<__w0 + 1>(__sp) + (__u & __mask1));168}169return __sp;170}171172template <class _Eng, size_t _Wp, class _UInt>173inline _LIBCPP_HIDE_FROM_ABI bool174operator==(const independent_bits_engine<_Eng, _Wp, _UInt>& __x, const independent_bits_engine<_Eng, _Wp, _UInt>& __y) {175return __x.base() == __y.base();176}177178template <class _Eng, size_t _Wp, class _UInt>179inline _LIBCPP_HIDE_FROM_ABI bool180operator!=(const independent_bits_engine<_Eng, _Wp, _UInt>& __x, const independent_bits_engine<_Eng, _Wp, _UInt>& __y) {181return !(__x == __y);182}183184template <class _CharT, class _Traits, class _Eng, size_t _Wp, class _UInt>185_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&186operator<<(basic_ostream<_CharT, _Traits>& __os, const independent_bits_engine<_Eng, _Wp, _UInt>& __x) {187return __os << __x.base();188}189190template <class _CharT, class _Traits, class _Eng, size_t _Wp, class _UInt>191_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&192operator>>(basic_istream<_CharT, _Traits>& __is, independent_bits_engine<_Eng, _Wp, _UInt>& __x) {193_Eng __e;194__is >> __e;195if (!__is.fail())196__x.__e_ = __e;197return __is;198}199200_LIBCPP_END_NAMESPACE_STD201202_LIBCPP_POP_MACROS203204#endif // _LIBCPP___RANDOM_INDEPENDENT_BITS_ENGINE_H205206207