GithubHelp home page GithubHelp logo

doytsujin / patchmap Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 1ykos/patchmap

0.0 1.0 0.0 609 KB

A fast and memory efficient hashmap using sorting to resolve collisions

License: MIT License

C++ 48.17% HTML 50.71% Shell 1.12%

patchmap's Introduction

patchmap

A fast and memory efficient hashmap using sorting to resolve collisions

It achieves a very good trade-off between memory efficiency and speed for load factors over ~0.5. It can be an almost drop-in replacement for std::unordered_map, with the caveat however that iterators are possibly invalidated by insertions.

Usage:

#include <iostream>
#include "patchmap.hpp"
int main() {
  whash::patchmap<int,int> hash_table;
  hash_table[7] = 77;
  for (const auto& elem : hash_table) {
    std::cout << elem.first << " " << elem.second << std::endl;
  }
}
> g++ -O3 -std=c++17 -DNDEBUG main.cpp

This hashmap is inspired by the 1973 publication "Ordered hash tables". The idea presented there is to resolve collisions via an ordering defined by the keys directly. The patchmap however resolves the collisions with an ordering defined by the hash-value of the keys. So long there are no collisions the keys in a hash table are already in hash-order. When there are collisions in the patchmap this order is upheld by inserting the key-value pairs at the appropriate position, displacing keys that compare greater to the right and keys that compare less to the left, essentially performing a single step of insertion sort. This improves the commonly encountered worst-case complexity of O(n) for lookups to O(log(n)) as a binary search can always be employed on a ordered list with random access. In addition, when the hash values are evenly distributed, which is the case for the hash functions supplied with this library, this allowes for an asymptotic complexity of O(log(-log(1-a))) because an interpolation search can be used to retrieve the entries, performing exceptionally well even when the table is almost full.

The resulting ordering is very similar to the one resulting from linear bidirectional probing with robin hood hashing (see for example sherwood_map) but allowing interpolation search leads to improved upper bounds while retaining the same average complexity and overall performance, exceeding other implementations at high load factors around 96% at the cost of O((1-a)¯²) reordering operations for insertions and deletions however.

If you are interested using this container contact me and I will make it work for you, if it does not just work out of the box. No, just contact me regardless, I'm curious how it works for you!

If you are interested in understanding the patchmap or want to implement it in your favourite programming language you should have a look at patchmap_v0.hpp. This is a oversimplified prototype, to ease the understanding without templates and with binary search instead of interpolation search.

TODO

  • use boosts advanced allocation to make use of expansion and reallocation
  • re-unify sparse_patchmap.hpp and patchmap.hpp

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.