#ifndef CYCLICHASH #define CYCLICHASH #include "characterhash.h" /** * Each instance is a rolling hash function meant to hash streams of characters. * Each new instance of this class comes with new random keys. * * Recommended usage to get L-bit hash values over n-grams: * CyclicHash<> hf(n,L ); * for(uint32 k = 0; k class CyclicHash { public: // myn is the length of the sequences, e.g., 3 means that you want to hash sequences of 3 characters // mywordsize is the number of bits you which to receive as hash values, e.g., 19 means that the hash values are 19-bit integers CyclicHash(int myn, int mywordsize=19) : hashvalue(0), n(myn), wordsize(mywordsize), hasher(maskfnc(wordsize)), mask1(maskfnc(wordsize-1)), myr(n%wordsize), maskn(maskfnc(wordsize-myr)) { if(static_cast(wordsize) > 8*sizeof(THashtype)) { cerr<<"Can't create "<> (wordsize-myr)) ; } void fastleftshift1(THashtype & x) const { x = ((x & mask1) << 1 ) | (x >> (wordsize-1)) ; } void fastrightshift1(THashtype & x) const { x = (x >> 1 ) | ((x & 1)<< (wordsize-1)) ; } THashtype getfastleftshift1(THashtype x) const { return ((x & mask1) << 1 ) | (x >> (wordsize-1)) ; } THashtype getfastrightshift1(THashtype x) const { return (x >> 1 ) | ((x & 1)<< (wordsize-1)) ; } // this is a convenience function, use eat,update and .hashvalue to use as a rolling hash function template THashtype hash(container & c) { THashtype answer(0); for(uint k = 0; k(c[k])]; } return answer; } THashtype hashz(TChartype outchar,uint n) { THashtype answer = hasher.hashvalues[static_cast(outchar)]; for(uint k = 0; k hasher; const THashtype mask1; const int myr; const THashtype maskn; }; #endif