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-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef _LIBCPP___RANDOM_GEOMETRIC_DISTRIBUTION_H 10 #define _LIBCPP___RANDOM_GEOMETRIC_DISTRIBUTION_H 11 12 #include <__config> 13 #include <__random/negative_binomial_distribution.h> 14 #include <iosfwd> 15 #include <limits> 16 17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 18 #pragma GCC system_header 19 #endif 20 21 _LIBCPP_PUSH_MACROS 22 #include <__undef_macros> 23 24 _LIBCPP_BEGIN_NAMESPACE_STD 25 26 template<class _IntType = int> 27 class _LIBCPP_TEMPLATE_VIS geometric_distribution 28 { 29 public: 30 // types 31 typedef _IntType result_type; 32 33 class _LIBCPP_TEMPLATE_VIS param_type 34 { 35 double __p_; 36 public: 37 typedef geometric_distribution distribution_type; 38 39 _LIBCPP_INLINE_VISIBILITY 40 explicit param_type(double __p = 0.5) : __p_(__p) {} 41 42 _LIBCPP_INLINE_VISIBILITY 43 double p() const {return __p_;} 44 45 friend _LIBCPP_INLINE_VISIBILITY 46 bool operator==(const param_type& __x, const param_type& __y) 47 {return __x.__p_ == __y.__p_;} 48 friend _LIBCPP_INLINE_VISIBILITY 49 bool operator!=(const param_type& __x, const param_type& __y) 50 {return !(__x == __y);} 51 }; 52 53 private: 54 param_type __p_; 55 56 public: 57 // constructors and reset functions 58 #ifndef _LIBCPP_CXX03_LANG 59 _LIBCPP_INLINE_VISIBILITY 60 geometric_distribution() : geometric_distribution(0.5) {} 61 _LIBCPP_INLINE_VISIBILITY 62 explicit geometric_distribution(double __p) 63 : __p_(__p) {} 64 #else 65 _LIBCPP_INLINE_VISIBILITY 66 explicit geometric_distribution(double __p = 0.5) 67 : __p_(__p) {} 68 #endif 69 _LIBCPP_INLINE_VISIBILITY 70 explicit geometric_distribution(const param_type& __p) : __p_(__p) {} 71 _LIBCPP_INLINE_VISIBILITY 72 void reset() {} 73 74 // generating functions 75 template<class _URNG> 76 _LIBCPP_INLINE_VISIBILITY 77 result_type operator()(_URNG& __g) 78 {return (*this)(__g, __p_);} 79 template<class _URNG> 80 _LIBCPP_INLINE_VISIBILITY 81 result_type operator()(_URNG& __g, const param_type& __p) 82 {return negative_binomial_distribution<result_type>(1, __p.p())(__g);} 83 84 // property functions 85 _LIBCPP_INLINE_VISIBILITY 86 double p() const {return __p_.p();} 87 88 _LIBCPP_INLINE_VISIBILITY 89 param_type param() const {return __p_;} 90 _LIBCPP_INLINE_VISIBILITY 91 void param(const param_type& __p) {__p_ = __p;} 92 93 _LIBCPP_INLINE_VISIBILITY 94 result_type min() const {return 0;} 95 _LIBCPP_INLINE_VISIBILITY 96 result_type max() const {return numeric_limits<result_type>::max();} 97 98 friend _LIBCPP_INLINE_VISIBILITY 99 bool operator==(const geometric_distribution& __x, 100 const geometric_distribution& __y) 101 {return __x.__p_ == __y.__p_;} 102 friend _LIBCPP_INLINE_VISIBILITY 103 bool operator!=(const geometric_distribution& __x, 104 const geometric_distribution& __y) 105 {return !(__x == __y);} 106 }; 107 108 template <class _CharT, class _Traits, class _IntType> 109 basic_ostream<_CharT, _Traits>& 110 operator<<(basic_ostream<_CharT, _Traits>& __os, 111 const geometric_distribution<_IntType>& __x) 112 { 113 __save_flags<_CharT, _Traits> __lx(__os); 114 typedef basic_ostream<_CharT, _Traits> _OStream; 115 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | 116 _OStream::scientific); 117 return __os << __x.p(); 118 } 119 120 template <class _CharT, class _Traits, class _IntType> 121 basic_istream<_CharT, _Traits>& 122 operator>>(basic_istream<_CharT, _Traits>& __is, 123 geometric_distribution<_IntType>& __x) 124 { 125 typedef geometric_distribution<_IntType> _Eng; 126 typedef typename _Eng::param_type param_type; 127 __save_flags<_CharT, _Traits> __lx(__is); 128 typedef basic_istream<_CharT, _Traits> _Istream; 129 __is.flags(_Istream::dec | _Istream::skipws); 130 double __p; 131 __is >> __p; 132 if (!__is.fail()) 133 __x.param(param_type(__p)); 134 return __is; 135 } 136 137 _LIBCPP_END_NAMESPACE_STD 138 139 _LIBCPP_POP_MACROS 140 141 #endif // _LIBCPP___RANDOM_GEOMETRIC_DISTRIBUTION_H 142