lemire / rollinghashcpp

Rolling Hash C++ Library
183 stars 32 forks source link
hashing universality

Randomized rolling hash functions in C++

Ubuntu 22.04 CI (GCC 11)

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 (http://en.wikipedia.org/wiki/Rolling_hash), including:

This library is used by khmer: the in-memory nucleotide sequence k-mer engine.

These are randomized hash functions, meaning that each time you create a new hasher instance, you will get new hash values for a given input.

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
                  hf.eat(c); // 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
        }
        hf.reset(); // you can now hash a new string

Requirements

A recent GNU GCC C++ compiler or a recent CLANG.

What should I do after I download it?

It is a conventional Cmake projet.

cmake -B build
cmake --build build
ctest --test-dir build

Nim version

See Cyclic-Polynomial-Hash for a similar library written in Nim.

References

This work has been used in genomics, see