brintos

brintos / llvm-project-archived public Read only

0
0
Text · 7.8 KiB · ba7c2c7 Raw
198 lines · c
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef _LIBCPP___CXX03___RANDOM_INDEPENDENT_BITS_ENGINE_H10#define _LIBCPP___CXX03___RANDOM_INDEPENDENT_BITS_ENGINE_H11 12#include <__cxx03/__config>13#include <__cxx03/__fwd/istream.h>14#include <__cxx03/__fwd/ostream.h>15#include <__cxx03/__random/is_seed_sequence.h>16#include <__cxx03/__random/log2.h>17#include <__cxx03/__type_traits/conditional.h>18#include <__cxx03/__type_traits/enable_if.h>19#include <__cxx03/__type_traits/is_convertible.h>20#include <__cxx03/__utility/move.h>21#include <__cxx03/cstddef>22#include <__cxx03/limits>23 24#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)25#  pragma GCC system_header26#endif27 28_LIBCPP_PUSH_MACROS29#include <__cxx03/__undef_macros>30 31_LIBCPP_BEGIN_NAMESPACE_STD32 33template <class _Engine, size_t __w, class _UIntType>34class _LIBCPP_TEMPLATE_VIS independent_bits_engine {35  template <class _UInt, _UInt _R0, size_t _Wp, size_t _Mp>36  class __get_n {37    static const size_t _Dt = numeric_limits<_UInt>::digits;38    static const size_t _Np = _Wp / _Mp + (_Wp % _Mp != 0);39    static const size_t _W0 = _Wp / _Np;40    static const _UInt _Y0  = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;41 42  public:43    static const size_t value = _R0 - _Y0 > _Y0 / _Np ? _Np + 1 : _Np;44  };45 46public:47  // types48  typedef _UIntType result_type;49 50private:51  _Engine __e_;52 53  static const result_type _Dt = numeric_limits<result_type>::digits;54  static_assert(0 < __w, "independent_bits_engine invalid parameters");55  static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");56 57  typedef typename _Engine::result_type _Engine_result_type;58  typedef __conditional_t<sizeof(_Engine_result_type) <= sizeof(result_type), result_type, _Engine_result_type>59      _Working_result_type;60  static const _Working_result_type _Rp  = _Engine::_Max - _Engine::_Min + _Working_result_type(1);61  static const size_t __m                = __log2<_Working_result_type, _Rp>::value;62  static const size_t __n                = __get_n<_Working_result_type, _Rp, __w, __m>::value;63  static const size_t __w0               = __w / __n;64  static const size_t __n0               = __n - __w % __n;65  static const size_t _WDt               = numeric_limits<_Working_result_type>::digits;66  static const size_t _EDt               = numeric_limits<_Engine_result_type>::digits;67  static const _Working_result_type __y0 = __w0 >= _WDt ? 0 : (_Rp >> __w0) << __w0;68  static const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 : (_Rp >> (__w0 + 1)) << (__w0 + 1);69  static const _Engine_result_type70      __mask0 = __w0 > 0 ? _Engine_result_type(~0) >> (_EDt - __w0) : _Engine_result_type(0);71  static const _Engine_result_type72      __mask1 = __w0 < _EDt - 1 ? _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) : _Engine_result_type(~0);73 74public:75  static const result_type _Min = 0;76  static const result_type _Max = __w == _Dt ? result_type(~0) : (result_type(1) << __w) - result_type(1);77  static_assert(_Min < _Max, "independent_bits_engine invalid parameters");78 79  // engine characteristics80  _LIBCPP_HIDE_FROM_ABI static result_type min() { return _Min; }81  _LIBCPP_HIDE_FROM_ABI static result_type max() { return _Max; }82 83  // constructors and seeding functions84  _LIBCPP_HIDE_FROM_ABI independent_bits_engine() {}85  _LIBCPP_HIDE_FROM_ABI explicit independent_bits_engine(const _Engine& __e) : __e_(__e) {}86  _LIBCPP_HIDE_FROM_ABI explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}87  template <88      class _Sseq,89      __enable_if_t<__is_seed_sequence<_Sseq, independent_bits_engine>::value && !is_convertible<_Sseq, _Engine>::value,90                    int> = 0>91  _LIBCPP_HIDE_FROM_ABI explicit independent_bits_engine(_Sseq& __q) : __e_(__q) {}92  _LIBCPP_HIDE_FROM_ABI void seed() { __e_.seed(); }93  _LIBCPP_HIDE_FROM_ABI void seed(result_type __sd) { __e_.seed(__sd); }94  template <class _Sseq, __enable_if_t<__is_seed_sequence<_Sseq, independent_bits_engine>::value, int> = 0>95  _LIBCPP_HIDE_FROM_ABI void seed(_Sseq& __q) {96    __e_.seed(__q);97  }98 99  // generating functions100  _LIBCPP_HIDE_FROM_ABI result_type operator()() { return __eval(integral_constant<bool, _Rp != 0>()); }101  _LIBCPP_HIDE_FROM_ABI void discard(unsigned long long __z) {102    for (; __z; --__z)103      operator()();104  }105 106  // property functions107  _LIBCPP_HIDE_FROM_ABI const _Engine& base() const _NOEXCEPT { return __e_; }108 109  template <class _Eng, size_t _Wp, class _UInt>110  friend bool operator==(const independent_bits_engine<_Eng, _Wp, _UInt>& __x,111                         const independent_bits_engine<_Eng, _Wp, _UInt>& __y);112 113  template <class _Eng, size_t _Wp, class _UInt>114  friend bool operator!=(const independent_bits_engine<_Eng, _Wp, _UInt>& __x,115                         const independent_bits_engine<_Eng, _Wp, _UInt>& __y);116 117  template <class _CharT, class _Traits, class _Eng, size_t _Wp, class _UInt>118  friend basic_ostream<_CharT, _Traits>&119  operator<<(basic_ostream<_CharT, _Traits>& __os, const independent_bits_engine<_Eng, _Wp, _UInt>& __x);120 121  template <class _CharT, class _Traits, class _Eng, size_t _Wp, class _UInt>122  friend basic_istream<_CharT, _Traits>&123  operator>>(basic_istream<_CharT, _Traits>& __is, independent_bits_engine<_Eng, _Wp, _UInt>& __x);124 125private:126  _LIBCPP_HIDE_FROM_ABI result_type __eval(false_type);127  _LIBCPP_HIDE_FROM_ABI result_type __eval(true_type);128 129  template <size_t __count,130            __enable_if_t<__count< _Dt, int> = 0> _LIBCPP_HIDE_FROM_ABI static result_type __lshift(result_type __x) {131    return __x << __count;132  }133 134  template <size_t __count, __enable_if_t<(__count >= _Dt), int> = 0>135  _LIBCPP_HIDE_FROM_ABI static result_type __lshift(result_type) {136    return result_type(0);137  }138};139 140template <class _Engine, size_t __w, class _UIntType>141inline _UIntType independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type) {142  return static_cast<result_type>(__e_() & __mask0);143}144 145template <class _Engine, size_t __w, class _UIntType>146_UIntType independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type) {147  result_type __sp = 0;148  for (size_t __k = 0; __k < __n0; ++__k) {149    _Engine_result_type __u;150    do {151      __u = __e_() - _Engine::min();152    } while (__u >= __y0);153    __sp = static_cast<result_type>(__lshift<__w0>(__sp) + (__u & __mask0));154  }155  for (size_t __k = __n0; __k < __n; ++__k) {156    _Engine_result_type __u;157    do {158      __u = __e_() - _Engine::min();159    } while (__u >= __y1);160    __sp = static_cast<result_type>(__lshift<__w0 + 1>(__sp) + (__u & __mask1));161  }162  return __sp;163}164 165template <class _Eng, size_t _Wp, class _UInt>166inline _LIBCPP_HIDE_FROM_ABI bool167operator==(const independent_bits_engine<_Eng, _Wp, _UInt>& __x, const independent_bits_engine<_Eng, _Wp, _UInt>& __y) {168  return __x.base() == __y.base();169}170 171template <class _Eng, size_t _Wp, class _UInt>172inline _LIBCPP_HIDE_FROM_ABI bool173operator!=(const independent_bits_engine<_Eng, _Wp, _UInt>& __x, const independent_bits_engine<_Eng, _Wp, _UInt>& __y) {174  return !(__x == __y);175}176 177template <class _CharT, class _Traits, class _Eng, size_t _Wp, class _UInt>178_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&179operator<<(basic_ostream<_CharT, _Traits>& __os, const independent_bits_engine<_Eng, _Wp, _UInt>& __x) {180  return __os << __x.base();181}182 183template <class _CharT, class _Traits, class _Eng, size_t _Wp, class _UInt>184_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&185operator>>(basic_istream<_CharT, _Traits>& __is, independent_bits_engine<_Eng, _Wp, _UInt>& __x) {186  _Eng __e;187  __is >> __e;188  if (!__is.fail())189    __x.__e_ = __e;190  return __is;191}192 193_LIBCPP_END_NAMESPACE_STD194 195_LIBCPP_POP_MACROS196 197#endif // _LIBCPP___CXX03___RANDOM_INDEPENDENT_BITS_ENGINE_H198