[cfe-commits] [libcxx] r103524 - in /libcxx/trunk: include/__split_buffer include/memory include/random include/string test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.bernoulli/eval.pass.cpp

Howard Hinnant hhinnant at apple.com
Tue May 11 16:27:00 PDT 2010


Author: hhinnant
Date: Tue May 11 18:26:59 2010
New Revision: 103524

URL: http://llvm.org/viewvc/llvm-project?rev=103524&view=rev
Log:
[rand.dist.bern.bin].  The evaluation function for this binomial distribution is hopefully just a placeholder.  It is using the simplest and slowest method for computing the distribution and needs to be upgraded.

Modified:
    libcxx/trunk/include/__split_buffer
    libcxx/trunk/include/memory
    libcxx/trunk/include/random
    libcxx/trunk/include/string
    libcxx/trunk/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.bernoulli/eval.pass.cpp

Modified: libcxx/trunk/include/__split_buffer
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__split_buffer?rev=103524&r1=103523&r2=103524&view=diff
==============================================================================
--- libcxx/trunk/include/__split_buffer (original)
+++ libcxx/trunk/include/__split_buffer Tue May 11 18:26:59 2010
@@ -642,5 +642,3 @@
 _LIBCPP_END_NAMESPACE_STD
 
 #endif  // _LIBCPP_SPLIT_BUFFER
-
-// hh 080222 Created

Modified: libcxx/trunk/include/memory
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/memory?rev=103524&r1=103523&r2=103524&view=diff
==============================================================================
--- libcxx/trunk/include/memory (original)
+++ libcxx/trunk/include/memory Tue May 11 18:26:59 2010
@@ -3820,5 +3820,3 @@
 _LIBCPP_END_NAMESPACE_STD
 
 #endif  // _LIBCPP_MEMORY
-
-// hh 060228 Created

Modified: libcxx/trunk/include/random
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/random?rev=103524&r1=103523&r2=103524&view=diff
==============================================================================
--- libcxx/trunk/include/random (original)
+++ libcxx/trunk/include/random Tue May 11 18:26:59 2010
@@ -608,7 +608,62 @@
 };
 
 template<class IntType = int>
-    class binomial_distribution;
+class binomial_distribution
+{
+public:
+    // types
+    typedef IntType result_type;
+
+    class param_type
+    {
+    public:
+        typedef binomial_distribution distribution_type;
+
+        explicit param_type(IntType t = 1, double p = 0.5);
+
+        IntType t() const;
+        double p() const;
+
+        friend bool operator==(const param_type& x, const param_type& y);
+        friend bool operator!=(const param_type& x, const param_type& y);
+    };
+
+    // constructors and reset functions
+    explicit binomial_distribution(IntType t = 1, double p = 0.5);
+    explicit binomial_distribution(const param_type& parm);
+    void reset();
+
+    // generating functions
+    template<class URNG> result_type operator()(URNG& g);
+    template<class URNG> result_type operator()(URNG& g, const param_type& parm);
+
+    // property functions
+    IntType t() const;
+    double p() const;
+
+    param_type param() const;
+    void param(const param_type& parm);
+
+    result_type min() const;
+    result_type max() const;
+
+    friend bool operator==(const binomial_distribution& x,
+                           const binomial_distribution& y);
+    friend bool operator!=(const binomial_distribution& x,
+                           const binomial_distribution& y);
+
+    template <class charT, class traits>
+    friend
+    basic_ostream<charT, traits>&
+    operator<<(basic_ostream<charT, traits>& os,
+               const binomial_distribution& x);
+    
+    template <class charT, class traits>
+    friend
+    basic_istream<charT, traits>&
+    operator>>(basic_istream<charT, traits>& is,
+               binomial_distribution& x);
+};
 
 template<class IntType = int>
     class geometric_distribution;
@@ -2797,11 +2852,7 @@
     // generating functions
     template<class _URNG> result_type operator()(_URNG& __g)
         {return (*this)(__g, __p_);}
-    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p)
-        {
-            return _STD::generate_canonical<double, numeric_limits<double>::digits>(__g)
-                   < __p.p();
-        }
+    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
 
     // property functions
     double p() const {return __p_.p();}
@@ -2820,6 +2871,14 @@
         {return !(__x == __y);}
 };
 
+template<class _URNG>
+inline
+bernoulli_distribution::result_type
+bernoulli_distribution::operator()(_URNG& __g, const param_type& __p)
+{
+    return (__g() - __g.min()) < __p.p() * (__g.max() - __g.min() + 1.);
+}
+
 template <class _CharT, class _Traits>
 basic_ostream<_CharT, _Traits>&
 operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
@@ -2846,6 +2905,108 @@
     return __is;
 }
 
+template<class _IntType = int>
+class binomial_distribution
+{
+public:
+    // types
+    typedef _IntType result_type;
+
+    class param_type
+    {
+        result_type __t_;
+        double __p_;
+    public:
+        typedef binomial_distribution distribution_type;
+
+        explicit param_type(result_type __t = 1, double __p = 0.5)
+            : __t_(__t), __p_(__p) {}
+
+        result_type t() const {return __t_;}
+        double p() const {return __p_;}
+
+        friend bool operator==(const param_type& __x, const param_type& __y)
+            {return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;}
+        friend bool operator!=(const param_type& __x, const param_type& __y)
+            {return !(__x == __y);}
+    };
+
+private:
+    param_type __p_;
+
+public:
+    // constructors and reset functions
+    explicit binomial_distribution(result_type __t = 1, double __p = 0.5)
+        : __p_(param_type(__t, __p)) {}
+    explicit binomial_distribution(const param_type& __p) : __p_(__p) {}
+    void reset() {}
+
+    // generating functions
+    template<class _URNG> result_type operator()(_URNG& __g)
+        {return (*this)(__g, __p_);}
+    template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
+
+    // property functions
+    result_type t() const {return __p_.t();}
+    double p() const {return __p_.p();}
+
+    param_type param() const {return __p_;}
+    void param(const param_type& __p) {__p_ = __p;}
+
+    result_type min() const {return 0;}
+    result_type max() const {return t();}
+
+    friend bool operator==(const binomial_distribution& __x,
+                           const binomial_distribution& __y)
+        {return __x.__p_ == __y.__p_;}
+    friend bool operator!=(const binomial_distribution& __x,
+                           const binomial_distribution& __y)
+        {return !(__x == __y);}
+};
+
+template<class _IntType>
+template<class _URNG>
+_IntType
+binomial_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
+{
+    bernoulli_distribution __bd(__p.p());
+    _IntType __r = 0;
+    _IntType __t = __p.t();
+    for (_IntType __i = 0; __i < __t; ++__i)
+        __r += __bd(__g);
+    return __r;
+}
+
+template <class _CharT, class _Traits, class _IntType>
+basic_ostream<_CharT, _Traits>&
+operator<<(basic_ostream<_CharT, _Traits>& __os,
+           const binomial_distribution<_IntType>& __x)
+{
+    __save_flags<_CharT, _Traits> _(__os);
+    __os.flags(ios_base::dec | ios_base::left);
+    _CharT __sp = __os.widen(' ');
+    __os.fill(__sp);
+    return __os << __x.t() << __sp << __x.p();
+}
+
+template <class _CharT, class _Traits, class _IntType>
+basic_istream<_CharT, _Traits>&
+operator>>(basic_istream<_CharT, _Traits>& __is,
+           binomial_distribution<_IntType>& __x)
+{
+    typedef binomial_distribution<_IntType> _Eng;
+    typedef typename _Eng::result_type result_type;
+    typedef typename _Eng::param_type param_type;
+    __save_flags<_CharT, _Traits> _(__is);
+    __is.flags(ios_base::dec | ios_base::skipws);
+    result_type __t;
+    double __p;
+    __is >> __t >> __p;
+    if (!__is.fail())
+        __x.param(param_type(__t, __p));
+    return __is;
+}
+
 _LIBCPP_END_NAMESPACE_STD
 
 #endif  // _LIBCPP_RANDOM

Modified: libcxx/trunk/include/string
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/string?rev=103524&r1=103523&r2=103524&view=diff
==============================================================================
--- libcxx/trunk/include/string (original)
+++ libcxx/trunk/include/string Tue May 11 18:26:59 2010
@@ -3539,5 +3539,3 @@
 _LIBCPP_END_NAMESPACE_STD
 
 #endif  // _LIBCPP_STRING
-
-// hh 060525 Created

Modified: libcxx/trunk/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.bernoulli/eval.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.bernoulli/eval.pass.cpp?rev=103524&r1=103523&r2=103524&view=diff
==============================================================================
--- libcxx/trunk/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.bernoulli/eval.pass.cpp (original)
+++ libcxx/trunk/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.bernoulli/eval.pass.cpp Tue May 11 18:26:59 2010
@@ -19,7 +19,7 @@
 int main()
 {
     {
-        typedef std::uniform_int_distribution<> D;
+        typedef std::bernoulli_distribution D;
         typedef std::minstd_rand0 G;
         G g;
         D d(.75);





More information about the cfe-commits mailing list