Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r58653 - in trunk: boost/uuid libs/uuid/test
From: atompkins_at_[hidden]
Date: 2010-01-02 23:32:43


Author: atompkins
Date: 2010-01-02 23:32:38 EST (Sat, 02 Jan 2010)
New Revision: 58653
URL: http://svn.boost.org/trac/boost/changeset/58653

Log:
optimized seed_rng
optimized string_generator
fixed compile bug in string_generator for OS X
Text files modified:
   trunk/boost/uuid/seed_rng.hpp | 18 +++++++++++++++---
   trunk/boost/uuid/uuid_generators.hpp | 34 +++++++++++++++-------------------
   trunk/libs/uuid/test/test_generators.cpp | 15 +++++++++------
   3 files changed, 39 insertions(+), 28 deletions(-)

Modified: trunk/boost/uuid/seed_rng.hpp
==============================================================================
--- trunk/boost/uuid/seed_rng.hpp (original)
+++ trunk/boost/uuid/seed_rng.hpp 2010-01-02 23:32:38 EST (Sat, 02 Jan 2010)
@@ -77,7 +77,15 @@
 public:
     seed_rng()
         : rd_index_(5)
+ , random_(std::fopen( "/dev/urandom", "rb" ))
     {}
+
+ ~seed_rng()
+ {
+ if (random_) {
+ std::fclose(random_);
+ }
+ }
 
     result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const
     {
@@ -137,10 +145,9 @@
         {
             unsigned char buffer[ 20 ];
 
- if( std::FILE * f = std::fopen( "/dev/urandom", "rb" ) )
+ if(random_)
             {
- std::fread( buffer, 1, 20, f );
- std::fclose( f );
+ std::fread( buffer, 1, 20, random_ );
             }
 
             // using an uninitialized buffer[] if fopen fails
@@ -173,6 +180,11 @@
 private:
     unsigned int rd_[5];
     int rd_index_;
+ std::FILE * random_;
+
+private: // make seed_rng noncopyable
+ seed_rng(seed_rng const&);
+ seed_rng& operator=(seed_rng const&);
 };
 
 // almost a copy of boost::generator_iterator

Modified: trunk/boost/uuid/uuid_generators.hpp
==============================================================================
--- trunk/boost/uuid/uuid_generators.hpp (original)
+++ trunk/boost/uuid/uuid_generators.hpp 2010-01-02 23:32:38 EST (Sat, 02 Jan 2010)
@@ -144,33 +144,29 @@
     }
 
     unsigned char get_value(char c) const {
- static char const digits[23] = "0123456789abcdefABCDEF";
- static char const*const digits_end = digits+23;
-
- unsigned char const values[23] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,10,11,12,13,14,15 };
+ static char const*const digits_begin = "0123456789abcdefABCDEF";
+ static char const*const digits_end = digits_begin + 22;
 
- char const* d = std::find(&digits[0], digits_end, c);
- if (d == digits_end) {
- return static_cast<unsigned char>(-1);
- }
+ static unsigned char const values[] =
+ { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,10,11,12,13,14,15
+ , static_cast<unsigned char>(-1) };
 
- return values[std::distance(digits, d)];
+ char const* d = std::find(digits_begin, digits_end, c);
+ return values[std::distance(digits_begin, d)];
     }
 
     unsigned char get_value(wchar_t c) const {
- static wchar_t const digits[23] = L"0123456789abcdefABCDEF";
- static wchar_t const*const digits_end = digits+23;
+ static wchar_t const*const digits_begin = L"0123456789abcdefABCDEF";
+ static wchar_t const*const digits_end = digits_begin + 22;
         
- unsigned char const values[23] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,10,11,12,13,14,15 };
-
- wchar_t const* d = std::find(digits, digits_end, c);
- if (d == digits_end) {
- return static_cast<unsigned char>(-1);
- }
+ static unsigned char const values[] =
+ { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,10,11,12,13,14,15
+ , static_cast<unsigned char>(-1) };
 
- return values[std::distance(digits, d)];
+ wchar_t const* d = std::find(digits_begin, digits_end, c);
+ return values[std::distance(digits_begin, d)];
     }
-
+
     bool is_dash(char c) const {
         return c == '-';
     }

Modified: trunk/libs/uuid/test/test_generators.cpp
==============================================================================
--- trunk/libs/uuid/test/test_generators.cpp (original)
+++ trunk/libs/uuid/test/test_generators.cpp 2010-01-02 23:32:38 EST (Sat, 02 Jan 2010)
@@ -110,22 +110,25 @@
     }
 
     { // test random_generator
+ // default random number generator
         random_generator uuid_gen1;
         check_random_generator(uuid_gen1);
         
+ // specific random number generator
         basic_random_generator<boost::rand48> uuid_gen2;
         check_random_generator(uuid_gen2);
         
- boost::rand48 rand48_gen;
- basic_random_generator<boost::rand48> uuid_gen3(rand48_gen);
+ // pass by reference
+ boost::ecuyer1988 ecuyer1988_gen;
+ basic_random_generator<boost::ecuyer1988> uuid_gen3(ecuyer1988_gen);
         check_random_generator(uuid_gen3);
         
- basic_random_generator<boost::rand48> uuid_gen4(&rand48_gen);
+ // pass by pointer
+ boost::lagged_fibonacci607 lagged_fibonacci607_gen;
+ basic_random_generator<boost::lagged_fibonacci607> uuid_gen4(&lagged_fibonacci607_gen);
         check_random_generator(uuid_gen4);
-
- basic_random_generator<boost::lagged_fibonacci44497> uuid_gen5;
- check_random_generator(uuid_gen5);
 
+ // random device
         //basic_random_generator<boost::random_device> uuid_gen5;
         //check_random_generator(uuid_gen5);
     }


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk