Back to home page

EIC code displayed by LXR

 
 

    


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

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___CXX03___RANDOM_PIECEWISE_CONSTANT_DISTRIBUTION_H
0010 #define _LIBCPP___CXX03___RANDOM_PIECEWISE_CONSTANT_DISTRIBUTION_H
0011 
0012 #include <__cxx03/__algorithm/upper_bound.h>
0013 #include <__cxx03/__config>
0014 #include <__cxx03/__random/is_valid.h>
0015 #include <__cxx03/__random/uniform_real_distribution.h>
0016 #include <__cxx03/iosfwd>
0017 #include <__cxx03/numeric>
0018 #include <__cxx03/vector>
0019 
0020 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0021 #  pragma GCC system_header
0022 #endif
0023 
0024 _LIBCPP_PUSH_MACROS
0025 #include <__cxx03/__undef_macros>
0026 
0027 _LIBCPP_BEGIN_NAMESPACE_STD
0028 
0029 template <class _RealType = double>
0030 class _LIBCPP_TEMPLATE_VIS piecewise_constant_distribution {
0031   static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
0032                 "RealType must be a supported floating-point type");
0033 
0034 public:
0035   // types
0036   typedef _RealType result_type;
0037 
0038   class _LIBCPP_TEMPLATE_VIS param_type {
0039     vector<result_type> __b_;
0040     vector<result_type> __densities_;
0041     vector<result_type> __areas_;
0042 
0043   public:
0044     typedef piecewise_constant_distribution distribution_type;
0045 
0046     _LIBCPP_HIDE_FROM_ABI param_type();
0047     template <class _InputIteratorB, class _InputIteratorW>
0048     _LIBCPP_HIDE_FROM_ABI param_type(_InputIteratorB __f_b, _InputIteratorB __l_b, _InputIteratorW __f_w);
0049 #ifndef _LIBCPP_CXX03_LANG
0050     template <class _UnaryOperation>
0051     _LIBCPP_HIDE_FROM_ABI param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
0052 #endif // _LIBCPP_CXX03_LANG
0053     template <class _UnaryOperation>
0054     _LIBCPP_HIDE_FROM_ABI param_type(size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw);
0055     _LIBCPP_HIDE_FROM_ABI param_type(param_type const&) = default;
0056     _LIBCPP_HIDE_FROM_ABI param_type& operator=(const param_type& __rhs);
0057 
0058     _LIBCPP_HIDE_FROM_ABI vector<result_type> intervals() const { return __b_; }
0059     _LIBCPP_HIDE_FROM_ABI vector<result_type> densities() const { return __densities_; }
0060 
0061     friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
0062       return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;
0063     }
0064     friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
0065 
0066   private:
0067     _LIBCPP_HIDE_FROM_ABI void __init();
0068 
0069     friend class piecewise_constant_distribution;
0070 
0071     template <class _CharT, class _Traits, class _RT>
0072     friend basic_ostream<_CharT, _Traits>&
0073     operator<<(basic_ostream<_CharT, _Traits>& __os, const piecewise_constant_distribution<_RT>& __x);
0074 
0075     template <class _CharT, class _Traits, class _RT>
0076     friend basic_istream<_CharT, _Traits>&
0077     operator>>(basic_istream<_CharT, _Traits>& __is, piecewise_constant_distribution<_RT>& __x);
0078   };
0079 
0080 private:
0081   param_type __p_;
0082 
0083 public:
0084   // constructor and reset functions
0085   _LIBCPP_HIDE_FROM_ABI piecewise_constant_distribution() {}
0086   template <class _InputIteratorB, class _InputIteratorW>
0087   _LIBCPP_HIDE_FROM_ABI
0088   piecewise_constant_distribution(_InputIteratorB __f_b, _InputIteratorB __l_b, _InputIteratorW __f_w)
0089       : __p_(__f_b, __l_b, __f_w) {}
0090 
0091 #ifndef _LIBCPP_CXX03_LANG
0092   template <class _UnaryOperation>
0093   _LIBCPP_HIDE_FROM_ABI piecewise_constant_distribution(initializer_list<result_type> __bl, _UnaryOperation __fw)
0094       : __p_(__bl, __fw) {}
0095 #endif // _LIBCPP_CXX03_LANG
0096 
0097   template <class _UnaryOperation>
0098   _LIBCPP_HIDE_FROM_ABI
0099   piecewise_constant_distribution(size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
0100       : __p_(__nw, __xmin, __xmax, __fw) {}
0101 
0102   _LIBCPP_HIDE_FROM_ABI explicit piecewise_constant_distribution(const param_type& __p) : __p_(__p) {}
0103 
0104   _LIBCPP_HIDE_FROM_ABI void reset() {}
0105 
0106   // generating functions
0107   template <class _URNG>
0108   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
0109     return (*this)(__g, __p_);
0110   }
0111   template <class _URNG>
0112   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
0113 
0114   // property functions
0115   _LIBCPP_HIDE_FROM_ABI vector<result_type> intervals() const { return __p_.intervals(); }
0116   _LIBCPP_HIDE_FROM_ABI vector<result_type> densities() const { return __p_.densities(); }
0117 
0118   _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
0119   _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
0120 
0121   _LIBCPP_HIDE_FROM_ABI result_type min() const { return __p_.__b_.front(); }
0122   _LIBCPP_HIDE_FROM_ABI result_type max() const { return __p_.__b_.back(); }
0123 
0124   friend _LIBCPP_HIDE_FROM_ABI bool
0125   operator==(const piecewise_constant_distribution& __x, const piecewise_constant_distribution& __y) {
0126     return __x.__p_ == __y.__p_;
0127   }
0128   friend _LIBCPP_HIDE_FROM_ABI bool
0129   operator!=(const piecewise_constant_distribution& __x, const piecewise_constant_distribution& __y) {
0130     return !(__x == __y);
0131   }
0132 
0133   template <class _CharT, class _Traits, class _RT>
0134   friend basic_ostream<_CharT, _Traits>&
0135   operator<<(basic_ostream<_CharT, _Traits>& __os, const piecewise_constant_distribution<_RT>& __x);
0136 
0137   template <class _CharT, class _Traits, class _RT>
0138   friend basic_istream<_CharT, _Traits>&
0139   operator>>(basic_istream<_CharT, _Traits>& __is, piecewise_constant_distribution<_RT>& __x);
0140 };
0141 
0142 template <class _RealType>
0143 typename piecewise_constant_distribution<_RealType>::param_type&
0144 piecewise_constant_distribution<_RealType>::param_type::operator=(const param_type& __rhs) {
0145   //  These can throw
0146   __b_.reserve(__rhs.__b_.size());
0147   __densities_.reserve(__rhs.__densities_.size());
0148   __areas_.reserve(__rhs.__areas_.size());
0149 
0150   //  These can not throw
0151   __b_         = __rhs.__b_;
0152   __densities_ = __rhs.__densities_;
0153   __areas_     = __rhs.__areas_;
0154   return *this;
0155 }
0156 
0157 template <class _RealType>
0158 void piecewise_constant_distribution<_RealType>::param_type::__init() {
0159   // __densities_ contains non-normalized areas
0160   result_type __total_area = std::accumulate(__densities_.begin(), __densities_.end(), result_type());
0161   for (size_t __i = 0; __i < __densities_.size(); ++__i)
0162     __densities_[__i] /= __total_area;
0163   // __densities_ contains normalized areas
0164   __areas_.assign(__densities_.size(), result_type());
0165   std::partial_sum(__densities_.begin(), __densities_.end() - 1, __areas_.begin() + 1);
0166   // __areas_ contains partial sums of normalized areas: [0, __densities_ - 1]
0167   __densities_.back() = 1 - __areas_.back(); // correct round off error
0168   for (size_t __i = 0; __i < __densities_.size(); ++__i)
0169     __densities_[__i] /= (__b_[__i + 1] - __b_[__i]);
0170   // __densities_ now contains __densities_
0171 }
0172 
0173 template <class _RealType>
0174 piecewise_constant_distribution<_RealType>::param_type::param_type() : __b_(2), __densities_(1, 1.0), __areas_(1, 0.0) {
0175   __b_[1] = 1;
0176 }
0177 
0178 template <class _RealType>
0179 template <class _InputIteratorB, class _InputIteratorW>
0180 piecewise_constant_distribution<_RealType>::param_type::param_type(
0181     _InputIteratorB __f_b, _InputIteratorB __l_b, _InputIteratorW __f_w)
0182     : __b_(__f_b, __l_b) {
0183   if (__b_.size() < 2) {
0184     __b_.resize(2);
0185     __b_[0] = 0;
0186     __b_[1] = 1;
0187     __densities_.assign(1, 1.0);
0188     __areas_.assign(1, 0.0);
0189   } else {
0190     __densities_.reserve(__b_.size() - 1);
0191     for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__f_w)
0192       __densities_.push_back(*__f_w);
0193     __init();
0194   }
0195 }
0196 
0197 #ifndef _LIBCPP_CXX03_LANG
0198 
0199 template <class _RealType>
0200 template <class _UnaryOperation>
0201 piecewise_constant_distribution<_RealType>::param_type::param_type(
0202     initializer_list<result_type> __bl, _UnaryOperation __fw)
0203     : __b_(__bl.begin(), __bl.end()) {
0204   if (__b_.size() < 2) {
0205     __b_.resize(2);
0206     __b_[0] = 0;
0207     __b_[1] = 1;
0208     __densities_.assign(1, 1.0);
0209     __areas_.assign(1, 0.0);
0210   } else {
0211     __densities_.reserve(__b_.size() - 1);
0212     for (size_t __i = 0; __i < __b_.size() - 1; ++__i)
0213       __densities_.push_back(__fw((__b_[__i + 1] + __b_[__i]) * .5));
0214     __init();
0215   }
0216 }
0217 
0218 #endif // _LIBCPP_CXX03_LANG
0219 
0220 template <class _RealType>
0221 template <class _UnaryOperation>
0222 piecewise_constant_distribution<_RealType>::param_type::param_type(
0223     size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
0224     : __b_(__nw == 0 ? 2 : __nw + 1) {
0225   size_t __n      = __b_.size() - 1;
0226   result_type __d = (__xmax - __xmin) / __n;
0227   __densities_.reserve(__n);
0228   for (size_t __i = 0; __i < __n; ++__i) {
0229     __b_[__i] = __xmin + __i * __d;
0230     __densities_.push_back(__fw(__b_[__i] + __d * .5));
0231   }
0232   __b_[__n] = __xmax;
0233   __init();
0234 }
0235 
0236 template <class _RealType>
0237 template <class _URNG>
0238 _RealType piecewise_constant_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {
0239   static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
0240   typedef uniform_real_distribution<result_type> _Gen;
0241   result_type __u = _Gen()(__g);
0242   ptrdiff_t __k   = std::upper_bound(__p.__areas_.begin(), __p.__areas_.end(), __u) - __p.__areas_.begin() - 1;
0243   return (__u - __p.__areas_[__k]) / __p.__densities_[__k] + __p.__b_[__k];
0244 }
0245 
0246 template <class _CharT, class _Traits, class _RT>
0247 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0248 operator<<(basic_ostream<_CharT, _Traits>& __os, const piecewise_constant_distribution<_RT>& __x) {
0249   __save_flags<_CharT, _Traits> __lx(__os);
0250   typedef basic_ostream<_CharT, _Traits> _OStream;
0251   __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
0252   _CharT __sp = __os.widen(' ');
0253   __os.fill(__sp);
0254   size_t __n = __x.__p_.__b_.size();
0255   __os << __n;
0256   for (size_t __i = 0; __i < __n; ++__i)
0257     __os << __sp << __x.__p_.__b_[__i];
0258   __n = __x.__p_.__densities_.size();
0259   __os << __sp << __n;
0260   for (size_t __i = 0; __i < __n; ++__i)
0261     __os << __sp << __x.__p_.__densities_[__i];
0262   __n = __x.__p_.__areas_.size();
0263   __os << __sp << __n;
0264   for (size_t __i = 0; __i < __n; ++__i)
0265     __os << __sp << __x.__p_.__areas_[__i];
0266   return __os;
0267 }
0268 
0269 template <class _CharT, class _Traits, class _RT>
0270 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
0271 operator>>(basic_istream<_CharT, _Traits>& __is, piecewise_constant_distribution<_RT>& __x) {
0272   typedef piecewise_constant_distribution<_RT> _Eng;
0273   typedef typename _Eng::result_type result_type;
0274   __save_flags<_CharT, _Traits> __lx(__is);
0275   typedef basic_istream<_CharT, _Traits> _Istream;
0276   __is.flags(_Istream::dec | _Istream::skipws);
0277   size_t __n;
0278   __is >> __n;
0279   vector<result_type> __b(__n);
0280   for (size_t __i = 0; __i < __n; ++__i)
0281     __is >> __b[__i];
0282   __is >> __n;
0283   vector<result_type> __densities(__n);
0284   for (size_t __i = 0; __i < __n; ++__i)
0285     __is >> __densities[__i];
0286   __is >> __n;
0287   vector<result_type> __areas(__n);
0288   for (size_t __i = 0; __i < __n; ++__i)
0289     __is >> __areas[__i];
0290   if (!__is.fail()) {
0291     swap(__x.__p_.__b_, __b);
0292     swap(__x.__p_.__densities_, __densities);
0293     swap(__x.__p_.__areas_, __areas);
0294   }
0295   return __is;
0296 }
0297 
0298 _LIBCPP_END_NAMESPACE_STD
0299 
0300 _LIBCPP_POP_MACROS
0301 
0302 #endif // _LIBCPP___CXX03___RANDOM_PIECEWISE_CONSTANT_DISTRIBUTION_H