tudocomp
– The TU Dortmund Compression Framework
characterhash.h
Go to the documentation of this file.
1 #ifndef CHARACTERHASH
2 #define CHARACTERHASH
3 
4 typedef unsigned long long uint64;
5 typedef unsigned int uint32;
6 typedef unsigned int uint;
7 
8 #include <cassert>
9 #include <iostream>
10 #include <stdexcept>
11 #include "mersennetwister.h"
12 
13 using namespace std;
14 
16 
17 
18 class mersenneRNG {
19 public:
20  mersenneRNG(uint32 maxval) : mtr(),n(maxval) {};
21  uint32 operator()() {
22  return mtr.randInt(n);
23  }
24  void seed(uint32 seedval) {
25  mtr.seed(seedval);
26  }
27  void seed() {
28  mtr.seed();
29  }
30  uint32 rand_max() {
31  return n;
32  }
33 private:
34  MTRand mtr;
35  int n;
36 };
37 
38 template <typename hashvaluetype>
39 hashvaluetype maskfnc(uint64_t bits) {
40  assert(bits>0);
41  assert(bits<=sizeof(hashvaluetype)*8);
42  hashvaluetype x = static_cast<hashvaluetype>(1) << (bits - 1);
43  return x ^ (x - 1);
44 }
45 
46 template <typename hashvaluetype = uint32, typename chartype = unsigned char>
47 class CharacterHash {
48 public:
49  CharacterHash(hashvaluetype maxval) {
50  if(sizeof(hashvaluetype) <=4) {
51  mersenneRNG randomgenerator(maxval);
52  for(size_t k =0; k<nbrofchars; ++k)
53  hashvalues[k] = static_cast<hashvaluetype>(randomgenerator());
54  } else if (sizeof(hashvaluetype) == 8) {
55  mersenneRNG randomgenerator(maxval>>32);
56  mersenneRNG randomgeneratorbase((maxval>>32) ==0 ? maxval : 0xFFFFFFFFU);
57  for(size_t k =0; k<nbrofchars; ++k)
58  hashvalues[k] = static_cast<hashvaluetype>(randomgeneratorbase())
59  | (static_cast<hashvaluetype>(randomgenerator()) << 32);
60  } else throw runtime_error("unsupported hash value type");
61  }
62 
63  enum {nbrofchars = 1 << ( sizeof(chartype)*8 )};
64 
65  hashvaluetype hashvalues[1 << ( sizeof(chartype)*8 )];
66 };
67 
69 
70 #endif
71 
unsigned int uint
Definition: characterhash.h:6
unsigned int uint32
Definition: characterhash.h:5
unsigned long long uint64
Definition: characterhash.h:4