Giter Club home page Giter Club logo

caluhash's Introduction

Calvin's (Almost) Universal Hashing Library

This is a dirt-simple C++ library for universal hashing. It is useful if you need to churn out many different hash functions for the same data type. Bloom filters, for example, need many different hash functions. If you are implementing a server, you might want to generate a random hash function to prevent hash collision DoS attacks.

Note that this library implements the "almost universal" rolling hash scheme. It is fast and high-quality in practice, but doesn't offer the strongest guarantees of a "true" universal hash function generator.

How to use this library

#include <caluhash.hpp>
#include <random>
#include <iostream>

struct MyType {
    int field1;
    char field2;
};

// Teach the library how to hash a new type.
caluhash::Hasher& operator<<(caluhash::Hasher& h, const MyType& x) {
    return h << x.field1 << x.field2;
}

int main() {

    // Source of randomness.  Pick your favorite from <random>!
    std::mt19937_64 generator;

    // A randomly-selected hash function.  It will output numbers in the range
    // [0, 2^10).  Pass 64 instead of 10 to get the maximum possible range.
    //
    // The range parameter is useful if your number of buckets is a power of 2;
    // you will not have to "re-hash" the output to fit in how many buckets you
    // have.
    caluhash::HashFunction h(generator, 10);

    MyType x;
    std::cout << "h(x) = " << h(x) << std::endl;

    return 0;
}

See also: test/main.cpp.

How to include this library in your project

If you use CMake:

cmake_minimum_required(VERSION 3.14) # or higher

...

include(FetchContent)
FetchContent_Declare(
    caluhash_repo
    GIT_REPOSITORY https://github.com/Calvin-L/caluhash.git
    GIT_TAG        main)
FetchContent_MakeAvailable(caluhash_repo)

...

target_link_libraries(YOUR_TARGET_HERE PRIVATE caluhash)

Note that you may want to replace main with a specific revision for stable builds.

caluhash's People

Contributors

calvin-l avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.