Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:59

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___RANDOM_GEOMETRIC_DISTRIBUTION_H
0010 #define _LIBCPP___RANDOM_GEOMETRIC_DISTRIBUTION_H
0011 
0012 #include <__config>
0013 #include <__random/is_valid.h>
0014 #include <__random/negative_binomial_distribution.h>
0015 #include <iosfwd>
0016 #include <limits>
0017 
0018 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0019 #  pragma GCC system_header
0020 #endif
0021 
0022 _LIBCPP_PUSH_MACROS
0023 #include <__undef_macros>
0024 
0025 _LIBCPP_BEGIN_NAMESPACE_STD
0026 
0027 template <class _IntType = int>
0028 class _LIBCPP_TEMPLATE_VIS geometric_distribution {
0029   static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type");
0030 
0031 public:
0032   // types
0033   typedef _IntType result_type;
0034 
0035   class _LIBCPP_TEMPLATE_VIS param_type {
0036     double __p_;
0037 
0038   public:
0039     typedef geometric_distribution distribution_type;
0040 
0041     _LIBCPP_HIDE_FROM_ABI explicit param_type(double __p = 0.5) : __p_(__p) {}
0042 
0043     _LIBCPP_HIDE_FROM_ABI double p() const { return __p_; }
0044 
0045     friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
0046       return __x.__p_ == __y.__p_;
0047     }
0048     friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
0049   };
0050 
0051 private:
0052   param_type __p_;
0053 
0054 public:
0055   // constructors and reset functions
0056 #ifndef _LIBCPP_CXX03_LANG
0057   _LIBCPP_HIDE_FROM_ABI geometric_distribution() : geometric_distribution(0.5) {}
0058   _LIBCPP_HIDE_FROM_ABI explicit geometric_distribution(double __p) : __p_(__p) {}
0059 #else
0060   _LIBCPP_HIDE_FROM_ABI explicit geometric_distribution(double __p = 0.5) : __p_(__p) {}
0061 #endif
0062   _LIBCPP_HIDE_FROM_ABI explicit geometric_distribution(const param_type& __p) : __p_(__p) {}
0063   _LIBCPP_HIDE_FROM_ABI void reset() {}
0064 
0065   // generating functions
0066   template <class _URNG>
0067   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
0068     return (*this)(__g, __p_);
0069   }
0070   template <class _URNG>
0071   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p) {
0072     return negative_binomial_distribution<result_type>(1, __p.p())(__g);
0073   }
0074 
0075   // property functions
0076   _LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); }
0077 
0078   _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
0079   _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
0080 
0081   _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
0082   _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::max(); }
0083 
0084   friend _LIBCPP_HIDE_FROM_ABI bool operator==(const geometric_distribution& __x, const geometric_distribution& __y) {
0085     return __x.__p_ == __y.__p_;
0086   }
0087   friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const geometric_distribution& __x, const geometric_distribution& __y) {
0088     return !(__x == __y);
0089   }
0090 };
0091 
0092 template <class _CharT, class _Traits, class _IntType>
0093 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0094 operator<<(basic_ostream<_CharT, _Traits>& __os, const geometric_distribution<_IntType>& __x) {
0095   __save_flags<_CharT, _Traits> __lx(__os);
0096   typedef basic_ostream<_CharT, _Traits> _OStream;
0097   __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
0098   return __os << __x.p();
0099 }
0100 
0101 template <class _CharT, class _Traits, class _IntType>
0102 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
0103 operator>>(basic_istream<_CharT, _Traits>& __is, geometric_distribution<_IntType>& __x) {
0104   typedef geometric_distribution<_IntType> _Eng;
0105   typedef typename _Eng::param_type param_type;
0106   __save_flags<_CharT, _Traits> __lx(__is);
0107   typedef basic_istream<_CharT, _Traits> _Istream;
0108   __is.flags(_Istream::dec | _Istream::skipws);
0109   double __p;
0110   __is >> __p;
0111   if (!__is.fail())
0112     __x.param(param_type(__p));
0113   return __is;
0114 }
0115 
0116 _LIBCPP_END_NAMESPACE_STD
0117 
0118 _LIBCPP_POP_MACROS
0119 
0120 #endif // _LIBCPP___RANDOM_GEOMETRIC_DISTRIBUTION_H