Tag m_tag;
} Sequence;
public:
- ReSeqArray(size_t deltaList = 128, int deltaBuffer = 1024);
+ ReSeqArray(size_t deltaList = -1024*sizeof(size_t), int deltaBuffer = - 1024*1024);
virtual ~ReSeqArray();
ReSeqArray(const ReSeqArray& source);
ReSeqArray& operator = (const ReSeqArray& source);
}
private:
void run(){
+ massTest();
testBasic();
testNext();
}
void massTest(){
- ReCongruentialGenerator rand;
- ReHashList hash();
+ const int maxKeys = 50*1000;
+ const int minLength = 3;
+ const int maxLength = 10;
+ const int countGet = maxKeys * 50;
+ ReShiftRandom rand;
+ ReHashList hash(false, 3, 3, 1);
+ ReSeqArray list;
+ list.setCapacity(maxKeys, maxKeys * maxLength);
+ list.setSizes(4, maxKeys <= 0xffff ? 2 : 3);
ReByteBuffer key, value;
- log(false, "missing masstest");
+ int64_t start = timer();
+ ReSeqArray::Tag tag;
+ int ix;
+ int collisions = 0;
+ for(ix = 0; ix < maxKeys; ix++){
+ rand.nextString(minLength, maxLength, key);
+ if (hash.get(key, value)){
+ value.append("+", 1);
+ collisions++;
+ } else {
+ value.set(key).append("X", 1);
+ }
+ hash.put(key, value);
+ }
+ int duration1 = milliSecSince(start);
+ start = timer();
+ for (ix = 0; ix < countGet; ix++){
+ int ix2 = rand.nextInt(maxKeys);
+ list.get(ix2, key);
+ checkT(hash.get(key, value));
+ checkT(value.startsWith(key.str(), key.length()));
+ }
+ int duration2 = milliSecSince(start);
+ logF(false, "hashlist: keys: %1d duration: %s get count: %d duration: %s collisions: %d",
+ maxKeys, key.setLength(0).appendMilliSec(duration1).str(),
+ countGet, value.setLength(0).appendMilliSec(duration2).str(),
+ collisions);
}
void testBasic(){
ReHashList hash;
#endif
void testBase(){
+ extern void testReHashList(void);
+ testReHashList();
extern void testReTestUnit();
testReTestUnit();
void testAll(){
try
{
- testOs();
testBase();
+ testOs();
testMath();
testString();
} catch (ReException e){
* ReRandomizer.cpp
*
* Created on: 01.11.2010
- * Author: wk
*/
seed_t seed = nextSeed();
if (minValue == maxValue)
rc = minValue;
- else
- // we calculate in 64 bit:
- rc = (int) (minValue + seed % (maxValue - minValue + 1));
+ else {
+ // we calculate in 64 bit, no range overflow:
+ int64_t range = (int64_t) maxValue - minValue + 1;
+ int64_t offset = seed % range;
+ rc = (int) (minValue + offset);
+ }
if (s_trace){
static int count = 0;
printf ("%c %8x ", count++ % 4 == 0 ? '\n' : ' ', rc);
*
* All character will be inside the range ' ' .. chr(127).
*
- * @param buffer Out: The place for the string.
- * @param maxLength The maximum length of the result string.
* @param minLength The minimum length of the result string.
+ * @param maxLength The maximum length of the result string.
+ * @param buffer Out: The place for the string.
*
* @result The buffer.
*/
-char* ReRandomizer::nextString(char* buffer, int maxLength, int minLength){
- int len = nextInt(minLength, maxLength);
+const char* ReRandomizer::nextString(int minLength, int maxLength, ReByteBuffer &buffer){
+ int len = nextInt(maxLength, minLength);
+ buffer.setLength(len);
+ char* ptr = buffer.buffer();
for (int ii = 0; ii < len; ii++){
- buffer[ii] = nextChar();
+ ptr[ii] = nextChar();
}
- buffer[len] = '\0';
- return buffer;
+ return buffer.str();
}
/**
*/
ReCongruentialGenerator::ReCongruentialGenerator() :
m_seed(0x4711),
- m_factor(2631),
- m_increment(0x9),
+ m_factor(214013),
+ m_increment(2531011),
m_lastSetSeed(0x4711)
{
}
printf(" Seed: %llx ", seed);
}
+
+/** @brief Returns the next 64 bit pseudo random number.
+ *
+ * A congruential generator produces good random in the most significant
+ * bits. Therefore we exchange the bits of the result.
+ * Then x % m returns better results.
+ *
+ * @return a pseudo random number
+*/
+ReRandomizer::seed_t ReShiftRandom::nextSeed(){
+ seed_t rc = ReCongruentialGenerator::nextSeed();
+ rc = ((rc & 0x7fffffff) << 33) | ((rc >> 31) & 0x1ffffffffll);
+ return rc;
+}
* ReRandomizer.h
*
* Created on: 01.11.2010
- * Author: wk
*/
#ifndef RANDOMIZER_H_
virtual int nextInt(int maxValue = INT_MAX, int minValue = 0);
virtual int64_t nextInt64(int64_t maxValue = LLONG_MAX, int64_t minValue = 0);
char nextChar();
- char* nextString(char* buffer, int maxLength = 80, int minLength = 0);
+ const char* nextString(int minLength, int maxLength, ReByteBuffer& buffer);
void shuffle(void* array, size_t length, size_t elemSize);
/** @brief Sets the instance to a defined start state.
*/
void setFactor(seed_t factor);
void setIncrement(seed_t increment);
void setSeed(seed_t m_seed);
-private:
+protected:
+ friend class ReShiftRandom;
virtual seed_t nextSeed();
private:
seed_t m_seed;
seed_t m_lastSetSeed;
};
+class ReShiftRandom : public ReCongruentialGenerator {
+protected:
+ virtual seed_t nextSeed();
+};
#endif /* RANDOMIZER_H_ */