DriveHQ Start Menu
Cloud Drive Mapping
Folder Sync
Cloud Backup
True Drop Box
FTP/SFTP Hosting
Group Account
DriveHQ Start Menu
Online File Server
My Storage
|
Manage Shares
|
Publishes
|
Drop Boxes
|
Group Account
WebDAV Drive Mapping
Cloud Drive Home
|
WebDAV Guide
|
Drive Mapping Tool
|
Drive Mapping URL
Complete Data Backup
Backup Guide
|
Online Backup Tool
|
Cloud-to-Cloud Backup
FTP, Email & Web Service
FTP Home
|
FTP Hosting FAQ
|
Email Hosting
|
EmailManager
|
Web Hosting
Help & Resources
About
|
Enterprise Service
|
Partnership
|
Comparisons
|
Support
Quick Links
Security and Privacy
Download Software
Service Manual
Use Cases
Group Account
Online Help
Blog
Contact
Cloud Surveillance
Sign Up
Login
Features
Business Features
Online File Server
FTP Hosting
Cloud Drive Mapping
Cloud File Backup
Email Backup & Hosting
Cloud File Sharing
Folder Synchronization
Group Management
True Drop Box
Full-text Search
AD Integration/SSO
Mobile Access
IP Camera & DVR Solution
More...
Personal Features
Personal Cloud Drive
Backup All Devices
Mobile APPs
Personal Web Hosting
Sub-Account (for Kids)
Home/PC/Kids Monitoring
More...
Software
DriveHQ Drive Mapping Tool
DriveHQ FileManager
DriveHQ Online Backup
DriveHQ Mobile Apps
Pricing
Business Plans & Pricing
Personal Plans & Pricing
Price Comparison with Others
Feature Comparison with Others
Install Mobile App
Sign up
Creating account...
Invalid character in username! Only 0-9, a-z, A-Z, _, -, . allowed.
Username is required!
Invalid email address!
E-mail is required!
Password is required!
Password is invalid!
Password and confirmation do not match.
Confirm password is required!
I accept
Membership Agreement
Please read the Membership Agreement and check "I accept"!
Free Quick Sign-up
Sign-up Page
Log in
Signing in...
Username or e-mail address is required!
Password is required!
Keep me logged in
Quick Login
Forgot Password
Up
Upload
Download
Share
Publish
New Folder
New File
Copy
Cut
Delete
Paste
Rate
Upgrade
Rotate
Effect
Edit
Slide
History
/* 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 41369 2007-11-25 18:07:19Z bemandawes $ * * Revision history * 2001-02-18 moved to individual header files */ #ifndef BOOST_RANDOM_CONST_MOD_HPP #define BOOST_RANDOM_CONST_MOD_HPP #include
#include
#include
#include
#include
namespace boost { namespace random { /* * Some random number generators require modular arithmetic. Put * everything we need here. * IntType must be an integral type. */ namespace detail { template
struct do_add { }; template<> struct do_add
{ template
static IntType add(IntType m, IntType x, IntType c) { if (x < m - c) return x + c; else return x - (m-c); } }; template<> struct do_add
{ template
static IntType add(IntType, IntType, IntType) { // difficult assert(!"const_mod::add with c too large"); return 0; } }; } // namespace detail #if !(defined(__BORLANDC__) && (__BORLANDC__ == 0x560)) template
class const_mod { public: static IntType add(IntType x, IntType c) { if(c == 0) return x; else if(c <= traits::const_max - m) // i.e. m+c < max return add_small(x, c); else return detail::do_add
::add(m, x, c); } static IntType mult(IntType a, IntType x) { 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 { // difficult assert(!"const_mod::mult with a too large"); return 0; } } static IntType mult_add(IntType a, IntType x, IntType c) { if(m <= (traits::const_max-c)/a) // i.e. a*m+c <= max return (a*x+c) % m; else return add(mult(a, x), c); } static IntType invert(IntType x) { return x == 0 ? 0 : invert_euclidian(x); } private: typedef integer_traits
traits; const_mod(); // don't instantiate static IntType add_small(IntType x, IntType c) { x += c; if(x >= m) x -= m; return x; } static IntType mult_small(IntType a, IntType x) { return a*x % m; } static IntType mult_schrage(IntType a, IntType value) { const IntType q = m / a; const IntType r = m % a; assert(r < q); // check that overflow cannot happen value = a*(value%q) - r*(value/q); // An optimizer bug in the SGI MIPSpro 7.3.1.x compiler requires this // convoluted formulation of the loop (Synge Todo) for(;;) { if (value > 0) break; value += m; } return value; } // 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 BOOST_STATIC_ASSERT(m > 0); #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) assert(boost::integer_traits
::is_signed); #elif !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) BOOST_STATIC_ASSERT(boost::integer_traits
::is_signed); #endif assert(c > 0); IntType l1 = 0; IntType l2 = 1; IntType n = c; IntType p = m; for(;;) { IntType q = p / n; l1 -= q * l2; // this requires a signed IntType! p -= q * n; if(p == 0) return (l2 < 1 ? l2 + m : l2); IntType q2 = n / p; l2 -= q2 * l1; n -= q2 * p; if(n == 0) return (l1 < 1 ? l1 + m : l1); } } }; // The modulus is exactly the word size: rely on machine overflow handling. // Due to a GCC bug, we cannot partially specialize in the presence of // template value parameters. template<> class const_mod
{ typedef unsigned int IntType; public: static IntType add(IntType x, IntType c) { return x+c; } static IntType mult(IntType a, IntType x) { return a*x; } static IntType mult_add(IntType a, IntType x, IntType c) { return a*x+c; } // m is not prime, thus invert is not useful private: // don't instantiate const_mod(); }; template<> class const_mod
{ typedef unsigned long IntType; public: static IntType add(IntType x, IntType c) { return x+c; } static IntType mult(IntType a, IntType x) { return a*x; } static IntType mult_add(IntType a, IntType x, IntType c) { return a*x+c; } // m is not prime, thus invert is not useful private: // don't instantiate const_mod(); }; // the modulus is some power of 2: rely partly on machine overflow handling // we only specialize for rand48 at the moment #ifndef BOOST_NO_INT64_T template<> class const_mod
{ typedef uint64_t IntType; public: static IntType add(IntType x, IntType c) { return c == 0 ? x : mod(x+c); } static IntType mult(IntType a, IntType x) { return mod(a*x); } static IntType mult_add(IntType a, IntType x, IntType c) { return mod(a*x+c); } static IntType mod(IntType x) { return x &= ((uint64_t(1) << 48)-1); } // m is not prime, thus invert is not useful private: // don't instantiate const_mod(); }; #endif /* !BOOST_NO_INT64_T */ #else // // for some reason Borland C++ Builder 6 has problems with // the full specialisations of const_mod, define a generic version // instead, the compiler will optimise away the const-if statements: // template
class const_mod { public: static IntType add(IntType x, IntType c) { if(0 == m) { return x+c; } else { if(c == 0) return x; else if(c <= traits::const_max - m) // i.e. m+c < max return add_small(x, c); else return detail::do_add
::add(m, x, c); } } static IntType mult(IntType a, IntType x) { if(x == 0) { return a*x; } 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 { // difficult assert(!"const_mod::mult with a too large"); return 0; } } } static IntType mult_add(IntType a, IntType x, IntType c) { if(m == 0) { return a*x+c; } else { if(m <= (traits::const_max-c)/a) // i.e. a*m+c <= max return (a*x+c) % m; else return add(mult(a, x), c); } } static IntType invert(IntType x) { return x == 0 ? 0 : invert_euclidian(x); } private: typedef integer_traits
traits; const_mod(); // don't instantiate static IntType add_small(IntType x, IntType c) { x += c; if(x >= m) x -= m; return x; } static IntType mult_small(IntType a, IntType x) { return a*x % m; } static IntType mult_schrage(IntType a, IntType value) { const IntType q = m / a; const IntType r = m % a; assert(r < q); // check that overflow cannot happen value = a*(value%q) - r*(value/q); while(value <= 0) value += m; return value; } // 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 BOOST_STATIC_ASSERT(m > 0); #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS BOOST_STATIC_ASSERT(boost::integer_traits
::is_signed); #endif assert(c > 0); IntType l1 = 0; IntType l2 = 1; IntType n = c; IntType p = m; for(;;) { IntType q = p / n; l1 -= q * l2; // this requires a signed IntType! p -= q * n; if(p == 0) return (l2 < 1 ? l2 + m : l2); IntType q2 = n / p; l2 -= q2 * l1; n -= q2 * p; if(n == 0) return (l1 < 1 ? l1 + m : l1); } } }; #endif } // namespace random } // namespace boost #endif // BOOST_RANDOM_CONST_MOD_HPP
const_mod.hpp
Page URL
File URL
Prev 1/6
Next
Download
( 8 KB )
Note: The DriveHQ service banners will NOT be displayed if the file owner is a paid member.
Comments
Total ratings:
0
Average rating:
Not Rated
Would you like to comment?
Join DriveHQ
for a free account, or
Logon
if you are already a member.