Skip to content

Commit

Permalink
Adding another example.
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire committed Jan 12, 2015
1 parent 4747579 commit f02d773
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
4 changes: 2 additions & 2 deletions cyclichash.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class CyclicHash {
// this is a convenience function, use eat,update and .hashvalue to use as a rolling hash function
template<class container>
hashvaluetype hash(container & c) {
assert(c.size()==static_cast<uint>(n));
hashvaluetype answer(0);
for(uint k = 0; k<c.size();++k) {
fastleftshift(answer, 1) ;
Expand All @@ -81,7 +80,8 @@ class CyclicHash {
}

uint32 hashvalue;
const int n, wordsize;
int n;
const int wordsize;
CharacterHash hasher;
const hashvaluetype mask1;
const int myr;
Expand Down
34 changes: 34 additions & 0 deletions example2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <string>
#include <vector>
#include <memory>
#include <iostream>

// given hash value of "ABCD", can I have value of
// "ABCDE", without computing the whole hash value?

#include "cyclichash.h"


int main(int argc, char * argv[])
{
CyclicHash hf(5,19);
string input = "ABCDE";
hf.eat(input[0]);//A
hf.eat(input[1]);//B
hf.eat(input[2]);//C
hf.eat(input[3]);//D
cout<<"Hash value of ABCD is " << hf.hashvalue << endl;
// we check the answer going the long way...
const std::vector<unsigned char> charvectslice(input.begin(), input.begin()+4);
uint32_t trueanswerslice = hf.hash(charvectslice);
if(trueanswerslice != hf.hashvalue ) throw runtime_error("bug");
// we continue
hf.eat(input[4]);//E
cout<<"Hash value of ABCDE is " << hf.hashvalue << endl;
// we check the answer going the long way
const std::vector<unsigned char> charvect(input.begin(), input.end());
uint32_t trueanswer = hf.hash(charvect);
if(trueanswer != hf.hashvalue ) throw runtime_error("bug");
return 0;

}
3 changes: 1 addition & 2 deletions generalhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ class GeneralHash {
// this is a convenience function, use eat,update and .hashvalue to use as a rolling hash function
template<class container>
hashvaluetype hash(container & c) const {
assert(c.size()==static_cast<uint>(n));
hashvaluetype answer(0);
for(uint k = 0; k<c.size();++k) {
fastleftshift(answer, 1) ;
Expand All @@ -116,7 +115,7 @@ class GeneralHash {

hashvaluetype hashvalue;
const int wordsize;
const int n;
int n;
hashvaluetype irreduciblepoly;
CharacterHash hasher;
const hashvaluetype lastbit;
Expand Down
3 changes: 2 additions & 1 deletion rabinkarphash.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ class KarpRabinHash {
}

hashvaluetype hashvalue;
const int n, wordsize;
int n;
const wordsize;
CharacterHash hasher;
const hashvaluetype HASHMASK;
hashvaluetype BtoN;
Expand Down
3 changes: 2 additions & 1 deletion threewisehash.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ class ThreeWiseHash {
}

hashvaluetype hashvalue;
const int n, wordsize;
int n;
const int wordsize;
deque<chartype> ngram;
vector<CharacterHash> hashers;
CharacterHash hasher;//placeholder
Expand Down
4 changes: 4 additions & 0 deletions unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ bool isItAFunction(uint L = 7) {
return true;
}





template<class hashfunction>
bool isItRandom(uint L = 19) {
cout<<"checking that it is randomized "<<endl;
Expand Down

0 comments on commit f02d773

Please sign in to comment.