xref: /freebsd/contrib/llvm-project/libcxx/include/__cxx03/__random/geometric_distribution.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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___CXX03___RANDOM_GEOMETRIC_DISTRIBUTION_H
10 #define _LIBCPP___CXX03___RANDOM_GEOMETRIC_DISTRIBUTION_H
11 
12 #include <__cxx03/__config>
13 #include <__cxx03/__random/is_valid.h>
14 #include <__cxx03/__random/negative_binomial_distribution.h>
15 #include <__cxx03/iosfwd>
16 #include <__cxx03/limits>
17 
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #  pragma GCC system_header
20 #endif
21 
22 _LIBCPP_PUSH_MACROS
23 #include <__cxx03/__undef_macros>
24 
25 _LIBCPP_BEGIN_NAMESPACE_STD
26 
27 template <class _IntType = int>
28 class _LIBCPP_TEMPLATE_VIS geometric_distribution {
29   static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type");
30 
31 public:
32   // types
33   typedef _IntType result_type;
34 
35   class _LIBCPP_TEMPLATE_VIS param_type {
36     double __p_;
37 
38   public:
39     typedef geometric_distribution distribution_type;
40 
__p_(__p)41     _LIBCPP_HIDE_FROM_ABI explicit param_type(double __p = 0.5) : __p_(__p) {}
42 
p()43     _LIBCPP_HIDE_FROM_ABI double p() const { return __p_; }
44 
45     friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
46       return __x.__p_ == __y.__p_;
47     }
48     friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
49   };
50 
51 private:
52   param_type __p_;
53 
54 public:
55   // constructors and reset functions
__p_(__p)56   _LIBCPP_HIDE_FROM_ABI explicit geometric_distribution(double __p = 0.5) : __p_(__p) {}
geometric_distribution(const param_type & __p)57   _LIBCPP_HIDE_FROM_ABI explicit geometric_distribution(const param_type& __p) : __p_(__p) {}
reset()58   _LIBCPP_HIDE_FROM_ABI void reset() {}
59 
60   // generating functions
61   template <class _URNG>
operator()62   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
63     return (*this)(__g, __p_);
64   }
65   template <class _URNG>
operator()66   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p) {
67     return negative_binomial_distribution<result_type>(1, __p.p())(__g);
68   }
69 
70   // property functions
p()71   _LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); }
72 
param()73   _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
param(const param_type & __p)74   _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
75 
min()76   _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
max()77   _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::max(); }
78 
79   friend _LIBCPP_HIDE_FROM_ABI bool operator==(const geometric_distribution& __x, const geometric_distribution& __y) {
80     return __x.__p_ == __y.__p_;
81   }
82   friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const geometric_distribution& __x, const geometric_distribution& __y) {
83     return !(__x == __y);
84   }
85 };
86 
87 template <class _CharT, class _Traits, class _IntType>
88 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
89 operator<<(basic_ostream<_CharT, _Traits>& __os, const geometric_distribution<_IntType>& __x) {
90   __save_flags<_CharT, _Traits> __lx(__os);
91   typedef basic_ostream<_CharT, _Traits> _OStream;
92   __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
93   return __os << __x.p();
94 }
95 
96 template <class _CharT, class _Traits, class _IntType>
97 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
98 operator>>(basic_istream<_CharT, _Traits>& __is, geometric_distribution<_IntType>& __x) {
99   typedef geometric_distribution<_IntType> _Eng;
100   typedef typename _Eng::param_type param_type;
101   __save_flags<_CharT, _Traits> __lx(__is);
102   typedef basic_istream<_CharT, _Traits> _Istream;
103   __is.flags(_Istream::dec | _Istream::skipws);
104   double __p;
105   __is >> __p;
106   if (!__is.fail())
107     __x.param(param_type(__p));
108   return __is;
109 }
110 
111 _LIBCPP_END_NAMESPACE_STD
112 
113 _LIBCPP_POP_MACROS
114 
115 #endif // _LIBCPP___CXX03___RANDOM_GEOMETRIC_DISTRIBUTION_H
116