Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Rolling Hash C++ Library
C++ Makefile
Branch: master
Failed to load latest commit information.
.travis.yml Adding travis file.
Makefile Adding references...
characterhash.h Reformatting.
cyclichash.h Reformatting.
example.cpp Reformatting.
example2.cpp Reformatting.
example3.cpp Reformatting.
example4.cpp Reformatting.
example64bits.cpp Reformatting.
generalhash.h Reformatting.
mersennetwister.h Reformatting.
rabinkarphash.h Reformatting.
speedtesting.cpp Reformatting.
threewisehash.h Reformatting.
unit.cpp Reformatting.

Testing n-gram hash functions

Daniel Lemire

License: Apache 2.0

What is this?

This is a set of C++ classes implementing various recursive n-gram hashing techniques, also called rolling hashing (, including:

  • Randomized Karp-Rabin (sometimes called Rabin-Karp)
  • Hashing by Cyclic Polynomials (also known as Buzhash)
  • Hashing by Irreducible Polynomials

Code sample

    const uint n(3);//hash all sequences of 3 characters
    const uint L(7); // you need 7 bits
    CyclicHash<uint32> hf(n,L );// if you want 64-bit values replace uint32 by uint64
    for(uint32 k = 0; k<n;++k) {
              chartype c = ... ; // grab some character
    ; // feed it to the hasher
    while(...) { // go over your string
       hf.hashvalue; // at all times, this contains the hash value
       chartype c = ... ;// point to the next character
       chartype out = ...; // character we want to forget
       hf.update(out,c); // update hash value



What should I do after I download it?








Daniel Lemire, Owen Kaser: Recursive n-gram hashing is pairwise independent, at best, Computer Speech & Language, Volume 24, Issue 4, October 2010, Pages 698-710

Daniel Lemire, The universality of iterated hashing over variable-length strings, Discrete Applied Mathematics 160 (4-5), 2012.

Owen Kaser and Daniel Lemire, Strongly universal string hashing is fast, Computer Journal (2014) 57 (11): 1624-1638.

Something went wrong with that request. Please try again.