diff options
author | Mike Buland <eichlan@xagasoft.com> | 2012-05-22 17:15:40 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2012-05-22 17:15:40 +0000 |
commit | ce793e31f387c0715fa5b50c20e06510cc3e95ff (patch) | |
tree | 5b15972bdcbe7a26933e8158f52b29fb014dc421 /src/stable | |
parent | 2295579eb790d6eff6e54e84c01da6de10809a71 (diff) | |
download | libbu++-ce793e31f387c0715fa5b50c20e06510cc3e95ff.tar.gz libbu++-ce793e31f387c0715fa5b50c20e06510cc3e95ff.tar.bz2 libbu++-ce793e31f387c0715fa5b50c20e06510cc3e95ff.tar.xz libbu++-ce793e31f387c0715fa5b50c20e06510cc3e95ff.zip |
Moved random to stable, just needs some minor tweaks. But it's already in use
in a couple of core components, including in tempFile name generation.
Diffstat (limited to 'src/stable')
-rw-r--r-- | src/stable/file.cpp | 8 | ||||
-rw-r--r-- | src/stable/random.cpp | 32 | ||||
-rw-r--r-- | src/stable/random.h | 59 | ||||
-rw-r--r-- | src/stable/randombase.cpp | 16 | ||||
-rw-r--r-- | src/stable/randombase.h | 25 | ||||
-rw-r--r-- | src/stable/randombasic.cpp | 31 | ||||
-rw-r--r-- | src/stable/randombasic.h | 29 | ||||
-rw-r--r-- | src/stable/randomcmwc.cpp | 52 | ||||
-rw-r--r-- | src/stable/randomcmwc.h | 29 | ||||
-rw-r--r-- | src/stable/randomsystem.cpp | 40 | ||||
-rw-r--r-- | src/stable/randomsystem.h | 37 |
11 files changed, 354 insertions, 4 deletions
diff --git a/src/stable/file.cpp b/src/stable/file.cpp index 8d10596..81b476d 100644 --- a/src/stable/file.cpp +++ b/src/stable/file.cpp | |||
@@ -12,6 +12,7 @@ | |||
12 | #include <fcntl.h> | 12 | #include <fcntl.h> |
13 | #include <unistd.h> | 13 | #include <unistd.h> |
14 | #include <time.h> | 14 | #include <time.h> |
15 | #include "bu/random.h" | ||
15 | 16 | ||
16 | #include "bu/config.h" | 17 | #include "bu/config.h" |
17 | 18 | ||
@@ -190,7 +191,6 @@ void Bu::File::setBlocking( bool bBlocking ) | |||
190 | Bu::File Bu::File::tempFile( Bu::String &sName ) | 191 | Bu::File Bu::File::tempFile( Bu::String &sName ) |
191 | { | 192 | { |
192 | uint32_t iX; | 193 | uint32_t iX; |
193 | iX = time( NULL ) + getpid(); | ||
194 | int iXes; | 194 | int iXes; |
195 | for( iXes = sName.getSize()-1; iXes >= 0; iXes-- ) | 195 | for( iXes = sName.getSize()-1; iXes >= 0; iXes-- ) |
196 | { | 196 | { |
@@ -200,11 +200,11 @@ Bu::File Bu::File::tempFile( Bu::String &sName ) | |||
200 | iXes++; | 200 | iXes++; |
201 | if( iXes == sName.getSize() ) | 201 | if( iXes == sName.getSize() ) |
202 | throw Bu::ExceptionBase("Invalid temporary filename template."); | 202 | throw Bu::ExceptionBase("Invalid temporary filename template."); |
203 | for( int iter = 0; iter < 100; iter++ ) | 203 | for( int iter = 0; iter < 1000; iter++ ) |
204 | { | 204 | { |
205 | for( int j = iXes; j < sName.getSize(); j++ ) | 205 | for( int j = iXes; j < sName.getSize(); j++ ) |
206 | { | 206 | { |
207 | iX = (1103515245 * iX + 12345); | 207 | uint32_t iX = Bu::Random::rand(); |
208 | sName[j] = ('A'+(iX%26)) | ((iX&0x1000)?(0x20):(0)); | 208 | sName[j] = ('A'+(iX%26)) | ((iX&0x1000)?(0x20):(0)); |
209 | } | 209 | } |
210 | 210 | ||
@@ -214,7 +214,7 @@ Bu::File Bu::File::tempFile( Bu::String &sName ) | |||
214 | |Bu::File::Create|Bu::File::Exclusive ); | 214 | |Bu::File::Create|Bu::File::Exclusive ); |
215 | } catch(...) { } | 215 | } catch(...) { } |
216 | } | 216 | } |
217 | throw Bu::FileException("Failed to create unique temporary file after 100" | 217 | throw Bu::FileException("Failed to create unique temporary file after 1000" |
218 | " iterations."); | 218 | " iterations."); |
219 | } | 219 | } |
220 | 220 | ||
diff --git a/src/stable/random.cpp b/src/stable/random.cpp new file mode 100644 index 0000000..725948a --- /dev/null +++ b/src/stable/random.cpp | |||
@@ -0,0 +1,32 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2012 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | #include "bu/random.h" | ||
8 | |||
9 | #include "bu/randombasic.h" | ||
10 | |||
11 | Bu::Random::Random() : | ||
12 | pGen( NULL ) | ||
13 | { | ||
14 | pGen = new RandomBasic(); | ||
15 | } | ||
16 | |||
17 | Bu::Random::~Random() | ||
18 | { | ||
19 | delete pGen; | ||
20 | pGen = NULL; | ||
21 | } | ||
22 | |||
23 | int32_t Bu::Random::rand() | ||
24 | { | ||
25 | return getInstance().pGen->rand(); | ||
26 | } | ||
27 | |||
28 | void Bu::Random::seed( int32_t iSeed ) | ||
29 | { | ||
30 | getInstance().pGen->seed( iSeed ); | ||
31 | } | ||
32 | |||
diff --git a/src/stable/random.h b/src/stable/random.h new file mode 100644 index 0000000..ba26f21 --- /dev/null +++ b/src/stable/random.h | |||
@@ -0,0 +1,59 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2012 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | #ifndef BU_RANDOM_H | ||
8 | #define BU_RANDOM_H | ||
9 | |||
10 | #include "bu/singleton.h" | ||
11 | #include <stdint.h> | ||
12 | |||
13 | namespace Bu | ||
14 | { | ||
15 | class RandomBase; | ||
16 | class Random : public Bu::Singleton<Bu::Random> | ||
17 | { | ||
18 | friend class Bu::Singleton<Bu::Random>; | ||
19 | private: | ||
20 | Random(); | ||
21 | virtual ~Random(); | ||
22 | |||
23 | public: | ||
24 | template<typename cl> | ||
25 | static void setGenerator() | ||
26 | { | ||
27 | delete getInstance().pGen; | ||
28 | getInstance().pGen = new cl(); | ||
29 | } | ||
30 | |||
31 | template<typename cl, typename t1> | ||
32 | static void setGenerator( t1 p1 ) | ||
33 | { | ||
34 | delete getInstance().pGen; | ||
35 | getInstance().pGen = new cl( p1 ); | ||
36 | } | ||
37 | |||
38 | template<typename cl, typename t1, typename t2> | ||
39 | static void setGenerator( t1 p1, t2 p2 ) | ||
40 | { | ||
41 | delete getInstance().pGen; | ||
42 | getInstance().pGen = new cl( p1, p2 ); | ||
43 | } | ||
44 | |||
45 | RandomBase &getGenerator() { return *pGen; } | ||
46 | |||
47 | static int32_t rand(); | ||
48 | static void seed( int32_t iSeed ); | ||
49 | |||
50 | private: | ||
51 | void checkInit(); | ||
52 | |||
53 | private: | ||
54 | Bu::RandomBase *pGen; | ||
55 | }; | ||
56 | }; | ||
57 | |||
58 | #endif | ||
59 | |||
diff --git a/src/stable/randombase.cpp b/src/stable/randombase.cpp new file mode 100644 index 0000000..6514df3 --- /dev/null +++ b/src/stable/randombase.cpp | |||
@@ -0,0 +1,16 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2012 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | #include "bu/randombase.h" | ||
8 | |||
9 | Bu::RandomBase::RandomBase() | ||
10 | { | ||
11 | } | ||
12 | |||
13 | Bu::RandomBase::~RandomBase() | ||
14 | { | ||
15 | } | ||
16 | |||
diff --git a/src/stable/randombase.h b/src/stable/randombase.h new file mode 100644 index 0000000..aff1d45 --- /dev/null +++ b/src/stable/randombase.h | |||
@@ -0,0 +1,25 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2012 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | #ifndef BU_RANDOM_BASE_H | ||
8 | #define BU_RANDOM_BASE_H | ||
9 | |||
10 | #include <stdint.h> | ||
11 | |||
12 | namespace Bu | ||
13 | { | ||
14 | class RandomBase | ||
15 | { | ||
16 | public: | ||
17 | RandomBase(); | ||
18 | virtual ~RandomBase(); | ||
19 | |||
20 | virtual void seed( int32_t iSeed )=0; | ||
21 | virtual int32_t rand()=0; | ||
22 | }; | ||
23 | }; | ||
24 | |||
25 | #endif | ||
diff --git a/src/stable/randombasic.cpp b/src/stable/randombasic.cpp new file mode 100644 index 0000000..ac591be --- /dev/null +++ b/src/stable/randombasic.cpp | |||
@@ -0,0 +1,31 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2012 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #include "bu/randombasic.h" | ||
9 | |||
10 | Bu::RandomBasic::RandomBasic() : | ||
11 | a( 6364136223846793005 ), | ||
12 | c( 1442695040888963407 ), | ||
13 | x( 0 ) | ||
14 | { | ||
15 | } | ||
16 | |||
17 | Bu::RandomBasic::~RandomBasic() | ||
18 | { | ||
19 | } | ||
20 | |||
21 | void Bu::RandomBasic::seed( int32_t iSeed ) | ||
22 | { | ||
23 | c = iSeed; | ||
24 | } | ||
25 | |||
26 | int32_t Bu::RandomBasic::rand() | ||
27 | { | ||
28 | x = (a*x + c); | ||
29 | return (int32_t)x; | ||
30 | } | ||
31 | |||
diff --git a/src/stable/randombasic.h b/src/stable/randombasic.h new file mode 100644 index 0000000..a53e16f --- /dev/null +++ b/src/stable/randombasic.h | |||
@@ -0,0 +1,29 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2012 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | #ifndef BU_RANDOM_BASIC_H | ||
8 | #define BU_RANDOM_BASIC_H | ||
9 | |||
10 | #include "bu/randombase.h" | ||
11 | |||
12 | namespace Bu | ||
13 | { | ||
14 | class RandomBasic : public RandomBase | ||
15 | { | ||
16 | public: | ||
17 | RandomBasic(); | ||
18 | virtual ~RandomBasic(); | ||
19 | |||
20 | virtual void seed( int32_t iSeed ); | ||
21 | |||
22 | virtual int32_t rand(); | ||
23 | |||
24 | private: | ||
25 | int64_t a, c, x; | ||
26 | }; | ||
27 | }; | ||
28 | |||
29 | #endif | ||
diff --git a/src/stable/randomcmwc.cpp b/src/stable/randomcmwc.cpp new file mode 100644 index 0000000..a4e807e --- /dev/null +++ b/src/stable/randomcmwc.cpp | |||
@@ -0,0 +1,52 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2012 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #include "bu/randomcmwc.h" | ||
9 | |||
10 | #define PHI 0x9e3779b9 | ||
11 | |||
12 | Bu::RandomCmwc::RandomCmwc() : | ||
13 | q( 0 ), | ||
14 | c( 362436 ) | ||
15 | { | ||
16 | q = new uint32_t[4096]; | ||
17 | } | ||
18 | |||
19 | Bu::RandomCmwc::~RandomCmwc() | ||
20 | { | ||
21 | delete[] q; | ||
22 | } | ||
23 | |||
24 | void Bu::RandomCmwc::seed( int32_t iSeed ) | ||
25 | { | ||
26 | int i; | ||
27 | |||
28 | q[0] = iSeed; | ||
29 | q[1] = iSeed + PHI; | ||
30 | q[2] = iSeed + PHI + PHI; | ||
31 | |||
32 | for (i = 3; i < 4096; i++) | ||
33 | q[i] = q[i - 3] ^ q[i - 2] ^ PHI ^ i; | ||
34 | } | ||
35 | |||
36 | int32_t Bu::RandomCmwc::rand() | ||
37 | { | ||
38 | uint64_t t, a = 18782LL; | ||
39 | static uint32_t i = 4095; | ||
40 | uint32_t x, r = 0xfffffffe; | ||
41 | i = (i + 1) & 4095; | ||
42 | t = a * q[i] + c; | ||
43 | c = (t >> 32); | ||
44 | x = t + c; | ||
45 | if( x < c ) | ||
46 | { | ||
47 | x++; | ||
48 | c++; | ||
49 | } | ||
50 | return (q[i] = r - x); | ||
51 | } | ||
52 | |||
diff --git a/src/stable/randomcmwc.h b/src/stable/randomcmwc.h new file mode 100644 index 0000000..747eb6a --- /dev/null +++ b/src/stable/randomcmwc.h | |||
@@ -0,0 +1,29 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2012 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | #ifndef BU_RANDOM_CMWC_H | ||
8 | #define BU_RANDOM_CMWC_H | ||
9 | |||
10 | #include "bu/randombase.h" | ||
11 | |||
12 | namespace Bu | ||
13 | { | ||
14 | class RandomCmwc : public RandomBase | ||
15 | { | ||
16 | public: | ||
17 | RandomCmwc(); | ||
18 | virtual ~RandomCmwc(); | ||
19 | |||
20 | virtual void seed( int32_t iSeed ); | ||
21 | |||
22 | virtual int32_t rand(); | ||
23 | |||
24 | private: | ||
25 | uint32_t *q, c; | ||
26 | }; | ||
27 | }; | ||
28 | |||
29 | #endif | ||
diff --git a/src/stable/randomsystem.cpp b/src/stable/randomsystem.cpp new file mode 100644 index 0000000..0501587 --- /dev/null +++ b/src/stable/randomsystem.cpp | |||
@@ -0,0 +1,40 @@ | |||
1 | #include "bu/randomsystem.h" | ||
2 | #include "bu/file.h" | ||
3 | |||
4 | Bu::RandomSystem::RandomSystem( Type eType ) : | ||
5 | eType( eType ), | ||
6 | pSrc( 0 ) | ||
7 | { | ||
8 | switch( eType ) | ||
9 | { | ||
10 | case Bu::RandomSystem::Fast: | ||
11 | pSrc = new Bu::File("/dev/urandom", Bu::File::Read ); | ||
12 | break; | ||
13 | |||
14 | case Bu::RandomSystem::Good: | ||
15 | pSrc = new Bu::File("/dev/random", Bu::File::Read ); | ||
16 | break; | ||
17 | } | ||
18 | } | ||
19 | |||
20 | Bu::RandomSystem::~RandomSystem() | ||
21 | { | ||
22 | delete pSrc; | ||
23 | } | ||
24 | |||
25 | void Bu::RandomSystem::seed( int32_t /*iSeed*/ ) | ||
26 | { | ||
27 | // Seed really has no effect here... | ||
28 | // on linux, if we were root, we could write data to random/urandom to | ||
29 | // perturb the data, but it's not necesarry | ||
30 | } | ||
31 | |||
32 | int32_t Bu::RandomSystem::rand() | ||
33 | { | ||
34 | if( !pSrc ) | ||
35 | throw Bu::ExceptionBase("Not initialized"); | ||
36 | int32_t i; | ||
37 | pSrc->read( &i, sizeof(int32_t) ); | ||
38 | return i; | ||
39 | } | ||
40 | |||
diff --git a/src/stable/randomsystem.h b/src/stable/randomsystem.h new file mode 100644 index 0000000..7106d58 --- /dev/null +++ b/src/stable/randomsystem.h | |||
@@ -0,0 +1,37 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2012 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | #ifndef BU_RANDOM_SYSTEM_H | ||
8 | #define BU_RANDOM_SYSTEM_H | ||
9 | |||
10 | #include "bu/randombase.h" | ||
11 | |||
12 | namespace Bu | ||
13 | { | ||
14 | class File; | ||
15 | class RandomSystem : public RandomBase | ||
16 | { | ||
17 | public: | ||
18 | enum Type | ||
19 | { | ||
20 | Fast, | ||
21 | Good | ||
22 | }; | ||
23 | |||
24 | RandomSystem( Type eType=Fast ); | ||
25 | virtual ~RandomSystem(); | ||
26 | |||
27 | virtual void seed( int32_t iSeed ); | ||
28 | |||
29 | virtual int32_t rand(); | ||
30 | |||
31 | private: | ||
32 | Type eType; | ||
33 | File *pSrc; | ||
34 | }; | ||
35 | }; | ||
36 | |||
37 | #endif | ||