ndnboost: Include boost::iostreams for internal use.
diff --git a/include/ndnboost/random/detail/config.hpp b/include/ndnboost/random/detail/config.hpp
new file mode 100644
index 0000000..0d7f7bb
--- /dev/null
+++ b/include/ndnboost/random/detail/config.hpp
@@ -0,0 +1,18 @@
+/* boost random/detail/config.hpp header file
+ *
+ * Copyright Steven Watanabe 2009
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org for most recent version including documentation.
+ *
+ * $Id: config.hpp 52492 2009-04-19 14:55:57Z steven_watanabe $
+ */
+
+#include <ndnboost/config.hpp>
+
+#if (defined(NDNBOOST_NO_OPERATORS_IN_NAMESPACE) || defined(NDNBOOST_NO_MEMBER_TEMPLATE_FRIENDS)) \
+ && !defined(NDNBOOST_MSVC)
+ #define NDNBOOST_RANDOM_NO_STREAM_OPERATORS
+#endif
diff --git a/include/ndnboost/random/detail/const_mod.hpp b/include/ndnboost/random/detail/const_mod.hpp
new file mode 100644
index 0000000..20277f2
--- /dev/null
+++ b/include/ndnboost/random/detail/const_mod.hpp
@@ -0,0 +1,216 @@
+/* boost random/detail/const_mod.hpp header file
+ *
+ * Copyright Jens Maurer 2000-2001
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org for most recent version including documentation.
+ *
+ * $Id: const_mod.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
+ *
+ * Revision history
+ * 2001-02-18 moved to individual header files
+ */
+
+#ifndef NDNBOOST_RANDOM_CONST_MOD_HPP
+#define NDNBOOST_RANDOM_CONST_MOD_HPP
+
+#include <ndnboost/assert.hpp>
+#include <ndnboost/static_assert.hpp>
+#include <ndnboost/integer_traits.hpp>
+#include <ndnboost/type_traits/make_unsigned.hpp>
+#include <ndnboost/random/detail/large_arithmetic.hpp>
+
+#include <ndnboost/random/detail/disable_warnings.hpp>
+
+namespace ndnboost {
+namespace random {
+
+template<class IntType, IntType m>
+class const_mod
+{
+public:
+ static IntType apply(IntType x)
+ {
+ if(((unsigned_m() - 1) & unsigned_m()) == 0)
+ return (unsigned_type(x)) & (unsigned_m() - 1);
+ else {
+ IntType supress_warnings = (m == 0);
+ NDNBOOST_ASSERT(supress_warnings == 0);
+ return x % (m + supress_warnings);
+ }
+ }
+
+ static IntType add(IntType x, IntType c)
+ {
+ if(((unsigned_m() - 1) & unsigned_m()) == 0)
+ return (unsigned_type(x) + unsigned_type(c)) & (unsigned_m() - 1);
+ else if(c == 0)
+ return x;
+ else if(x < m - c)
+ return x + c;
+ else
+ return x - (m - c);
+ }
+
+ static IntType mult(IntType a, IntType x)
+ {
+ if(((unsigned_m() - 1) & unsigned_m()) == 0)
+ return unsigned_type(a) * unsigned_type(x) & (unsigned_m() - 1);
+ else if(a == 0)
+ return 0;
+ else if(a == 1)
+ return x;
+ else if(m <= traits::const_max/a) // i.e. a*m <= max
+ return mult_small(a, x);
+ else if(traits::is_signed && (m%a < m/a))
+ return mult_schrage(a, x);
+ else
+ return mult_general(a, x);
+ }
+
+ static IntType mult_add(IntType a, IntType x, IntType c)
+ {
+ if(((unsigned_m() - 1) & unsigned_m()) == 0)
+ return (unsigned_type(a) * unsigned_type(x) + unsigned_type(c)) & (unsigned_m() - 1);
+ else if(a == 0)
+ return c;
+ else if(m <= (traits::const_max-c)/a) { // i.e. a*m+c <= max
+ IntType supress_warnings = (m == 0);
+ NDNBOOST_ASSERT(supress_warnings == 0);
+ return (a*x+c) % (m + supress_warnings);
+ } else
+ return add(mult(a, x), c);
+ }
+
+ static IntType pow(IntType a, ndnboost::uintmax_t exponent)
+ {
+ IntType result = 1;
+ while(exponent != 0) {
+ if(exponent % 2 == 1) {
+ result = mult(result, a);
+ }
+ a = mult(a, a);
+ exponent /= 2;
+ }
+ return result;
+ }
+
+ static IntType invert(IntType x)
+ { return x == 0 ? 0 : (m == 0? invert_euclidian0(x) : invert_euclidian(x)); }
+
+private:
+ typedef integer_traits<IntType> traits;
+ typedef typename make_unsigned<IntType>::type unsigned_type;
+
+ const_mod(); // don't instantiate
+
+ static IntType mult_small(IntType a, IntType x)
+ {
+ IntType supress_warnings = (m == 0);
+ NDNBOOST_ASSERT(supress_warnings == 0);
+ return a*x % (m + supress_warnings);
+ }
+
+ static IntType mult_schrage(IntType a, IntType value)
+ {
+ const IntType q = m / a;
+ const IntType r = m % a;
+
+ NDNBOOST_ASSERT(r < q); // check that overflow cannot happen
+
+ return sub(a*(value%q), r*(value/q));
+ }
+
+ static IntType mult_general(IntType a, IntType b)
+ {
+ IntType suppress_warnings = (m == 0);
+ NDNBOOST_ASSERT(suppress_warnings == 0);
+ IntType modulus = m + suppress_warnings;
+ NDNBOOST_ASSERT(modulus == m);
+ if(::ndnboost::uintmax_t(modulus) <=
+ (::std::numeric_limits< ::ndnboost::uintmax_t>::max)() / modulus)
+ {
+ return static_cast<IntType>(ndnboost::uintmax_t(a) * b % modulus);
+ } else {
+ return static_cast<IntType>(detail::mulmod(a, b, modulus));
+ }
+ }
+
+ static IntType sub(IntType a, IntType b)
+ {
+ if(a < b)
+ return m - (b - a);
+ else
+ return a - b;
+ }
+
+ static unsigned_type unsigned_m()
+ {
+ if(m == 0) {
+ return unsigned_type((std::numeric_limits<IntType>::max)()) + 1;
+ } else {
+ return unsigned_type(m);
+ }
+ }
+
+ // invert c in the finite field (mod m) (m must be prime)
+ static IntType invert_euclidian(IntType c)
+ {
+ // we are interested in the gcd factor for c, because this is our inverse
+ NDNBOOST_ASSERT(c > 0);
+ IntType l1 = 0;
+ IntType l2 = 1;
+ IntType n = c;
+ IntType p = m;
+ for(;;) {
+ IntType q = p / n;
+ l1 += q * l2;
+ p -= q * n;
+ if(p == 0)
+ return l2;
+ IntType q2 = n / p;
+ l2 += q2 * l1;
+ n -= q2 * p;
+ if(n == 0)
+ return m - l1;
+ }
+ }
+
+ // invert c in the finite field (mod m) (c must be relatively prime to m)
+ static IntType invert_euclidian0(IntType c)
+ {
+ // we are interested in the gcd factor for c, because this is our inverse
+ NDNBOOST_ASSERT(c > 0);
+ if(c == 1) return 1;
+ IntType l1 = 0;
+ IntType l2 = 1;
+ IntType n = c;
+ IntType p = m;
+ IntType max = (std::numeric_limits<IntType>::max)();
+ IntType q = max / n;
+ NDNBOOST_ASSERT(max % n != n - 1 && "c must be relatively prime to m.");
+ l1 += q * l2;
+ p = max - q * n + 1;
+ for(;;) {
+ if(p == 0)
+ return l2;
+ IntType q2 = n / p;
+ l2 += q2 * l1;
+ n -= q2 * p;
+ if(n == 0)
+ return m - l1;
+ q = p / n;
+ l1 += q * l2;
+ p -= q * n;
+ }
+ }
+};
+
+} // namespace random
+} // namespace ndnboost
+
+#include <ndnboost/random/detail/enable_warnings.hpp>
+
+#endif // NDNBOOST_RANDOM_CONST_MOD_HPP
diff --git a/include/ndnboost/random/detail/disable_warnings.hpp b/include/ndnboost/random/detail/disable_warnings.hpp
new file mode 100644
index 0000000..4f707d0
--- /dev/null
+++ b/include/ndnboost/random/detail/disable_warnings.hpp
@@ -0,0 +1,23 @@
+/* boost random/detail/disable_warnings.hpp header file
+ *
+ * Copyright Steven Watanabe 2009
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org for most recent version including documentation.
+ *
+ * $Id: disable_warnings.hpp 60755 2010-03-22 00:45:06Z steven_watanabe $
+ *
+ */
+
+// No #include guard. This header is intended to be included multiple times.
+
+#include <ndnboost/config.hpp>
+
+#ifdef NDNBOOST_MSVC
+#pragma warning(push)
+#pragma warning(disable:4512)
+#pragma warning(disable:4127)
+#pragma warning(disable:4724)
+#endif
diff --git a/include/ndnboost/random/detail/enable_warnings.hpp b/include/ndnboost/random/detail/enable_warnings.hpp
new file mode 100644
index 0000000..68535e2
--- /dev/null
+++ b/include/ndnboost/random/detail/enable_warnings.hpp
@@ -0,0 +1,18 @@
+/* boost random/detail/enable_warnings.hpp header file
+ *
+ * Copyright Steven Watanabe 2009
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org for most recent version including documentation.
+ *
+ * $Id: enable_warnings.hpp 58649 2010-01-02 21:23:17Z steven_watanabe $
+ *
+ */
+
+// No #include guard. This header is intended to be included multiple times.
+
+#ifdef NDNBOOST_MSVC
+#pragma warning(pop)
+#endif
diff --git a/include/ndnboost/random/detail/generator_bits.hpp b/include/ndnboost/random/detail/generator_bits.hpp
new file mode 100644
index 0000000..94d49d6
--- /dev/null
+++ b/include/ndnboost/random/detail/generator_bits.hpp
@@ -0,0 +1,36 @@
+/* boost random/detail/generator_bits.hpp header file
+ *
+ * Copyright Steven Watanabe 2011
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org for most recent version including documentation.
+ *
+ * $Id: generator_bits.hpp 72951 2011-07-07 04:57:37Z steven_watanabe $
+ *
+ */
+
+#ifndef NDNBOOST_RANDOM_DETAIL_GENERATOR_BITS_HPP
+#define NDNBOOST_RANDOM_DETAIL_GENERATOR_BITS_HPP
+
+#include <ndnboost/limits.hpp>
+
+namespace ndnboost {
+namespace random {
+namespace detail {
+
+// This is a temporary measure that retains backwards
+// compatibility.
+template<class URNG>
+struct generator_bits {
+ static std::size_t value() {
+ return std::numeric_limits<typename URNG::result_type>::digits;
+ }
+};
+
+} // namespace detail
+} // namespace random
+} // namespace ndnboost
+
+#endif // NDNBOOST_RANDOM_DETAIL_GENERATOR_BITS_HPP
diff --git a/include/ndnboost/random/detail/integer_log2.hpp b/include/ndnboost/random/detail/integer_log2.hpp
new file mode 100644
index 0000000..f7429af
--- /dev/null
+++ b/include/ndnboost/random/detail/integer_log2.hpp
@@ -0,0 +1,84 @@
+/* boost random/detail/integer_log2.hpp header file
+ *
+ * Copyright Steven Watanabe 2011
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org for most recent version including documentation.
+ *
+ * $Id: integer_log2.hpp 83381 2013-03-09 22:55:05Z eric_niebler $
+ *
+ */
+
+#ifndef NDNBOOST_RANDOM_DETAIL_INTEGER_LOG2_HPP
+#define NDNBOOST_RANDOM_DETAIL_INTEGER_LOG2_HPP
+
+#include <ndnboost/config.hpp>
+#include <ndnboost/limits.hpp>
+#include <ndnboost/pending/integer_log2.hpp>
+
+namespace ndnboost {
+namespace random {
+namespace detail {
+
+#if !defined(NDNBOOST_NO_CXX11_CONSTEXPR)
+#define NDNBOOST_RANDOM_DETAIL_CONSTEXPR constexpr
+#elif defined(NDNBOOST_MSVC)
+#define NDNBOOST_RANDOM_DETAIL_CONSTEXPR __forceinline
+#elif defined(__GNUC__) && __GNUC__ >= 4
+#define NDNBOOST_RANDOM_DETAIL_CONSTEXPR inline __attribute__((const)) __attribute__((always_inline))
+#else
+#define NDNBOOST_RANDOM_DETAIL_CONSTEXPR inline
+#endif
+
+template<int Shift>
+struct integer_log2_impl
+{
+#if defined(NDNBOOST_NO_CXX11_CONSTEXPR)
+ template<class T>
+ NDNBOOST_RANDOM_DETAIL_CONSTEXPR static int apply(T t, int accum)
+ {
+ int update = ((t >> Shift) != 0) * Shift;
+ return integer_log2_impl<Shift / 2>::apply(t >> update, accum + update);
+ }
+#else
+ template<class T>
+ NDNBOOST_RANDOM_DETAIL_CONSTEXPR static int apply2(T t, int accum, int update)
+ {
+ return integer_log2_impl<Shift / 2>::apply(t >> update, accum + update);
+ }
+
+ template<class T>
+ NDNBOOST_RANDOM_DETAIL_CONSTEXPR static int apply(T t, int accum)
+ {
+ return apply2(t, accum, ((t >> Shift) != 0) * Shift);
+ }
+#endif
+};
+
+template<>
+struct integer_log2_impl<1>
+{
+ template<class T>
+ NDNBOOST_RANDOM_DETAIL_CONSTEXPR static int apply(T t, int accum)
+ {
+ return int(t >> 1) + accum;
+ }
+};
+
+template<class T>
+NDNBOOST_RANDOM_DETAIL_CONSTEXPR int integer_log2(T t)
+{
+ return integer_log2_impl<
+ ::ndnboost::detail::max_pow2_less<
+ ::std::numeric_limits<T>::digits, 4
+ >::value
+ >::apply(t, 0);
+}
+
+} // namespace detail
+} // namespace random
+} // namespace ndnboost
+
+#endif // NDNBOOST_RANDOM_DETAIL_INTEGER_LOG2_HPP
diff --git a/include/ndnboost/random/detail/large_arithmetic.hpp b/include/ndnboost/random/detail/large_arithmetic.hpp
new file mode 100644
index 0000000..c9c6434
--- /dev/null
+++ b/include/ndnboost/random/detail/large_arithmetic.hpp
@@ -0,0 +1,122 @@
+/* boost random/detail/large_arithmetic.hpp header file
+ *
+ * Copyright Steven Watanabe 2011
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org for most recent version including documentation.
+ *
+ * $Id: large_arithmetic.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
+ */
+
+#ifndef NDNBOOST_RANDOM_DETAIL_LARGE_ARITHMETIC_HPP
+#define NDNBOOST_RANDOM_DETAIL_LARGE_ARITHMETIC_HPP
+
+#include <ndnboost/cstdint.hpp>
+#include <ndnboost/integer.hpp>
+#include <ndnboost/limits.hpp>
+#include <ndnboost/random/detail/integer_log2.hpp>
+
+#include <ndnboost/random/detail/disable_warnings.hpp>
+
+namespace ndnboost {
+namespace random {
+namespace detail {
+
+struct div_t {
+ ndnboost::uintmax_t quotient;
+ ndnboost::uintmax_t remainder;
+};
+
+inline div_t muldivmod(ndnboost::uintmax_t a, ndnboost::uintmax_t b, ndnboost::uintmax_t m)
+{
+ static const int bits =
+ ::std::numeric_limits< ::ndnboost::uintmax_t>::digits / 2;
+ static const ::ndnboost::uintmax_t mask = (::ndnboost::uintmax_t(1) << bits) - 1;
+ typedef ::ndnboost::uint_t<bits>::fast digit_t;
+
+ int shift = std::numeric_limits< ::ndnboost::uintmax_t>::digits - 1
+ - detail::integer_log2(m);
+
+ a <<= shift;
+ m <<= shift;
+
+ digit_t product[4] = { 0, 0, 0, 0 };
+ digit_t a_[2] = { digit_t(a & mask), digit_t((a >> bits) & mask) };
+ digit_t b_[2] = { digit_t(b & mask), digit_t((b >> bits) & mask) };
+ digit_t m_[2] = { digit_t(m & mask), digit_t((m >> bits) & mask) };
+
+ // multiply a * b
+ for(int i = 0; i < 2; ++i) {
+ digit_t carry = 0;
+ for(int j = 0; j < 2; ++j) {
+ ::ndnboost::uint64_t temp = ::ndnboost::uintmax_t(a_[i]) * b_[j] +
+ carry + product[i + j];
+ product[i + j] = digit_t(temp & mask);
+ carry = digit_t(temp >> bits);
+ }
+ if(carry != 0) {
+ product[i + 2] += carry;
+ }
+ }
+
+ digit_t quotient[2];
+
+ if(m == 0) {
+ div_t result = {
+ ((::ndnboost::uintmax_t(product[3]) << bits) | product[2]),
+ ((::ndnboost::uintmax_t(product[1]) << bits) | product[0]) >> shift,
+ };
+ return result;
+ }
+
+ // divide product / m
+ for(int i = 3; i >= 2; --i) {
+ ::ndnboost::uintmax_t temp =
+ ::ndnboost::uintmax_t(product[i]) << bits | product[i - 1];
+
+ digit_t q = digit_t((product[i] == m_[1]) ? mask : temp / m_[1]);
+
+ ::ndnboost::uintmax_t rem =
+ ((temp - ::ndnboost::uintmax_t(q) * m_[1]) << bits) + product[i - 2];
+
+ ::ndnboost::uintmax_t diff = m_[0] * ::ndnboost::uintmax_t(q);
+
+ int error = 0;
+ if(diff > rem) {
+ if(diff - rem > m) {
+ error = 2;
+ } else {
+ error = 1;
+ }
+ }
+ q -= error;
+ rem = rem + error * m - diff;
+
+ quotient[i - 2] = q;
+ product[i] = 0;
+ product[i-1] = (rem >> bits) & mask;
+ product[i-2] = rem & mask;
+ }
+
+ div_t result = {
+ ((::ndnboost::uintmax_t(quotient[1]) << bits) | quotient[0]),
+ ((::ndnboost::uintmax_t(product[1]) << bits) | product[0]) >> shift,
+ };
+ return result;
+}
+
+inline ndnboost::uintmax_t muldiv(ndnboost::uintmax_t a, ndnboost::uintmax_t b, ndnboost::uintmax_t m)
+{ return detail::muldivmod(a, b, m).quotient; }
+
+inline ndnboost::uintmax_t mulmod(ndnboost::uintmax_t a, ndnboost::uintmax_t b, ndnboost::uintmax_t m)
+{ return detail::muldivmod(a, b, m).remainder; }
+
+} // namespace detail
+} // namespace random
+} // namespace ndnboost
+
+#include <ndnboost/random/detail/enable_warnings.hpp>
+
+#endif // NDNBOOST_RANDOM_DETAIL_LARGE_ARITHMETIC_HPP
diff --git a/include/ndnboost/random/detail/operators.hpp b/include/ndnboost/random/detail/operators.hpp
new file mode 100644
index 0000000..4df4881
--- /dev/null
+++ b/include/ndnboost/random/detail/operators.hpp
@@ -0,0 +1,84 @@
+/* boost random/detail/operators.hpp header file
+ *
+ * Copyright Steven Watanabe 2010-2011
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org for most recent version including documentation.
+ *
+ * $Id: operators.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
+ */
+
+#ifndef NDNBOOST_RANDOM_DETAIL_OPERATORS_HPP
+#define NDNBOOST_RANDOM_DETAIL_OPERATORS_HPP
+
+#include <ndnboost/random/detail/config.hpp>
+#include <ndnboost/detail/workaround.hpp>
+
+#if NDNBOOST_WORKAROUND(NDNBOOST_MSVC, <= 1310) \
+ || NDNBOOST_WORKAROUND(__SUNPRO_CC, NDNBOOST_TESTED_AT(0x5100))
+
+#define NDNBOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, T, t) \
+ template<class CharT, class Traits> \
+ friend std::basic_ostream<CharT,Traits>& \
+ operator<<(std::basic_ostream<CharT,Traits>& os, const T& t) { \
+ t.print(os, t); \
+ return os; \
+ } \
+ template<class CharT, class Traits> \
+ static std::basic_ostream<CharT,Traits>& \
+ print(std::basic_ostream<CharT,Traits>& os, const T& t)
+
+#define NDNBOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, T, t) \
+ template<class CharT, class Traits> \
+ friend std::basic_istream<CharT,Traits>& \
+ operator>>(std::basic_istream<CharT,Traits>& is, T& t) { \
+ t.read(is, t); \
+ return is; \
+ } \
+ template<class CharT, class Traits> \
+ static std::basic_istream<CharT,Traits>& \
+ read(std::basic_istream<CharT,Traits>& is, T& t)
+
+#endif
+
+#if defined(__BORLANDC__)
+
+#define NDNBOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(T, lhs, rhs) \
+ bool operator==(const T& rhs) const \
+ { return T::is_equal(*this, rhs); } \
+ static bool is_equal(const T& lhs, const T& rhs)
+
+#define NDNBOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(T) \
+ bool operator!=(const T& rhs) const \
+ { return !T::is_equal(*this, rhs); }
+
+#endif
+
+#ifndef NDNBOOST_RANDOM_DETAIL_OSTREAM_OPERATOR
+#define NDNBOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, T, t) \
+ template<class CharT, class Traits> \
+ friend std::basic_ostream<CharT,Traits>& \
+ operator<<(std::basic_ostream<CharT,Traits>& os, const T& t)
+#endif
+
+#ifndef NDNBOOST_RANDOM_DETAIL_ISTREAM_OPERATOR
+#define NDNBOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, T, t) \
+ template<class CharT, class Traits> \
+ friend std::basic_istream<CharT,Traits>& \
+ operator>>(std::basic_istream<CharT,Traits>& is, T& t)
+#endif
+
+#ifndef NDNBOOST_RANDOM_DETAIL_EQUALITY_OPERATOR
+#define NDNBOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(T, lhs, rhs) \
+ friend bool operator==(const T& lhs, const T& rhs)
+#endif
+
+#ifndef NDNBOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR
+#define NDNBOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(T) \
+ friend bool operator!=(const T& lhs, const T& rhs) \
+ { return !(lhs == rhs); }
+#endif
+
+#endif
diff --git a/include/ndnboost/random/detail/ptr_helper.hpp b/include/ndnboost/random/detail/ptr_helper.hpp
new file mode 100644
index 0000000..c07a88a
--- /dev/null
+++ b/include/ndnboost/random/detail/ptr_helper.hpp
@@ -0,0 +1,94 @@
+/* boost random/detail/ptr_helper.hpp header file
+ *
+ * Copyright Jens Maurer 2002
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org for most recent version including documentation.
+ *
+ * $Id: ptr_helper.hpp 24096 2004-07-27 03:43:34Z dgregor $
+ *
+ */
+
+#ifndef NDNBOOST_RANDOM_DETAIL_PTR_HELPER_HPP
+#define NDNBOOST_RANDOM_DETAIL_PTR_HELPER_HPP
+
+#include <ndnboost/config.hpp>
+
+
+namespace ndnboost {
+namespace random {
+namespace detail {
+
+// type_traits could help here, but I don't want to depend on type_traits.
+template<class T>
+struct ptr_helper
+{
+ typedef T value_type;
+ typedef T& reference_type;
+ typedef const T& rvalue_type;
+ static reference_type ref(T& r) { return r; }
+ static const T& ref(const T& r) { return r; }
+};
+
+#ifndef NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
+template<class T>
+struct ptr_helper<T&>
+{
+ typedef T value_type;
+ typedef T& reference_type;
+ typedef T& rvalue_type;
+ static reference_type ref(T& r) { return r; }
+ static const T& ref(const T& r) { return r; }
+};
+
+template<class T>
+struct ptr_helper<T*>
+{
+ typedef T value_type;
+ typedef T& reference_type;
+ typedef T* rvalue_type;
+ static reference_type ref(T * p) { return *p; }
+ static const T& ref(const T * p) { return *p; }
+};
+#endif
+
+} // namespace detail
+} // namespace random
+} // namespace ndnboost
+
+//
+// NDNBOOST_RANDOM_PTR_HELPER_SPEC --
+//
+// Helper macro for broken compilers defines specializations of
+// ptr_helper.
+//
+#ifdef NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
+# define NDNBOOST_RANDOM_PTR_HELPER_SPEC(T) \
+namespace ndnboost { namespace random { namespace detail { \
+template<> \
+struct ptr_helper<T&> \
+{ \
+ typedef T value_type; \
+ typedef T& reference_type; \
+ typedef T& rvalue_type; \
+ static reference_type ref(T& r) { return r; } \
+ static const T& ref(const T& r) { return r; } \
+}; \
+ \
+template<> \
+struct ptr_helper<T*> \
+{ \
+ typedef T value_type; \
+ typedef T& reference_type; \
+ typedef T* rvalue_type; \
+ static reference_type ref(T * p) { return *p; } \
+ static const T& ref(const T * p) { return *p; } \
+}; \
+}}}
+#else
+# define NDNBOOST_RANDOM_PTR_HELPER_SPEC(T)
+#endif
+
+#endif // NDNBOOST_RANDOM_DETAIL_PTR_HELPER_HPP
diff --git a/include/ndnboost/random/detail/seed.hpp b/include/ndnboost/random/detail/seed.hpp
new file mode 100644
index 0000000..7f5ac2b
--- /dev/null
+++ b/include/ndnboost/random/detail/seed.hpp
@@ -0,0 +1,114 @@
+/* boost random/detail/seed.hpp header file
+ *
+ * Copyright Steven Watanabe 2009
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org for most recent version including documentation.
+ *
+ * $Id: seed.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
+ */
+
+#ifndef NDNBOOST_RANDOM_DETAIL_SEED_HPP
+#define NDNBOOST_RANDOM_DETAIL_SEED_HPP
+
+#include <ndnboost/config.hpp>
+
+// Sun seems to have trouble with the use of SFINAE for the
+// templated constructor. So does Borland.
+#if !defined(NDNBOOST_NO_SFINAE) && !defined(__SUNPRO_CC) && !defined(__BORLANDC__)
+
+#include <ndnboost/utility/enable_if.hpp>
+#include <ndnboost/type_traits/is_arithmetic.hpp>
+
+namespace ndnboost {
+namespace random {
+namespace detail {
+
+template<class T>
+struct disable_seed : ndnboost::disable_if<ndnboost::is_arithmetic<T> > {};
+
+template<class Engine, class T>
+struct disable_constructor : disable_seed<T> {};
+
+template<class Engine>
+struct disable_constructor<Engine, Engine> {};
+
+#define NDNBOOST_RANDOM_DETAIL_GENERATOR_CONSTRUCTOR(Self, Generator, gen) \
+ template<class Generator> \
+ explicit Self(Generator& gen, typename ::ndnboost::random::detail::disable_constructor<Self, Generator>::type* = 0)
+
+#define NDNBOOST_RANDOM_DETAIL_GENERATOR_SEED(Self, Generator, gen) \
+ template<class Generator> \
+ void seed(Generator& gen, typename ::ndnboost::random::detail::disable_seed<Generator>::type* = 0)
+
+#define NDNBOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(Self, SeedSeq, seq) \
+ template<class SeedSeq> \
+ explicit Self(SeedSeq& seq, typename ::ndnboost::random::detail::disable_constructor<Self, SeedSeq>::type* = 0)
+
+#define NDNBOOST_RANDOM_DETAIL_SEED_SEQ_SEED(Self, SeedSeq, seq) \
+ template<class SeedSeq> \
+ void seed(SeedSeq& seq, typename ::ndnboost::random::detail::disable_seed<SeedSeq>::type* = 0)
+
+#define NDNBOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(Self, T, x) \
+ explicit Self(const T& x)
+
+#define NDNBOOST_RANDOM_DETAIL_ARITHMETIC_SEED(Self, T, x) \
+ void seed(const T& x)
+}
+}
+}
+
+#else
+
+#include <ndnboost/type_traits/is_arithmetic.hpp>
+#include <ndnboost/mpl/bool.hpp>
+
+#define NDNBOOST_RANDOM_DETAIL_GENERATOR_CONSTRUCTOR(Self, Generator, gen) \
+ Self(Self& other) { *this = other; } \
+ Self(const Self& other) { *this = other; } \
+ template<class Generator> \
+ explicit Self(Generator& gen) { \
+ boost_random_constructor_impl(gen, ::ndnboost::is_arithmetic<Generator>());\
+ } \
+ template<class Generator> \
+ void boost_random_constructor_impl(Generator& gen, ::ndnboost::mpl::false_)
+
+#define NDNBOOST_RANDOM_DETAIL_GENERATOR_SEED(Self, Generator, gen) \
+ template<class Generator> \
+ void seed(Generator& gen) { \
+ boost_random_seed_impl(gen, ::ndnboost::is_arithmetic<Generator>());\
+ }\
+ template<class Generator>\
+ void boost_random_seed_impl(Generator& gen, ::ndnboost::mpl::false_)
+
+#define NDNBOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(Self, SeedSeq, seq) \
+ Self(Self& other) { *this = other; } \
+ Self(const Self& other) { *this = other; } \
+ template<class SeedSeq> \
+ explicit Self(SeedSeq& seq) { \
+ boost_random_constructor_impl(seq, ::ndnboost::is_arithmetic<SeedSeq>());\
+ } \
+ template<class SeedSeq> \
+ void boost_random_constructor_impl(SeedSeq& seq, ::ndnboost::mpl::false_)
+
+#define NDNBOOST_RANDOM_DETAIL_SEED_SEQ_SEED(Self, SeedSeq, seq) \
+ template<class SeedSeq> \
+ void seed(SeedSeq& seq) { \
+ boost_random_seed_impl(seq, ::ndnboost::is_arithmetic<SeedSeq>()); \
+ } \
+ template<class SeedSeq> \
+ void boost_random_seed_impl(SeedSeq& seq, ::ndnboost::mpl::false_)
+
+#define NDNBOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(Self, T, x) \
+ explicit Self(const T& x) { boost_random_constructor_impl(x, ::ndnboost::mpl::true_()); }\
+ void boost_random_constructor_impl(const T& x, ::ndnboost::mpl::true_)
+
+#define NDNBOOST_RANDOM_DETAIL_ARITHMETIC_SEED(Self, T, x) \
+ void seed(const T& x) { boost_random_seed_impl(x, ::ndnboost::mpl::true_()); }\
+ void boost_random_seed_impl(const T& x, ::ndnboost::mpl::true_)
+
+#endif
+
+#endif
diff --git a/include/ndnboost/random/detail/seed_impl.hpp b/include/ndnboost/random/detail/seed_impl.hpp
new file mode 100644
index 0000000..7184edc
--- /dev/null
+++ b/include/ndnboost/random/detail/seed_impl.hpp
@@ -0,0 +1,397 @@
+/* boost random/detail/seed.hpp header file
+ *
+ * Copyright Steven Watanabe 2009
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org for most recent version including documentation.
+ *
+ * $Id: seed_impl.hpp 72951 2011-07-07 04:57:37Z steven_watanabe $
+ */
+
+#ifndef NDNBOOST_RANDOM_DETAIL_SEED_IMPL_HPP
+#define NDNBOOST_RANDOM_DETAIL_SEED_IMPL_HPP
+
+#include <stdexcept>
+#include <ndnboost/cstdint.hpp>
+#include <ndnboost/config/no_tr1/cmath.hpp>
+#include <ndnboost/integer/integer_mask.hpp>
+#include <ndnboost/integer/static_log2.hpp>
+#include <ndnboost/type_traits/is_signed.hpp>
+#include <ndnboost/type_traits/is_integral.hpp>
+#include <ndnboost/type_traits/make_unsigned.hpp>
+#include <ndnboost/mpl/bool.hpp>
+#include <ndnboost/mpl/if.hpp>
+#include <ndnboost/mpl/int.hpp>
+#include <ndnboost/random/detail/const_mod.hpp>
+#include <ndnboost/random/detail/integer_log2.hpp>
+#include <ndnboost/random/detail/signed_unsigned_tools.hpp>
+#include <ndnboost/random/detail/generator_bits.hpp>
+
+#include <ndnboost/random/detail/disable_warnings.hpp>
+
+namespace ndnboost {
+namespace random {
+namespace detail {
+
+// finds the seed type of an engine, given its
+// result_type. If the result_type is integral
+// the seed type is the same. If the result_type
+// is floating point, the seed type is uint32_t
+template<class T>
+struct seed_type
+{
+ typedef typename ndnboost::mpl::if_<ndnboost::is_integral<T>,
+ T,
+ ndnboost::uint32_t
+ >::type type;
+};
+
+template<int N>
+struct const_pow_impl
+{
+ template<class T>
+ static T call(T arg, int n, T result)
+ {
+ return const_pow_impl<N / 2>::call(arg * arg, n / 2,
+ n%2 == 0? result : result * arg);
+ }
+};
+
+template<>
+struct const_pow_impl<0>
+{
+ template<class T>
+ static T call(T, int, T result)
+ {
+ return result;
+ }
+};
+
+// requires N is an upper bound on n
+template<int N, class T>
+inline T const_pow(T arg, int n) { return const_pow_impl<N>::call(arg, n, T(1)); }
+
+template<class T>
+inline T pow2(int n)
+{
+ typedef unsigned int_type;
+ const int max_bits = std::numeric_limits<int_type>::digits;
+ T multiplier = T(int_type(1) << (max_bits - 1)) * 2;
+ return (int_type(1) << (n % max_bits)) *
+ const_pow<std::numeric_limits<T>::digits / max_bits>(multiplier, n / max_bits);
+}
+
+template<class Engine, class Iter>
+void generate_from_real(Engine& eng, Iter begin, Iter end)
+{
+ using std::fmod;
+ typedef typename Engine::result_type RealType;
+ const int Bits = detail::generator_bits<Engine>::value();
+ int remaining_bits = 0;
+ ndnboost::uint_least32_t saved_bits = 0;
+ RealType multiplier = pow2<RealType>( Bits);
+ RealType mult32 = RealType(4294967296.0); // 2^32
+ while(true) {
+ RealType val = eng() * multiplier;
+ int available_bits = Bits;
+ // Make sure the compiler can optimize this out
+ // if it isn't possible.
+ if(Bits < 32 && available_bits < 32 - remaining_bits) {
+ saved_bits |= ndnboost::uint_least32_t(val) << remaining_bits;
+ remaining_bits += Bits;
+ } else {
+ // If Bits < 32, then remaining_bits != 0, since
+ // if remaining_bits == 0, available_bits < 32 - 0,
+ // and we won't get here to begin with.
+ if(Bits < 32 || remaining_bits != 0) {
+ ndnboost::uint_least32_t divisor =
+ (ndnboost::uint_least32_t(1) << (32 - remaining_bits));
+ ndnboost::uint_least32_t extra_bits = ndnboost::uint_least32_t(fmod(val, mult32)) & (divisor - 1);
+ val = val / divisor;
+ *begin++ = saved_bits | (extra_bits << remaining_bits);
+ if(begin == end) return;
+ available_bits -= 32 - remaining_bits;
+ remaining_bits = 0;
+ }
+ // If Bits < 32 we should never enter this loop
+ if(Bits >= 32) {
+ for(; available_bits >= 32; available_bits -= 32) {
+ ndnboost::uint_least32_t word = ndnboost::uint_least32_t(fmod(val, mult32));
+ val /= mult32;
+ *begin++ = word;
+ if(begin == end) return;
+ }
+ }
+ remaining_bits = available_bits;
+ saved_bits = static_cast<ndnboost::uint_least32_t>(val);
+ }
+ }
+}
+
+template<class Engine, class Iter>
+void generate_from_int(Engine& eng, Iter begin, Iter end)
+{
+ typedef typename Engine::result_type IntType;
+ typedef typename ndnboost::make_unsigned<IntType>::type unsigned_type;
+ int remaining_bits = 0;
+ ndnboost::uint_least32_t saved_bits = 0;
+ unsigned_type range = ndnboost::random::detail::subtract<IntType>()((eng.max)(), (eng.min)());
+
+ int bits =
+ (range == (std::numeric_limits<unsigned_type>::max)()) ?
+ std::numeric_limits<unsigned_type>::digits :
+ detail::integer_log2(range + 1);
+
+ {
+ int discarded_bits = detail::integer_log2(bits);
+ unsigned_type excess = (range + 1) >> (bits - discarded_bits);
+ if(excess != 0) {
+ int extra_bits = detail::integer_log2((excess - 1) ^ excess);
+ bits = bits - discarded_bits + extra_bits;
+ }
+ }
+
+ unsigned_type mask = (static_cast<unsigned_type>(2) << (bits - 1)) - 1;
+ unsigned_type limit = ((range + 1) & ~mask) - 1;
+
+ while(true) {
+ unsigned_type val;
+ do {
+ val = ndnboost::random::detail::subtract<IntType>()(eng(), (eng.min)());
+ } while(limit != range && val > limit);
+ val &= mask;
+ int available_bits = bits;
+ if(available_bits == 32) {
+ *begin++ = static_cast<ndnboost::uint_least32_t>(val) & 0xFFFFFFFFu;
+ if(begin == end) return;
+ } else if(available_bits % 32 == 0) {
+ for(int i = 0; i < available_bits / 32; ++i) {
+ ndnboost::uint_least32_t word = ndnboost::uint_least32_t(val) & 0xFFFFFFFFu;
+ int supress_warning = (bits >= 32);
+ NDNBOOST_ASSERT(supress_warning == 1);
+ val >>= (32 * supress_warning);
+ *begin++ = word;
+ if(begin == end) return;
+ }
+ } else if(bits < 32 && available_bits < 32 - remaining_bits) {
+ saved_bits |= ndnboost::uint_least32_t(val) << remaining_bits;
+ remaining_bits += bits;
+ } else {
+ if(bits < 32 || remaining_bits != 0) {
+ ndnboost::uint_least32_t extra_bits = ndnboost::uint_least32_t(val) & ((ndnboost::uint_least32_t(1) << (32 - remaining_bits)) - 1);
+ val >>= 32 - remaining_bits;
+ *begin++ = saved_bits | (extra_bits << remaining_bits);
+ if(begin == end) return;
+ available_bits -= 32 - remaining_bits;
+ remaining_bits = 0;
+ }
+ if(bits >= 32) {
+ for(; available_bits >= 32; available_bits -= 32) {
+ ndnboost::uint_least32_t word = ndnboost::uint_least32_t(val) & 0xFFFFFFFFu;
+ int supress_warning = (bits >= 32);
+ NDNBOOST_ASSERT(supress_warning == 1);
+ val >>= (32 * supress_warning);
+ *begin++ = word;
+ if(begin == end) return;
+ }
+ }
+ remaining_bits = available_bits;
+ saved_bits = static_cast<ndnboost::uint_least32_t>(val);
+ }
+ }
+}
+
+template<class Engine, class Iter>
+void generate_impl(Engine& eng, Iter first, Iter last, ndnboost::mpl::true_)
+{
+ return detail::generate_from_int(eng, first, last);
+}
+
+template<class Engine, class Iter>
+void generate_impl(Engine& eng, Iter first, Iter last, ndnboost::mpl::false_)
+{
+ return detail::generate_from_real(eng, first, last);
+}
+
+template<class Engine, class Iter>
+void generate(Engine& eng, Iter first, Iter last)
+{
+ return detail::generate_impl(eng, first, last, ndnboost::is_integral<typename Engine::result_type>());
+}
+
+
+
+template<class IntType, IntType m, class SeedSeq>
+IntType seed_one_int(SeedSeq& seq)
+{
+ static const int log = ::ndnboost::mpl::if_c<(m == 0),
+ ::ndnboost::mpl::int_<(::std::numeric_limits<IntType>::digits)>,
+ ::ndnboost::static_log2<m> >::type::value;
+ static const int k =
+ (log + ((~(static_cast<IntType>(2) << (log - 1)) & m)? 32 : 31)) / 32;
+ ::ndnboost::uint_least32_t array[log / 32 + 4];
+ seq.generate(&array[0], &array[0] + k + 3);
+ IntType s = 0;
+ for(int j = 0; j < k; ++j) {
+ IntType digit = const_mod<IntType, m>::apply(IntType(array[j+3]));
+ IntType mult = IntType(1) << 32*j;
+ s = const_mod<IntType, m>::mult_add(mult, digit, s);
+ }
+ return s;
+}
+
+template<class IntType, IntType m, class Iter>
+IntType get_one_int(Iter& first, Iter last)
+{
+ static const int log = ::ndnboost::mpl::if_c<(m == 0),
+ ::ndnboost::mpl::int_<(::std::numeric_limits<IntType>::digits)>,
+ ::ndnboost::static_log2<m> >::type::value;
+ static const int k =
+ (log + ((~(static_cast<IntType>(2) << (log - 1)) & m)? 32 : 31)) / 32;
+ IntType s = 0;
+ for(int j = 0; j < k; ++j) {
+ if(first == last) {
+ throw ::std::invalid_argument("Not enough elements in call to seed.");
+ }
+ IntType digit = const_mod<IntType, m>::apply(IntType(*first++));
+ IntType mult = IntType(1) << 32*j;
+ s = const_mod<IntType, m>::mult_add(mult, digit, s);
+ }
+ return s;
+}
+
+// TODO: work in-place whenever possible
+template<int w, std::size_t n, class SeedSeq, class UIntType>
+void seed_array_int_impl(SeedSeq& seq, UIntType (&x)[n])
+{
+ ndnboost::uint_least32_t storage[((w+31)/32) * n];
+ seq.generate(&storage[0], &storage[0] + ((w+31)/32) * n);
+ for(std::size_t j = 0; j < n; j++) {
+ UIntType val = 0;
+ for(std::size_t k = 0; k < (w+31)/32; ++k) {
+ val += static_cast<UIntType>(storage[(w+31)/32*j + k]) << 32*k;
+ }
+ x[j] = val & ::ndnboost::low_bits_mask_t<w>::sig_bits;
+ }
+}
+
+template<int w, std::size_t n, class SeedSeq, class IntType>
+inline void seed_array_int_impl(SeedSeq& seq, IntType (&x)[n], ndnboost::mpl::true_)
+{
+ typedef typename ndnboost::make_unsigned<IntType>::type unsigned_array[n];
+ seed_array_int_impl<w>(seq, reinterpret_cast<unsigned_array&>(x));
+}
+
+template<int w, std::size_t n, class SeedSeq, class IntType>
+inline void seed_array_int_impl(SeedSeq& seq, IntType (&x)[n], ndnboost::mpl::false_)
+{
+ seed_array_int_impl<w>(seq, x);
+}
+
+template<int w, std::size_t n, class SeedSeq, class IntType>
+inline void seed_array_int(SeedSeq& seq, IntType (&x)[n])
+{
+ seed_array_int_impl<w>(seq, x, ndnboost::is_signed<IntType>());
+}
+
+template<int w, std::size_t n, class Iter, class UIntType>
+void fill_array_int_impl(Iter& first, Iter last, UIntType (&x)[n])
+{
+ for(std::size_t j = 0; j < n; j++) {
+ UIntType val = 0;
+ for(std::size_t k = 0; k < (w+31)/32; ++k) {
+ if(first == last) {
+ throw std::invalid_argument("Not enough elements in call to seed.");
+ }
+ val += static_cast<UIntType>(*first++) << 32*k;
+ }
+ x[j] = val & ::ndnboost::low_bits_mask_t<w>::sig_bits;
+ }
+}
+
+template<int w, std::size_t n, class Iter, class IntType>
+inline void fill_array_int_impl(Iter& first, Iter last, IntType (&x)[n], ndnboost::mpl::true_)
+{
+ typedef typename ndnboost::make_unsigned<IntType>::type unsigned_array[n];
+ fill_array_int_impl<w>(first, last, reinterpret_cast<unsigned_array&>(x));
+}
+
+template<int w, std::size_t n, class Iter, class IntType>
+inline void fill_array_int_impl(Iter& first, Iter last, IntType (&x)[n], ndnboost::mpl::false_)
+{
+ fill_array_int_impl<w>(first, last, x);
+}
+
+template<int w, std::size_t n, class Iter, class IntType>
+inline void fill_array_int(Iter& first, Iter last, IntType (&x)[n])
+{
+ fill_array_int_impl<w>(first, last, x, ndnboost::is_signed<IntType>());
+}
+
+template<int w, std::size_t n, class RealType>
+void seed_array_real_impl(const ndnboost::uint_least32_t* storage, RealType (&x)[n])
+{
+ ndnboost::uint_least32_t mask = ~((~ndnboost::uint_least32_t(0)) << (w%32));
+ RealType two32 = 4294967296.0;
+ const RealType divisor = RealType(1)/detail::pow2<RealType>(w);
+ unsigned int j;
+ for(j = 0; j < n; ++j) {
+ RealType val = RealType(0);
+ RealType mult = divisor;
+ for(int k = 0; k < w/32; ++k) {
+ val += *storage++ * mult;
+ mult *= two32;
+ }
+ if(mask != 0) {
+ val += (*storage++ & mask) * mult;
+ }
+ NDNBOOST_ASSERT(val >= 0);
+ NDNBOOST_ASSERT(val < 1);
+ x[j] = val;
+ }
+}
+
+template<int w, std::size_t n, class SeedSeq, class RealType>
+void seed_array_real(SeedSeq& seq, RealType (&x)[n])
+{
+ using std::pow;
+ ndnboost::uint_least32_t storage[((w+31)/32) * n];
+ seq.generate(&storage[0], &storage[0] + ((w+31)/32) * n);
+ seed_array_real_impl<w>(storage, x);
+}
+
+template<int w, std::size_t n, class Iter, class RealType>
+void fill_array_real(Iter& first, Iter last, RealType (&x)[n])
+{
+ ndnboost::uint_least32_t mask = ~((~ndnboost::uint_least32_t(0)) << (w%32));
+ RealType two32 = 4294967296.0;
+ const RealType divisor = RealType(1)/detail::pow2<RealType>(w);
+ unsigned int j;
+ for(j = 0; j < n; ++j) {
+ RealType val = RealType(0);
+ RealType mult = divisor;
+ for(int k = 0; k < w/32; ++k, ++first) {
+ if(first == last) throw std::invalid_argument("Not enough elements in call to seed.");
+ val += *first * mult;
+ mult *= two32;
+ }
+ if(mask != 0) {
+ if(first == last) throw std::invalid_argument("Not enough elements in call to seed.");
+ val += (*first & mask) * mult;
+ ++first;
+ }
+ NDNBOOST_ASSERT(val >= 0);
+ NDNBOOST_ASSERT(val < 1);
+ x[j] = val;
+ }
+}
+
+}
+}
+}
+
+#include <ndnboost/random/detail/enable_warnings.hpp>
+
+#endif
diff --git a/include/ndnboost/random/detail/signed_unsigned_tools.hpp b/include/ndnboost/random/detail/signed_unsigned_tools.hpp
new file mode 100644
index 0000000..8fcb1b6
--- /dev/null
+++ b/include/ndnboost/random/detail/signed_unsigned_tools.hpp
@@ -0,0 +1,89 @@
+/* boost random/detail/signed_unsigned_tools.hpp header file
+ *
+ * Copyright Jens Maurer 2006
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org for most recent version including documentation.
+ */
+
+#ifndef NDNBOOST_RANDOM_DETAIL_SIGNED_UNSIGNED_TOOLS
+#define NDNBOOST_RANDOM_DETAIL_SIGNED_UNSIGNED_TOOLS
+
+#include <ndnboost/limits.hpp>
+#include <ndnboost/config.hpp>
+#include <ndnboost/type_traits/make_unsigned.hpp>
+
+namespace ndnboost {
+namespace random {
+namespace detail {
+
+
+/*
+ * Compute x - y, we know that x >= y, return an unsigned value.
+ */
+
+template<class T, bool sgn = std::numeric_limits<T>::is_signed>
+struct subtract { };
+
+template<class T>
+struct subtract<T, /* signed */ false>
+{
+ typedef T result_type;
+ result_type operator()(T x, T y) { return x - y; }
+};
+
+template<class T>
+struct subtract<T, /* signed */ true>
+{
+ typedef typename make_unsigned<T>::type result_type;
+ result_type operator()(T x, T y)
+ {
+ if (y >= 0) // because x >= y, it follows that x >= 0, too
+ return result_type(x) - result_type(y);
+ if (x >= 0) // y < 0
+ // avoid the nasty two's complement case for y == min()
+ return result_type(x) + result_type(-(y+1)) + 1;
+ // both x and y are negative: no signed overflow
+ return result_type(x - y);
+ }
+};
+
+/*
+ * Compute x + y, x is unsigned, result fits in type of "y".
+ */
+
+template<class T1, class T2, bool sgn = std::numeric_limits<T2>::is_signed>
+struct add { };
+
+template<class T1, class T2>
+struct add<T1, T2, /* signed */ false>
+{
+ typedef T2 result_type;
+ result_type operator()(T1 x, T2 y) { return T2(x) + y; }
+};
+
+template<class T1, class T2>
+struct add<T1, T2, /* signed */ true>
+{
+ typedef T2 result_type;
+ result_type operator()(T1 x, T2 y)
+ {
+ if (y >= 0)
+ return T2(x) + y;
+ // y < 0
+ if (x > T1(-(y+1))) // result >= 0 after subtraction
+ // avoid the nasty two's complement edge case for y == min()
+ return T2(x - T1(-(y+1)) - 1);
+ // abs(x) < abs(y), thus T2 able to represent x
+ return T2(x) + y;
+ }
+};
+
+} // namespace detail
+} // namespace random
+} // namespace ndnboost
+
+#endif // NDNBOOST_RANDOM_DETAIL_SIGNED_UNSIGNED_TOOLS
+
diff --git a/include/ndnboost/random/linear_congruential.hpp b/include/ndnboost/random/linear_congruential.hpp
new file mode 100644
index 0000000..b4c69d8
--- /dev/null
+++ b/include/ndnboost/random/linear_congruential.hpp
@@ -0,0 +1,466 @@
+/* boost random/linear_congruential.hpp header file
+ *
+ * Copyright Jens Maurer 2000-2001
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org for most recent version including documentation.
+ *
+ * $Id: linear_congruential.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
+ *
+ * Revision history
+ * 2001-02-18 moved to individual header files
+ */
+
+#ifndef NDNBOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP
+#define NDNBOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP
+
+#include <iostream>
+#include <stdexcept>
+#include <ndnboost/assert.hpp>
+#include <ndnboost/config.hpp>
+#include <ndnboost/cstdint.hpp>
+#include <ndnboost/limits.hpp>
+#include <ndnboost/static_assert.hpp>
+#include <ndnboost/integer/static_log2.hpp>
+#include <ndnboost/mpl/if.hpp>
+#include <ndnboost/type_traits/is_arithmetic.hpp>
+#include <ndnboost/random/detail/config.hpp>
+#include <ndnboost/random/detail/const_mod.hpp>
+#include <ndnboost/random/detail/seed.hpp>
+#include <ndnboost/random/detail/seed_impl.hpp>
+#include <ndnboost/detail/workaround.hpp>
+
+#include <ndnboost/random/detail/disable_warnings.hpp>
+
+namespace ndnboost {
+namespace random {
+
+/**
+ * Instantiations of class template linear_congruential_engine model a
+ * \pseudo_random_number_generator. Linear congruential pseudo-random
+ * number generators are described in:
+ *
+ * @blockquote
+ * "Mathematical methods in large-scale computing units", D. H. Lehmer,
+ * Proc. 2nd Symposium on Large-Scale Digital Calculating Machines,
+ * Harvard University Press, 1951, pp. 141-146
+ * @endblockquote
+ *
+ * Let x(n) denote the sequence of numbers returned by some pseudo-random
+ * number generator. Then for the linear congruential generator,
+ * x(n+1) := (a * x(n) + c) mod m. Parameters for the generator are
+ * x(0), a, c, m. The template parameter IntType shall denote an integral
+ * type. It must be large enough to hold values a, c, and m. The template
+ * parameters a and c must be smaller than m.
+ *
+ * Note: The quality of the generator crucially depends on the choice of
+ * the parameters. User code should use one of the sensibly parameterized
+ * generators such as minstd_rand instead.
+ */
+template<class IntType, IntType a, IntType c, IntType m>
+class linear_congruential_engine
+{
+public:
+ typedef IntType result_type;
+
+ // Required for old Boost.Random concept
+ NDNBOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
+
+ NDNBOOST_STATIC_CONSTANT(IntType, multiplier = a);
+ NDNBOOST_STATIC_CONSTANT(IntType, increment = c);
+ NDNBOOST_STATIC_CONSTANT(IntType, modulus = m);
+ NDNBOOST_STATIC_CONSTANT(IntType, default_seed = 1);
+
+ NDNBOOST_STATIC_ASSERT(std::numeric_limits<IntType>::is_integer);
+ NDNBOOST_STATIC_ASSERT(m == 0 || a < m);
+ NDNBOOST_STATIC_ASSERT(m == 0 || c < m);
+
+ /**
+ * Constructs a @c linear_congruential_engine, using the default seed
+ */
+ linear_congruential_engine() { seed(); }
+
+ /**
+ * Constructs a @c linear_congruential_engine, seeding it with @c x0.
+ */
+ NDNBOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(linear_congruential_engine,
+ IntType, x0)
+ { seed(x0); }
+
+ /**
+ * Constructs a @c linear_congruential_engine, seeding it with values
+ * produced by a call to @c seq.generate().
+ */
+ NDNBOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(linear_congruential_engine,
+ SeedSeq, seq)
+ { seed(seq); }
+
+ /**
+ * Constructs a @c linear_congruential_engine and seeds it
+ * with values taken from the itrator range [first, last)
+ * and adjusts first to point to the element after the last one
+ * used. If there are not enough elements, throws @c std::invalid_argument.
+ *
+ * first and last must be input iterators.
+ */
+ template<class It>
+ linear_congruential_engine(It& first, It last)
+ {
+ seed(first, last);
+ }
+
+ // compiler-generated copy constructor and assignment operator are fine
+
+ /**
+ * Calls seed(default_seed)
+ */
+ void seed() { seed(default_seed); }
+
+ /**
+ * If c mod m is zero and x0 mod m is zero, changes the current value of
+ * the generator to 1. Otherwise, changes it to x0 mod m. If c is zero,
+ * distinct seeds in the range [1,m) will leave the generator in distinct
+ * states. If c is not zero, the range is [0,m).
+ */
+ NDNBOOST_RANDOM_DETAIL_ARITHMETIC_SEED(linear_congruential_engine, IntType, x0)
+ {
+ // wrap _x if it doesn't fit in the destination
+ if(modulus == 0) {
+ _x = x0;
+ } else {
+ _x = x0 % modulus;
+ }
+ // handle negative seeds
+ if(_x <= 0 && _x != 0) {
+ _x += modulus;
+ }
+ // adjust to the correct range
+ if(increment == 0 && _x == 0) {
+ _x = 1;
+ }
+ NDNBOOST_ASSERT(_x >= (min)());
+ NDNBOOST_ASSERT(_x <= (max)());
+ }
+
+ /**
+ * Seeds a @c linear_congruential_engine using values from a SeedSeq.
+ */
+ NDNBOOST_RANDOM_DETAIL_SEED_SEQ_SEED(linear_congruential_engine, SeedSeq, seq)
+ { seed(detail::seed_one_int<IntType, m>(seq)); }
+
+ /**
+ * seeds a @c linear_congruential_engine with values taken
+ * from the itrator range [first, last) and adjusts @c first to
+ * point to the element after the last one used. If there are
+ * not enough elements, throws @c std::invalid_argument.
+ *
+ * @c first and @c last must be input iterators.
+ */
+ template<class It>
+ void seed(It& first, It last)
+ { seed(detail::get_one_int<IntType, m>(first, last)); }
+
+ /**
+ * Returns the smallest value that the @c linear_congruential_engine
+ * can produce.
+ */
+ static result_type min NDNBOOST_PREVENT_MACRO_SUBSTITUTION ()
+ { return c == 0 ? 1 : 0; }
+ /**
+ * Returns the largest value that the @c linear_congruential_engine
+ * can produce.
+ */
+ static result_type max NDNBOOST_PREVENT_MACRO_SUBSTITUTION ()
+ { return modulus-1; }
+
+ /** Returns the next value of the @c linear_congruential_engine. */
+ IntType operator()()
+ {
+ _x = const_mod<IntType, m>::mult_add(a, _x, c);
+ return _x;
+ }
+
+ /** Fills a range with random values */
+ template<class Iter>
+ void generate(Iter first, Iter last)
+ { detail::generate_from_int(*this, first, last); }
+
+ /** Advances the state of the generator by @c z. */
+ void discard(ndnboost::uintmax_t z)
+ {
+ typedef const_mod<IntType, m> mod_type;
+ IntType b_inv = mod_type::invert(a-1);
+ IntType b_gcd = mod_type::mult(a-1, b_inv);
+ if(b_gcd == 1) {
+ IntType a_z = mod_type::pow(a, z);
+ _x = mod_type::mult_add(a_z, _x,
+ mod_type::mult(mod_type::mult(c, b_inv), a_z - 1));
+ } else {
+ // compute (a^z - 1)*c % (b_gcd * m) / (b / b_gcd) * inv(b / b_gcd)
+ // we're storing the intermediate result / b_gcd
+ IntType a_zm1_over_gcd = 0;
+ IntType a_km1_over_gcd = (a - 1) / b_gcd;
+ ndnboost::uintmax_t exponent = z;
+ while(exponent != 0) {
+ if(exponent % 2 == 1) {
+ a_zm1_over_gcd =
+ mod_type::mult_add(
+ b_gcd,
+ mod_type::mult(a_zm1_over_gcd, a_km1_over_gcd),
+ mod_type::add(a_zm1_over_gcd, a_km1_over_gcd));
+ }
+ a_km1_over_gcd = mod_type::mult_add(
+ b_gcd,
+ mod_type::mult(a_km1_over_gcd, a_km1_over_gcd),
+ mod_type::add(a_km1_over_gcd, a_km1_over_gcd));
+ exponent /= 2;
+ }
+
+ IntType a_z = mod_type::mult_add(b_gcd, a_zm1_over_gcd, 1);
+ IntType num = mod_type::mult(c, a_zm1_over_gcd);
+ b_inv = mod_type::invert((a-1)/b_gcd);
+ _x = mod_type::mult_add(a_z, _x, mod_type::mult(b_inv, num));
+ }
+ }
+
+ friend bool operator==(const linear_congruential_engine& x,
+ const linear_congruential_engine& y)
+ { return x._x == y._x; }
+ friend bool operator!=(const linear_congruential_engine& x,
+ const linear_congruential_engine& y)
+ { return !(x == y); }
+
+#if !defined(NDNBOOST_RANDOM_NO_STREAM_OPERATORS)
+ /** Writes a @c linear_congruential_engine to a @c std::ostream. */
+ template<class CharT, class Traits>
+ friend std::basic_ostream<CharT,Traits>&
+ operator<<(std::basic_ostream<CharT,Traits>& os,
+ const linear_congruential_engine& lcg)
+ {
+ return os << lcg._x;
+ }
+
+ /** Reads a @c linear_congruential_engine from a @c std::istream. */
+ template<class CharT, class Traits>
+ friend std::basic_istream<CharT,Traits>&
+ operator>>(std::basic_istream<CharT,Traits>& is,
+ linear_congruential_engine& lcg)
+ {
+ lcg.read(is);
+ return is;
+ }
+#endif
+
+private:
+
+ /// \cond show_private
+
+ template<class CharT, class Traits>
+ void read(std::basic_istream<CharT, Traits>& is) {
+ IntType x;
+ if(is >> x) {
+ if(x >= (min)() && x <= (max)()) {
+ _x = x;
+ } else {
+ is.setstate(std::ios_base::failbit);
+ }
+ }
+ }
+
+ /// \endcond
+
+ IntType _x;
+};
+
+#ifndef NDNBOOST_NO_INCLASS_MEMBER_INITIALIZATION
+// A definition is required even for integral static constants
+template<class IntType, IntType a, IntType c, IntType m>
+const bool linear_congruential_engine<IntType, a, c, m>::has_fixed_range;
+template<class IntType, IntType a, IntType c, IntType m>
+const IntType linear_congruential_engine<IntType,a,c,m>::multiplier;
+template<class IntType, IntType a, IntType c, IntType m>
+const IntType linear_congruential_engine<IntType,a,c,m>::increment;
+template<class IntType, IntType a, IntType c, IntType m>
+const IntType linear_congruential_engine<IntType,a,c,m>::modulus;
+template<class IntType, IntType a, IntType c, IntType m>
+const IntType linear_congruential_engine<IntType,a,c,m>::default_seed;
+#endif
+
+/// \cond show_deprecated
+
+// provided for backwards compatibility
+template<class IntType, IntType a, IntType c, IntType m, IntType val = 0>
+class linear_congruential : public linear_congruential_engine<IntType, a, c, m>
+{
+ typedef linear_congruential_engine<IntType, a, c, m> base_type;
+public:
+ linear_congruential(IntType x0 = 1) : base_type(x0) {}
+ template<class It>
+ linear_congruential(It& first, It last) : base_type(first, last) {}
+};
+
+/// \endcond
+
+/**
+ * The specialization \minstd_rand0 was originally suggested in
+ *
+ * @blockquote
+ * A pseudo-random number generator for the System/360, P.A. Lewis,
+ * A.S. Goodman, J.M. Miller, IBM Systems Journal, Vol. 8, No. 2,
+ * 1969, pp. 136-146
+ * @endblockquote
+ *
+ * It is examined more closely together with \minstd_rand in
+ *
+ * @blockquote
+ * "Random Number Generators: Good ones are hard to find",
+ * Stephen K. Park and Keith W. Miller, Communications of
+ * the ACM, Vol. 31, No. 10, October 1988, pp. 1192-1201
+ * @endblockquote
+ */
+typedef linear_congruential_engine<uint32_t, 16807, 0, 2147483647> minstd_rand0;
+
+/** The specialization \minstd_rand was suggested in
+ *
+ * @blockquote
+ * "Random Number Generators: Good ones are hard to find",
+ * Stephen K. Park and Keith W. Miller, Communications of
+ * the ACM, Vol. 31, No. 10, October 1988, pp. 1192-1201
+ * @endblockquote
+ */
+typedef linear_congruential_engine<uint32_t, 48271, 0, 2147483647> minstd_rand;
+
+
+#if !defined(NDNBOOST_NO_INT64_T) && !defined(NDNBOOST_NO_INTEGRAL_INT64_T)
+/**
+ * Class @c rand48 models a \pseudo_random_number_generator. It uses
+ * the linear congruential algorithm with the parameters a = 0x5DEECE66D,
+ * c = 0xB, m = 2**48. It delivers identical results to the @c lrand48()
+ * function available on some systems (assuming lcong48 has not been called).
+ *
+ * It is only available on systems where @c uint64_t is provided as an
+ * integral type, so that for example static in-class constants and/or
+ * enum definitions with large @c uint64_t numbers work.
+ */
+class rand48
+{
+public:
+ typedef ndnboost::uint32_t result_type;
+
+ NDNBOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
+ /**
+ * Returns the smallest value that the generator can produce
+ */
+ static uint32_t min NDNBOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; }
+ /**
+ * Returns the largest value that the generator can produce
+ */
+ static uint32_t max NDNBOOST_PREVENT_MACRO_SUBSTITUTION ()
+ { return 0x7FFFFFFF; }
+
+ /** Seeds the generator with the default seed. */
+ rand48() : lcf(cnv(static_cast<uint32_t>(1))) {}
+ /**
+ * Constructs a \rand48 generator with x(0) := (x0 << 16) | 0x330e.
+ */
+ NDNBOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(rand48, result_type, x0)
+ { seed(x0); }
+ /**
+ * Seeds the generator with values produced by @c seq.generate().
+ */
+ NDNBOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(rand48, SeedSeq, seq)
+ { seed(seq); }
+ /**
+ * Seeds the generator using values from an iterator range,
+ * and updates first to point one past the last value consumed.
+ */
+ template<class It> rand48(It& first, It last) : lcf(first, last) { }
+
+ // compiler-generated copy ctor and assignment operator are fine
+
+ /** Seeds the generator with the default seed. */
+ void seed() { seed(static_cast<uint32_t>(1)); }
+ /**
+ * Changes the current value x(n) of the generator to (x0 << 16) | 0x330e.
+ */
+ NDNBOOST_RANDOM_DETAIL_ARITHMETIC_SEED(rand48, result_type, x0)
+ { lcf.seed(cnv(x0)); }
+ /**
+ * Seeds the generator using values from an iterator range,
+ * and updates first to point one past the last value consumed.
+ */
+ template<class It> void seed(It& first, It last) { lcf.seed(first,last); }
+ /**
+ * Seeds the generator with values produced by @c seq.generate().
+ */
+ NDNBOOST_RANDOM_DETAIL_SEED_SEQ_SEED(rand48, SeedSeq, seq)
+ { lcf.seed(seq); }
+
+ /** Returns the next value of the generator. */
+ uint32_t operator()() { return static_cast<uint32_t>(lcf() >> 17); }
+
+ /** Advances the state of the generator by @c z. */
+ void discard(ndnboost::uintmax_t z) { lcf.discard(z); }
+
+ /** Fills a range with random values */
+ template<class Iter>
+ void generate(Iter first, Iter last)
+ {
+ for(; first != last; ++first) {
+ *first = (*this)();
+ }
+ }
+
+#ifndef NDNBOOST_RANDOM_NO_STREAM_OPERATORS
+ /** Writes a @c rand48 to a @c std::ostream. */
+ template<class CharT,class Traits>
+ friend std::basic_ostream<CharT,Traits>&
+ operator<<(std::basic_ostream<CharT,Traits>& os, const rand48& r)
+ { os << r.lcf; return os; }
+
+ /** Reads a @c rand48 from a @c std::istream. */
+ template<class CharT,class Traits>
+ friend std::basic_istream<CharT,Traits>&
+ operator>>(std::basic_istream<CharT,Traits>& is, rand48& r)
+ { is >> r.lcf; return is; }
+#endif
+
+ /**
+ * Returns true if the two generators will produce identical
+ * sequences of values.
+ */
+ friend bool operator==(const rand48& x, const rand48& y)
+ { return x.lcf == y.lcf; }
+ /**
+ * Returns true if the two generators will produce different
+ * sequences of values.
+ */
+ friend bool operator!=(const rand48& x, const rand48& y)
+ { return !(x == y); }
+private:
+ /// \cond show_private
+ typedef random::linear_congruential_engine<uint64_t,
+ // xxxxULL is not portable
+ uint64_t(0xDEECE66DUL) | (uint64_t(0x5) << 32),
+ 0xB, uint64_t(1)<<48> lcf_t;
+ lcf_t lcf;
+
+ static ndnboost::uint64_t cnv(ndnboost::uint32_t x)
+ { return (static_cast<uint64_t>(x) << 16) | 0x330e; }
+ /// \endcond
+};
+#endif /* !NDNBOOST_NO_INT64_T && !NDNBOOST_NO_INTEGRAL_INT64_T */
+
+} // namespace random
+
+using random::minstd_rand0;
+using random::minstd_rand;
+using random::rand48;
+
+} // namespace ndnboost
+
+#include <ndnboost/random/detail/enable_warnings.hpp>
+
+#endif // NDNBOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP
diff --git a/include/ndnboost/random/uniform_01.hpp b/include/ndnboost/random/uniform_01.hpp
new file mode 100644
index 0000000..1a14110
--- /dev/null
+++ b/include/ndnboost/random/uniform_01.hpp
@@ -0,0 +1,277 @@
+/* boost random/uniform_01.hpp header file
+ *
+ * Copyright Jens Maurer 2000-2001
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org for most recent version including documentation.
+ *
+ * $Id: uniform_01.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
+ *
+ * Revision history
+ * 2001-02-18 moved to individual header files
+ */
+
+#ifndef NDNBOOST_RANDOM_UNIFORM_01_HPP
+#define NDNBOOST_RANDOM_UNIFORM_01_HPP
+
+#include <iostream>
+#include <ndnboost/config.hpp>
+#include <ndnboost/limits.hpp>
+#include <ndnboost/static_assert.hpp>
+#include <ndnboost/random/detail/config.hpp>
+#include <ndnboost/random/detail/ptr_helper.hpp>
+
+#include <ndnboost/random/detail/disable_warnings.hpp>
+
+namespace ndnboost {
+namespace random {
+
+#ifdef NDNBOOST_RANDOM_DOXYGEN
+
+/**
+ * The distribution function uniform_01 models a \random_distribution.
+ * On each invocation, it returns a random floating-point value
+ * uniformly distributed in the range [0..1).
+ *
+ * The template parameter RealType shall denote a float-like value type
+ * with support for binary operators +, -, and /.
+ *
+ * Note: The current implementation is buggy, because it may not fill
+ * all of the mantissa with random bits. I'm unsure how to fill a
+ * (to-be-invented) @c ndnboost::bigfloat class with random bits efficiently.
+ * It's probably time for a traits class.
+ */
+template<class RealType = double>
+class uniform_01
+{
+public:
+ typedef RealType input_type;
+ typedef RealType result_type;
+ result_type min NDNBOOST_PREVENT_MACRO_SUBSTITUTION () const;
+ result_type max NDNBOOST_PREVENT_MACRO_SUBSTITUTION () const;
+ void reset();
+
+ template<class Engine>
+ result_type operator()(Engine& eng);
+
+#ifndef NDNBOOST_RANDOM_NO_STREAM_OPERATORS
+ template<class CharT, class Traits>
+ friend std::basic_ostream<CharT,Traits>&
+ operator<<(std::basic_ostream<CharT,Traits>& os, const new_uniform_01&)
+ {
+ return os;
+ }
+
+ template<class CharT, class Traits>
+ friend std::basic_istream<CharT,Traits>&
+ operator>>(std::basic_istream<CharT,Traits>& is, new_uniform_01&)
+ {
+ return is;
+ }
+#endif
+};
+
+#else
+
+namespace detail {
+
+template<class RealType>
+class new_uniform_01
+{
+public:
+ typedef RealType input_type;
+ typedef RealType result_type;
+ // compiler-generated copy ctor and copy assignment are fine
+ result_type min NDNBOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(0); }
+ result_type max NDNBOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(1); }
+ void reset() { }
+
+ template<class Engine>
+ result_type operator()(Engine& eng) {
+ for (;;) {
+ typedef typename Engine::result_type base_result;
+ result_type factor = result_type(1) /
+ (result_type((eng.max)()-(eng.min)()) +
+ result_type(std::numeric_limits<base_result>::is_integer ? 1 : 0));
+ result_type result = result_type(eng() - (eng.min)()) * factor;
+ if (result < result_type(1))
+ return result;
+ }
+ }
+
+#ifndef NDNBOOST_RANDOM_NO_STREAM_OPERATORS
+ template<class CharT, class Traits>
+ friend std::basic_ostream<CharT,Traits>&
+ operator<<(std::basic_ostream<CharT,Traits>& os, const new_uniform_01&)
+ {
+ return os;
+ }
+
+ template<class CharT, class Traits>
+ friend std::basic_istream<CharT,Traits>&
+ operator>>(std::basic_istream<CharT,Traits>& is, new_uniform_01&)
+ {
+ return is;
+ }
+#endif
+};
+
+template<class UniformRandomNumberGenerator, class RealType>
+class backward_compatible_uniform_01
+{
+ typedef ndnboost::random::detail::ptr_helper<UniformRandomNumberGenerator> traits;
+public:
+ typedef UniformRandomNumberGenerator base_type;
+ typedef RealType result_type;
+
+ NDNBOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
+
+#if !defined(NDNBOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) && !(defined(NDNBOOST_MSVC) && NDNBOOST_MSVC <= 1300)
+ NDNBOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
+#endif
+
+ explicit backward_compatible_uniform_01(typename traits::rvalue_type rng)
+ : _rng(rng),
+ _factor(result_type(1) /
+ (result_type((base().max)()-(base().min)()) +
+ result_type(std::numeric_limits<base_result>::is_integer ? 1 : 0)))
+ {
+ }
+ // compiler-generated copy ctor and copy assignment are fine
+
+ result_type min NDNBOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(0); }
+ result_type max NDNBOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(1); }
+ typename traits::value_type& base() { return traits::ref(_rng); }
+ const typename traits::value_type& base() const { return traits::ref(_rng); }
+ void reset() { }
+
+ result_type operator()() {
+ for (;;) {
+ result_type result = result_type(base()() - (base().min)()) * _factor;
+ if (result < result_type(1))
+ return result;
+ }
+ }
+
+#if !defined(NDNBOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(NDNBOOST_NO_MEMBER_TEMPLATE_FRIENDS)
+ template<class CharT, class Traits>
+ friend std::basic_ostream<CharT,Traits>&
+ operator<<(std::basic_ostream<CharT,Traits>& os, const backward_compatible_uniform_01& u)
+ {
+ os << u._rng;
+ return os;
+ }
+
+ template<class CharT, class Traits>
+ friend std::basic_istream<CharT,Traits>&
+ operator>>(std::basic_istream<CharT,Traits>& is, backward_compatible_uniform_01& u)
+ {
+ is >> u._rng;
+ return is;
+ }
+#endif
+
+private:
+ typedef typename traits::value_type::result_type base_result;
+ UniformRandomNumberGenerator _rng;
+ result_type _factor;
+};
+
+#ifndef NDNBOOST_NO_INCLASS_MEMBER_INITIALIZATION
+// A definition is required even for integral static constants
+template<class UniformRandomNumberGenerator, class RealType>
+const bool backward_compatible_uniform_01<UniformRandomNumberGenerator, RealType>::has_fixed_range;
+#endif
+
+template<class UniformRandomNumberGenerator>
+struct select_uniform_01
+{
+ template<class RealType>
+ struct apply
+ {
+ typedef backward_compatible_uniform_01<UniformRandomNumberGenerator, RealType> type;
+ };
+};
+
+template<>
+struct select_uniform_01<float>
+{
+ template<class RealType>
+ struct apply
+ {
+ typedef new_uniform_01<float> type;
+ };
+};
+
+template<>
+struct select_uniform_01<double>
+{
+ template<class RealType>
+ struct apply
+ {
+ typedef new_uniform_01<double> type;
+ };
+};
+
+template<>
+struct select_uniform_01<long double>
+{
+ template<class RealType>
+ struct apply
+ {
+ typedef new_uniform_01<long double> type;
+ };
+};
+
+}
+
+// Because it is so commonly used: uniform distribution on the real [0..1)
+// range. This allows for specializations to avoid a costly int -> float
+// conversion plus float multiplication
+template<class UniformRandomNumberGenerator = double, class RealType = double>
+class uniform_01
+ : public detail::select_uniform_01<UniformRandomNumberGenerator>::NDNBOOST_NESTED_TEMPLATE apply<RealType>::type
+{
+ typedef typename detail::select_uniform_01<UniformRandomNumberGenerator>::NDNBOOST_NESTED_TEMPLATE apply<RealType>::type impl_type;
+ typedef ndnboost::random::detail::ptr_helper<UniformRandomNumberGenerator> traits;
+public:
+
+ uniform_01() {}
+
+ explicit uniform_01(typename traits::rvalue_type rng)
+ : impl_type(rng)
+ {
+ }
+
+#if !defined(NDNBOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(NDNBOOST_NO_MEMBER_TEMPLATE_FRIENDS)
+ template<class CharT, class Traits>
+ friend std::basic_ostream<CharT,Traits>&
+ operator<<(std::basic_ostream<CharT,Traits>& os, const uniform_01& u)
+ {
+ os << static_cast<const impl_type&>(u);
+ return os;
+ }
+
+ template<class CharT, class Traits>
+ friend std::basic_istream<CharT,Traits>&
+ operator>>(std::basic_istream<CharT,Traits>& is, uniform_01& u)
+ {
+ is >> static_cast<impl_type&>(u);
+ return is;
+ }
+#endif
+};
+
+#endif
+
+} // namespace random
+
+using random::uniform_01;
+
+} // namespace ndnboost
+
+#include <ndnboost/random/detail/enable_warnings.hpp>
+
+#endif // NDNBOOST_RANDOM_UNIFORM_01_HPP
diff --git a/include/ndnboost/random/uniform_smallint.hpp b/include/ndnboost/random/uniform_smallint.hpp
new file mode 100644
index 0000000..190e100
--- /dev/null
+++ b/include/ndnboost/random/uniform_smallint.hpp
@@ -0,0 +1,288 @@
+/* boost random/uniform_smallint.hpp header file
+ *
+ * Copyright Jens Maurer 2000-2001
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org for most recent version including documentation.
+ *
+ * $Id: uniform_smallint.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
+ *
+ * Revision history
+ * 2001-04-08 added min<max assertion (N. Becker)
+ * 2001-02-18 moved to individual header files
+ */
+
+#ifndef NDNBOOST_RANDOM_UNIFORM_SMALLINT_HPP
+#define NDNBOOST_RANDOM_UNIFORM_SMALLINT_HPP
+
+#include <istream>
+#include <iosfwd>
+#include <ndnboost/assert.hpp>
+#include <ndnboost/config.hpp>
+#include <ndnboost/limits.hpp>
+#include <ndnboost/type_traits/is_integral.hpp>
+#include <ndnboost/random/detail/config.hpp>
+#include <ndnboost/random/detail/operators.hpp>
+#include <ndnboost/random/detail/signed_unsigned_tools.hpp>
+#include <ndnboost/random/uniform_01.hpp>
+#include <ndnboost/detail/workaround.hpp>
+
+namespace ndnboost {
+namespace random {
+
+// uniform integer distribution on a small range [min, max]
+
+/**
+ * The distribution function uniform_smallint models a \random_distribution.
+ * On each invocation, it returns a random integer value uniformly distributed
+ * in the set of integer numbers {min, min+1, min+2, ..., max}. It assumes
+ * that the desired range (max-min+1) is small compared to the range of the
+ * underlying source of random numbers and thus makes no attempt to limit
+ * quantization errors.
+ *
+ * Let \f$r_{\mathtt{out}} = (\mbox{max}-\mbox{min}+1)\f$ the desired range of
+ * integer numbers, and
+ * let \f$r_{\mathtt{base}}\f$ be the range of the underlying source of random
+ * numbers. Then, for the uniform distribution, the theoretical probability
+ * for any number i in the range \f$r_{\mathtt{out}}\f$ will be
+ * \f$\displaystyle p_{\mathtt{out}}(i) = \frac{1}{r_{\mathtt{out}}}\f$.
+ * Likewise, assume a uniform distribution on \f$r_{\mathtt{base}}\f$ for
+ * the underlying source of random numbers, i.e.
+ * \f$\displaystyle p_{\mathtt{base}}(i) = \frac{1}{r_{\mathtt{base}}}\f$.
+ * Let \f$p_{\mathtt{out\_s}}(i)\f$ denote the random
+ * distribution generated by @c uniform_smallint. Then the sum over all
+ * i in \f$r_{\mathtt{out}}\f$ of
+ * \f$\displaystyle
+ * \left(\frac{p_{\mathtt{out\_s}}(i)}{p_{\mathtt{out}}(i)} - 1\right)^2\f$
+ * shall not exceed
+ * \f$\displaystyle \frac{r_{\mathtt{out}}}{r_{\mathtt{base}}^2}
+ * (r_{\mathtt{base}} \mbox{ mod } r_{\mathtt{out}})
+ * (r_{\mathtt{out}} - r_{\mathtt{base}} \mbox{ mod } r_{\mathtt{out}})\f$.
+ *
+ * The template parameter IntType shall denote an integer-like value type.
+ *
+ * @xmlnote
+ * The property above is the square sum of the relative differences
+ * in probabilities between the desired uniform distribution
+ * \f$p_{\mathtt{out}}(i)\f$ and the generated distribution
+ * \f$p_{\mathtt{out\_s}}(i)\f$.
+ * The property can be fulfilled with the calculation
+ * \f$(\mbox{base\_rng} \mbox{ mod } r_{\mathtt{out}})\f$, as follows:
+ * Let \f$r = r_{\mathtt{base}} \mbox{ mod } r_{\mathtt{out}}\f$.
+ * The base distribution on \f$r_{\mathtt{base}}\f$ is folded onto the
+ * range \f$r_{\mathtt{out}}\f$. The numbers i < r have assigned
+ * \f$\displaystyle
+ * \left\lfloor\frac{r_{\mathtt{base}}}{r_{\mathtt{out}}}\right\rfloor+1\f$
+ * numbers of the base distribution, the rest has only \f$\displaystyle
+ * \left\lfloor\frac{r_{\mathtt{base}}}{r_{\mathtt{out}}}\right\rfloor\f$.
+ * Therefore,
+ * \f$\displaystyle p_{\mathtt{out\_s}}(i) =
+ * \left(\left\lfloor\frac{r_{\mathtt{base}}}
+ * {r_{\mathtt{out}}}\right\rfloor+1\right) /
+ * r_{\mathtt{base}}\f$ for i < r and \f$\displaystyle p_{\mathtt{out\_s}}(i) =
+ * \left\lfloor\frac{r_{\mathtt{base}}}
+ * {r_{\mathtt{out}}}\right\rfloor/r_{\mathtt{base}}\f$ otherwise.
+ * Substituting this in the
+ * above sum formula leads to the desired result.
+ * @endxmlnote
+ *
+ * Note: The upper bound for
+ * \f$(r_{\mathtt{base}} \mbox{ mod } r_{\mathtt{out}})
+ * (r_{\mathtt{out}} - r_{\mathtt{base}} \mbox{ mod } r_{\mathtt{out}})\f$ is
+ * \f$\displaystyle \frac{r_{\mathtt{out}}^2}{4}\f$. Regarding the upper bound
+ * for the square sum of the relative quantization error of
+ * \f$\displaystyle \frac{r_\mathtt{out}^3}{4r_{\mathtt{base}}^2}\f$, it
+ * seems wise to either choose \f$r_{\mathtt{base}}\f$ so that
+ * \f$r_{\mathtt{base}} > 10r_{\mathtt{out}}^2\f$ or ensure that
+ * \f$r_{\mathtt{base}}\f$ is
+ * divisible by \f$r_{\mathtt{out}}\f$.
+ */
+template<class IntType = int>
+class uniform_smallint
+{
+public:
+ typedef IntType input_type;
+ typedef IntType result_type;
+
+ class param_type
+ {
+ public:
+
+ typedef uniform_smallint distribution_type;
+
+ /** constructs the parameters of a @c uniform_smallint distribution. */
+ param_type(IntType min_arg = 0, IntType max_arg = 9)
+ : _min(min_arg), _max(max_arg)
+ {
+ NDNBOOST_ASSERT(_min <= _max);
+ }
+
+ /** Returns the minimum value. */
+ IntType a() const { return _min; }
+ /** Returns the maximum value. */
+ IntType b() const { return _max; }
+
+
+ /** Writes the parameters to a @c std::ostream. */
+ NDNBOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
+ {
+ os << parm._min << " " << parm._max;
+ return os;
+ }
+
+ /** Reads the parameters from a @c std::istream. */
+ NDNBOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
+ {
+ is >> parm._min >> std::ws >> parm._max;
+ return is;
+ }
+
+ /** Returns true if the two sets of parameters are equal. */
+ NDNBOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
+ { return lhs._min == rhs._min && lhs._max == rhs._max; }
+
+ /** Returns true if the two sets of parameters are different. */
+ NDNBOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
+
+ private:
+ IntType _min;
+ IntType _max;
+ };
+
+ /**
+ * Constructs a @c uniform_smallint. @c min and @c max are the
+ * lower and upper bounds of the output range, respectively.
+ */
+ explicit uniform_smallint(IntType min_arg = 0, IntType max_arg = 9)
+ : _min(min_arg), _max(max_arg) {}
+
+ /**
+ * Constructs a @c uniform_smallint from its parameters.
+ */
+ explicit uniform_smallint(const param_type& parm)
+ : _min(parm.a()), _max(parm.b()) {}
+
+ /** Returns the minimum value of the distribution. */
+ result_type a() const { return _min; }
+ /** Returns the maximum value of the distribution. */
+ result_type b() const { return _max; }
+ /** Returns the minimum value of the distribution. */
+ result_type min NDNBOOST_PREVENT_MACRO_SUBSTITUTION () const { return _min; }
+ /** Returns the maximum value of the distribution. */
+ result_type max NDNBOOST_PREVENT_MACRO_SUBSTITUTION () const { return _max; }
+
+ /** Returns the parameters of the distribution. */
+ param_type param() const { return param_type(_min, _max); }
+ /** Sets the parameters of the distribution. */
+ void param(const param_type& parm)
+ {
+ _min = parm.a();
+ _max = parm.b();
+ }
+
+ /**
+ * Effects: Subsequent uses of the distribution do not depend
+ * on values produced by any engine prior to invoking reset.
+ */
+ void reset() { }
+
+ /** Returns a value uniformly distributed in the range [min(), max()]. */
+ template<class Engine>
+ result_type operator()(Engine& eng) const
+ {
+ typedef typename Engine::result_type base_result;
+ return generate(eng, ndnboost::is_integral<base_result>());
+ }
+
+ /** Returns a value uniformly distributed in the range [param.a(), param.b()]. */
+ template<class Engine>
+ result_type operator()(Engine& eng, const param_type& parm) const
+ { return uniform_smallint(parm)(eng); }
+
+ /** Writes the distribution to a @c std::ostream. */
+ NDNBOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, uniform_smallint, ud)
+ {
+ os << ud._min << " " << ud._max;
+ return os;
+ }
+
+ /** Reads the distribution from a @c std::istream. */
+ NDNBOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, uniform_smallint, ud)
+ {
+ is >> ud._min >> std::ws >> ud._max;
+ return is;
+ }
+
+ /**
+ * Returns true if the two distributions will produce identical
+ * sequences of values given equal generators.
+ */
+ NDNBOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(uniform_smallint, lhs, rhs)
+ { return lhs._min == rhs._min && lhs._max == rhs._max; }
+
+ /**
+ * Returns true if the two distributions may produce different
+ * sequences of values given equal generators.
+ */
+ NDNBOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(uniform_smallint)
+
+private:
+
+ // \cond show_private
+ template<class Engine>
+ result_type generate(Engine& eng, ndnboost::mpl::true_) const
+ {
+ // equivalent to (eng() - eng.min()) % (_max - _min + 1) + _min,
+ // but guarantees no overflow.
+ typedef typename Engine::result_type base_result;
+ typedef typename ndnboost::make_unsigned<base_result>::type base_unsigned;
+ typedef typename ndnboost::make_unsigned<result_type>::type range_type;
+ range_type range = random::detail::subtract<result_type>()(_max, _min);
+ base_unsigned base_range =
+ random::detail::subtract<result_type>()((eng.max)(), (eng.min)());
+ base_unsigned val =
+ random::detail::subtract<base_result>()(eng(), (eng.min)());
+ if(range >= base_range) {
+ return ndnboost::random::detail::add<range_type, result_type>()(
+ static_cast<range_type>(val), _min);
+ } else {
+ base_unsigned modulus = static_cast<base_unsigned>(range) + 1;
+ return ndnboost::random::detail::add<range_type, result_type>()(
+ static_cast<range_type>(val % modulus), _min);
+ }
+ }
+
+ template<class Engine>
+ result_type generate(Engine& eng, ndnboost::mpl::false_) const
+ {
+ typedef typename Engine::result_type base_result;
+ typedef typename ndnboost::make_unsigned<result_type>::type range_type;
+ range_type range = random::detail::subtract<result_type>()(_max, _min);
+ base_result val = ndnboost::uniform_01<base_result>()(eng);
+ // what is the worst that can possibly happen here?
+ // base_result may not be able to represent all the values in [0, range]
+ // exactly. If this happens, it will cause round off error and we
+ // won't be able to produce all the values in the range. We don't
+ // care about this because the user has already told us not to by
+ // using uniform_smallint. However, we do need to be careful
+ // to clamp the result, or floating point rounding can produce
+ // an out of range result.
+ range_type offset = static_cast<range_type>(val * (static_cast<base_result>(range) + 1));
+ if(offset > range) return _max;
+ return ndnboost::random::detail::add<range_type, result_type>()(offset , _min);
+ }
+ // \endcond
+
+ result_type _min;
+ result_type _max;
+};
+
+} // namespace random
+
+using random::uniform_smallint;
+
+} // namespace ndnboost
+
+#endif // NDNBOOST_RANDOM_UNIFORM_SMALLINT_HPP