Home | History | Annotate | Download | only in rand.adapt.ibits
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <random>
     11 
     12 // template<class Engine, size_t w, class UIntType>
     13 // class independent_bits_engine
     14 // {
     15 // public:
     16 //     // types
     17 //     typedef UIntType result_type;
     18 
     19 #include <random>
     20 #include <type_traits>
     21 
     22 template <class UIntType, UIntType Min, UIntType Max>
     23 class rand1
     24 {
     25 public:
     26     // types
     27     typedef UIntType result_type;
     28 
     29 private:
     30     result_type x_;
     31 
     32     static_assert(Min < Max, "rand1 invalid parameters");
     33 public:
     34 
     35 #ifdef _LIBCPP_HAS_NO_CONSTEXPR
     36     // Workaround for lack of constexpr in C++03
     37     static const result_type _Min = Min;
     38     static const result_type _Max = Max;
     39 #endif
     40 
     41     static _LIBCPP_CONSTEXPR result_type min() {return Min;}
     42     static _LIBCPP_CONSTEXPR result_type max() {return Max;}
     43 
     44     explicit rand1(result_type sd = Min) : x_(sd)
     45     {
     46         if (x_ < Min)
     47             x_ = Min;
     48         if (x_ > Max)
     49             x_ = Max;
     50     }
     51 
     52     result_type operator()()
     53     {
     54         result_type r = x_;
     55         if (x_ < Max)
     56             ++x_;
     57         else
     58             x_ = Min;
     59         return r;
     60     }
     61 };
     62 
     63 void
     64 test1()
     65 {
     66     static_assert((std::is_same<
     67         std::independent_bits_engine<rand1<unsigned long, 0, 10>, 16, unsigned>::result_type,
     68         unsigned>::value), "");
     69 }
     70 
     71 void
     72 test2()
     73 {
     74     static_assert((std::is_same<
     75         std::independent_bits_engine<rand1<unsigned long, 0, 10>, 16, unsigned long long>::result_type,
     76         unsigned long long>::value), "");
     77 }
     78 
     79 int main()
     80 {
     81     test1();
     82     test2();
     83 }
     84