Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef __MicroURNG_dot_hpp__
00033 #define __MicroURNG_dot_hpp__
00034
00035 #include <stdexcept>
00036 #include <limits>
00037
00038 namespace r123{
00078 template<typename CBRNG>
00079 class MicroURNG{
00080
00081
00082
00083
00084
00085 public:
00086 typedef CBRNG cbrng_type;
00087 static const int BITS = 32;
00088 typedef typename cbrng_type::ctr_type ctr_type;
00089 typedef typename cbrng_type::key_type key_type;
00090 typedef typename cbrng_type::ukey_type ukey_type;
00091 typedef typename ctr_type::value_type result_type;
00092
00093 R123_STATIC_ASSERT( std::numeric_limits<result_type>::digits >= BITS, "The result_type must have at least 32 bits" );
00094
00095 result_type operator()(){
00096 if(last_elem == 0){
00097
00098 const size_t W = std::numeric_limits<result_type>::digits;
00099 ctr_type c = c0;
00100 c[c0.size()-1] |= n<<(W-BITS);
00101 rdata = b(c,k);
00102 n++;
00103 last_elem = rdata.size();
00104 }
00105 return rdata[--last_elem];
00106 }
00107 MicroURNG(cbrng_type _b, ctr_type _c0, ukey_type _uk) : b(_b), c0(_c0), k(_uk), n(0), last_elem(0) {
00108 chkhighbits();
00109 }
00110 MicroURNG(ctr_type _c0, ukey_type _uk) : b(), c0(_c0), k(_uk), n(0), last_elem(0) {
00111 chkhighbits();
00112 }
00113 result_type min R123_NO_MACRO_SUBST () const{
00114 return std::numeric_limits<result_type>::min R123_NO_MACRO_SUBST ();
00115 }
00116 result_type max R123_NO_MACRO_SUBST () const{
00117 return std::numeric_limits<result_type>::max R123_NO_MACRO_SUBST ();
00118 }
00119
00120 const ctr_type& counter() const{ return c0; }
00121 void reset(ctr_type _c0, ukey_type _uk){
00122 c0 = _c0;
00123 chkhighbits();
00124 k = _uk;
00125 n = 0;
00126 last_elem = 0;
00127 }
00128
00129 private:
00130 cbrng_type b;
00131 ctr_type c0;
00132 key_type k;
00133 R123_ULONG_LONG n;
00134 size_t last_elem;
00135 ctr_type rdata;
00136 void chkhighbits(){
00137 result_type r = c0[c0.size()-1];
00138 result_type mask = ((uint64_t)std::numeric_limits<result_type>::max R123_NO_MACRO_SUBST ())>>BITS;
00139 if((r&mask) != r)
00140 throw std::runtime_error("MicroURNG: c0, does not have high bits clear");
00141 }
00142 };
00143 }
00144 #endif