1344cef66SArthur O'Dwyer //===----------------------------------------------------------------------===// 2344cef66SArthur O'Dwyer // 3344cef66SArthur O'Dwyer // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4344cef66SArthur O'Dwyer // See https://llvm.org/LICENSE.txt for license information. 5344cef66SArthur O'Dwyer // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6344cef66SArthur O'Dwyer // 7344cef66SArthur O'Dwyer //===----------------------------------------------------------------------===// 8344cef66SArthur O'Dwyer 9344cef66SArthur O'Dwyer #ifndef _LIBCPP___RANDOM_DISCRETE_DISTRIBUTION_H 10344cef66SArthur O'Dwyer #define _LIBCPP___RANDOM_DISCRETE_DISTRIBUTION_H 11344cef66SArthur O'Dwyer 12344cef66SArthur O'Dwyer #include <__algorithm/upper_bound.h> 13344cef66SArthur O'Dwyer #include <__config> 14a3255f21SArthur O'Dwyer #include <__random/is_valid.h> 15344cef66SArthur O'Dwyer #include <__random/uniform_real_distribution.h> 16*2e43a304SNikolas Klauser #include <__vector/vector.h> 17*2e43a304SNikolas Klauser #include <initializer_list> 18344cef66SArthur O'Dwyer #include <iosfwd> 19344cef66SArthur O'Dwyer #include <numeric> 20344cef66SArthur O'Dwyer 21344cef66SArthur O'Dwyer #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 22344cef66SArthur O'Dwyer # pragma GCC system_header 23344cef66SArthur O'Dwyer #endif 24344cef66SArthur O'Dwyer 25344cef66SArthur O'Dwyer _LIBCPP_PUSH_MACROS 26344cef66SArthur O'Dwyer #include <__undef_macros> 27344cef66SArthur O'Dwyer 28344cef66SArthur O'Dwyer _LIBCPP_BEGIN_NAMESPACE_STD 29344cef66SArthur O'Dwyer 30344cef66SArthur O'Dwyer template <class _IntType = int> 319783f28cSLouis Dionne class _LIBCPP_TEMPLATE_VIS discrete_distribution { 3207e984bcSLouis Dionne static_assert(__libcpp_random_is_valid_inttype<_IntType>::value, "IntType must be a supported integer type"); 339783f28cSLouis Dionne 34344cef66SArthur O'Dwyer public: 35344cef66SArthur O'Dwyer // types 36344cef66SArthur O'Dwyer typedef _IntType result_type; 37344cef66SArthur O'Dwyer 389783f28cSLouis Dionne class _LIBCPP_TEMPLATE_VIS param_type { 39344cef66SArthur O'Dwyer vector<double> __p_; 409783f28cSLouis Dionne 41344cef66SArthur O'Dwyer public: 42344cef66SArthur O'Dwyer typedef discrete_distribution distribution_type; 43344cef66SArthur O'Dwyer 449783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI param_type() {} 45344cef66SArthur O'Dwyer template <class _InputIterator> 469783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI param_type(_InputIterator __f, _InputIterator __l) : __p_(__f, __l) { 479783f28cSLouis Dionne __init(); 489783f28cSLouis Dionne } 49344cef66SArthur O'Dwyer #ifndef _LIBCPP_CXX03_LANG 509783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI param_type(initializer_list<double> __wl) : __p_(__wl.begin(), __wl.end()) { __init(); } 51344cef66SArthur O'Dwyer #endif // _LIBCPP_CXX03_LANG 52344cef66SArthur O'Dwyer template <class _UnaryOperation> 539783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI param_type(size_t __nw, double __xmin, double __xmax, _UnaryOperation __fw); 54344cef66SArthur O'Dwyer 5583ce1397SNikolas Klauser _LIBCPP_HIDE_FROM_ABI vector<double> probabilities() const; 56344cef66SArthur O'Dwyer 579783f28cSLouis Dionne friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) { 589783f28cSLouis Dionne return __x.__p_ == __y.__p_; 599783f28cSLouis Dionne } 609783f28cSLouis Dionne friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); } 61344cef66SArthur O'Dwyer 62344cef66SArthur O'Dwyer private: 6383ce1397SNikolas Klauser _LIBCPP_HIDE_FROM_ABI void __init(); 64344cef66SArthur O'Dwyer 65344cef66SArthur O'Dwyer friend class discrete_distribution; 66344cef66SArthur O'Dwyer 67344cef66SArthur O'Dwyer template <class _CharT, class _Traits, class _IT> 689783f28cSLouis Dionne friend basic_ostream<_CharT, _Traits>& 699783f28cSLouis Dionne operator<<(basic_ostream<_CharT, _Traits>& __os, const discrete_distribution<_IT>& __x); 70344cef66SArthur O'Dwyer 71344cef66SArthur O'Dwyer template <class _CharT, class _Traits, class _IT> 729783f28cSLouis Dionne friend basic_istream<_CharT, _Traits>& 739783f28cSLouis Dionne operator>>(basic_istream<_CharT, _Traits>& __is, discrete_distribution<_IT>& __x); 74344cef66SArthur O'Dwyer }; 75344cef66SArthur O'Dwyer 76344cef66SArthur O'Dwyer private: 77344cef66SArthur O'Dwyer param_type __p_; 78344cef66SArthur O'Dwyer 79344cef66SArthur O'Dwyer public: 80344cef66SArthur O'Dwyer // constructor and reset functions 819783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI discrete_distribution() {} 82344cef66SArthur O'Dwyer template <class _InputIterator> 839783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI discrete_distribution(_InputIterator __f, _InputIterator __l) : __p_(__f, __l) {} 84344cef66SArthur O'Dwyer #ifndef _LIBCPP_CXX03_LANG 859783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI discrete_distribution(initializer_list<double> __wl) : __p_(__wl) {} 86344cef66SArthur O'Dwyer #endif // _LIBCPP_CXX03_LANG 87344cef66SArthur O'Dwyer template <class _UnaryOperation> 889783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI discrete_distribution(size_t __nw, double __xmin, double __xmax, _UnaryOperation __fw) 89344cef66SArthur O'Dwyer : __p_(__nw, __xmin, __xmax, __fw) {} 909783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI explicit discrete_distribution(const param_type& __p) : __p_(__p) {} 919783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI void reset() {} 92344cef66SArthur O'Dwyer 93344cef66SArthur O'Dwyer // generating functions 94344cef66SArthur O'Dwyer template <class _URNG> 959783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) { 969783f28cSLouis Dionne return (*this)(__g, __p_); 979783f28cSLouis Dionne } 9883ce1397SNikolas Klauser template <class _URNG> 9983ce1397SNikolas Klauser _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p); 100344cef66SArthur O'Dwyer 101344cef66SArthur O'Dwyer // property functions 1029783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI vector<double> probabilities() const { return __p_.probabilities(); } 103344cef66SArthur O'Dwyer 1049783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; } 1059783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; } 106344cef66SArthur O'Dwyer 1079783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; } 1089783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI result_type max() const { return __p_.__p_.size(); } 109344cef66SArthur O'Dwyer 1109783f28cSLouis Dionne friend _LIBCPP_HIDE_FROM_ABI bool operator==(const discrete_distribution& __x, const discrete_distribution& __y) { 1119783f28cSLouis Dionne return __x.__p_ == __y.__p_; 1129783f28cSLouis Dionne } 1139783f28cSLouis Dionne friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const discrete_distribution& __x, const discrete_distribution& __y) { 1149783f28cSLouis Dionne return !(__x == __y); 1159783f28cSLouis Dionne } 116344cef66SArthur O'Dwyer 117344cef66SArthur O'Dwyer template <class _CharT, class _Traits, class _IT> 1189783f28cSLouis Dionne friend basic_ostream<_CharT, _Traits>& 1199783f28cSLouis Dionne operator<<(basic_ostream<_CharT, _Traits>& __os, const discrete_distribution<_IT>& __x); 120344cef66SArthur O'Dwyer 121344cef66SArthur O'Dwyer template <class _CharT, class _Traits, class _IT> 1229783f28cSLouis Dionne friend basic_istream<_CharT, _Traits>& 1239783f28cSLouis Dionne operator>>(basic_istream<_CharT, _Traits>& __is, discrete_distribution<_IT>& __x); 124344cef66SArthur O'Dwyer }; 125344cef66SArthur O'Dwyer 126344cef66SArthur O'Dwyer template <class _IntType> 127344cef66SArthur O'Dwyer template <class _UnaryOperation> 1289783f28cSLouis Dionne discrete_distribution<_IntType>::param_type::param_type( 1299783f28cSLouis Dionne size_t __nw, double __xmin, double __xmax, _UnaryOperation __fw) { 1309783f28cSLouis Dionne if (__nw > 1) { 131344cef66SArthur O'Dwyer __p_.reserve(__nw - 1); 132344cef66SArthur O'Dwyer double __d = (__xmax - __xmin) / __nw; 133344cef66SArthur O'Dwyer double __d2 = __d / 2; 134344cef66SArthur O'Dwyer for (size_t __k = 0; __k < __nw; ++__k) 135344cef66SArthur O'Dwyer __p_.push_back(__fw(__xmin + __k * __d + __d2)); 136344cef66SArthur O'Dwyer __init(); 137344cef66SArthur O'Dwyer } 138344cef66SArthur O'Dwyer } 139344cef66SArthur O'Dwyer 140344cef66SArthur O'Dwyer template <class _IntType> 1419783f28cSLouis Dionne void discrete_distribution<_IntType>::param_type::__init() { 1429783f28cSLouis Dionne if (!__p_.empty()) { 1439783f28cSLouis Dionne if (__p_.size() > 1) { 14477a00c0dSLouis Dionne double __s = std::accumulate(__p_.begin(), __p_.end(), 0.0); 145344cef66SArthur O'Dwyer for (vector<double>::iterator __i = __p_.begin(), __e = __p_.end(); __i < __e; ++__i) 146344cef66SArthur O'Dwyer *__i /= __s; 147344cef66SArthur O'Dwyer vector<double> __t(__p_.size() - 1); 14877a00c0dSLouis Dionne std::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin()); 149344cef66SArthur O'Dwyer swap(__p_, __t); 1509783f28cSLouis Dionne } else { 151344cef66SArthur O'Dwyer __p_.clear(); 152344cef66SArthur O'Dwyer __p_.shrink_to_fit(); 153344cef66SArthur O'Dwyer } 154344cef66SArthur O'Dwyer } 155344cef66SArthur O'Dwyer } 156344cef66SArthur O'Dwyer 157344cef66SArthur O'Dwyer template <class _IntType> 1589783f28cSLouis Dionne vector<double> discrete_distribution<_IntType>::param_type::probabilities() const { 159344cef66SArthur O'Dwyer size_t __n = __p_.size(); 160344cef66SArthur O'Dwyer vector<double> __p(__n + 1); 16177a00c0dSLouis Dionne std::adjacent_difference(__p_.begin(), __p_.end(), __p.begin()); 162344cef66SArthur O'Dwyer if (__n > 0) 163344cef66SArthur O'Dwyer __p[__n] = 1 - __p_[__n - 1]; 164344cef66SArthur O'Dwyer else 165344cef66SArthur O'Dwyer __p[0] = 1; 166344cef66SArthur O'Dwyer return __p; 167344cef66SArthur O'Dwyer } 168344cef66SArthur O'Dwyer 169344cef66SArthur O'Dwyer template <class _IntType> 170344cef66SArthur O'Dwyer template <class _URNG> 1719783f28cSLouis Dionne _IntType discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p) { 1727624552eSArthur O'Dwyer static_assert(__libcpp_random_is_valid_urng<_URNG>::value, ""); 173344cef66SArthur O'Dwyer uniform_real_distribution<double> __gen; 1749783f28cSLouis Dionne return static_cast<_IntType>(std::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) - __p.__p_.begin()); 175344cef66SArthur O'Dwyer } 176344cef66SArthur O'Dwyer 177344cef66SArthur O'Dwyer template <class _CharT, class _Traits, class _IT> 17880c7e93aSNikolas Klauser _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 1799783f28cSLouis Dionne operator<<(basic_ostream<_CharT, _Traits>& __os, const discrete_distribution<_IT>& __x) { 180344cef66SArthur O'Dwyer __save_flags<_CharT, _Traits> __lx(__os); 181344cef66SArthur O'Dwyer typedef basic_ostream<_CharT, _Traits> _OStream; 1829783f28cSLouis Dionne __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific); 183344cef66SArthur O'Dwyer _CharT __sp = __os.widen(' '); 184344cef66SArthur O'Dwyer __os.fill(__sp); 185344cef66SArthur O'Dwyer size_t __n = __x.__p_.__p_.size(); 186344cef66SArthur O'Dwyer __os << __n; 187344cef66SArthur O'Dwyer for (size_t __i = 0; __i < __n; ++__i) 188344cef66SArthur O'Dwyer __os << __sp << __x.__p_.__p_[__i]; 189344cef66SArthur O'Dwyer return __os; 190344cef66SArthur O'Dwyer } 191344cef66SArthur O'Dwyer 192344cef66SArthur O'Dwyer template <class _CharT, class _Traits, class _IT> 19380c7e93aSNikolas Klauser _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& 1949783f28cSLouis Dionne operator>>(basic_istream<_CharT, _Traits>& __is, discrete_distribution<_IT>& __x) { 195344cef66SArthur O'Dwyer __save_flags<_CharT, _Traits> __lx(__is); 196344cef66SArthur O'Dwyer typedef basic_istream<_CharT, _Traits> _Istream; 197344cef66SArthur O'Dwyer __is.flags(_Istream::dec | _Istream::skipws); 198344cef66SArthur O'Dwyer size_t __n; 199344cef66SArthur O'Dwyer __is >> __n; 200344cef66SArthur O'Dwyer vector<double> __p(__n); 201344cef66SArthur O'Dwyer for (size_t __i = 0; __i < __n; ++__i) 202344cef66SArthur O'Dwyer __is >> __p[__i]; 203344cef66SArthur O'Dwyer if (!__is.fail()) 204344cef66SArthur O'Dwyer swap(__x.__p_.__p_, __p); 205344cef66SArthur O'Dwyer return __is; 206344cef66SArthur O'Dwyer } 207344cef66SArthur O'Dwyer 208344cef66SArthur O'Dwyer _LIBCPP_END_NAMESPACE_STD 209344cef66SArthur O'Dwyer 210344cef66SArthur O'Dwyer _LIBCPP_POP_MACROS 211344cef66SArthur O'Dwyer 212344cef66SArthur O'Dwyer #endif // _LIBCPP___RANDOM_DISCRETE_DISTRIBUTION_H 213