{"id":13730665,"url":"https://github.com/rigtorp/HashMap","last_synced_at":"2025-05-08T03:31:22.622Z","repository":{"id":137958495,"uuid":"47672102","full_name":"rigtorp/HashMap","owner":"rigtorp","description":"An open addressing linear probing hash table, tuned for delete heavy workloads","archived":false,"fork":false,"pushed_at":"2023-09-25T06:47:23.000Z","size":39,"stargazers_count":200,"open_issues_count":2,"forks_count":32,"subscribers_count":15,"default_branch":"master","last_synced_at":"2024-11-12T18:23:24.953Z","etag":null,"topics":["backshift-deletion","hashmap","hashtable","linear-probing","open-addressing"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rigtorp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-12-09T06:05:39.000Z","updated_at":"2024-11-09T04:23:08.000Z","dependencies_parsed_at":null,"dependency_job_id":"e3daa482-b587-476e-bebb-d04077efc9db","html_url":"https://github.com/rigtorp/HashMap","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rigtorp%2FHashMap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rigtorp%2FHashMap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rigtorp%2FHashMap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rigtorp%2FHashMap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rigtorp","download_url":"https://codeload.github.com/rigtorp/HashMap/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224695640,"owners_count":17354444,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["backshift-deletion","hashmap","hashtable","linear-probing","open-addressing"],"created_at":"2024-08-03T02:01:17.802Z","updated_at":"2024-11-14T21:31:26.011Z","avatar_url":"https://github.com/rigtorp.png","language":"C++","readme":"# HashMap.h\n\n[![C/C++ CI](https://github.com/rigtorp/HashMap/workflows/C/C++%20CI/badge.svg)](https://github.com/rigtorp/HashMap/actions)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/rigtorp/HashMap/master/LICENSE)\n\nA hash table mostly compatible with the C++11 *std::unordered_map*\ninterface, but with much higher performance for many workloads.\n\n## Implementation\n\nThis hash table uses [open addressing][1] with [linear probing][2] and\nbackshift deletion. Open addressing and linear probing minimizes\nmemory allocations and achieves high cache efficiency. Backshift deletion\nkeeps performance high for delete heavy workloads by not clobbering\nthe hash table with [tombestones][3].\n\n[1]: https://en.wikipedia.org/wiki/Open_addressing \"Open addressing\"\n[2]: https://en.wikipedia.org/wiki/Linear_probing \"Linear probing\"\n[3]: https://en.wikipedia.org/wiki/Lazy_deletion \"Lazy deletion\"\n\n## Usage\n\n`HashMap` is mostly compatible with the C++11 container interface. The\nmain differences are:\n\n- A key value to represent the empty key is required.\n- `Key` and `T` needs to be default constructible.\n- Iterators are invalidated on all modifying operations.\n- It's invalid to perform any operations with the empty key.\n- Destructors are not called on `erase`.\n- Extensions for lookups using related key types.\n\nMember functions:\n\n- `HashMap(size_type bucket_count, key_type empty_key);`\n\n  Construct a `HashMap` with `bucket_count` buckets and `empty_key` as\n  the empty key.\n\nThe rest of the member functions are implemented as for\n[`std::unordered_map`](http://en.cppreference.com/w/cpp/container/unordered_map).\n\n## Example\n\n```cpp\n  using namespace rigtorp;\n\n  // Hash for using std::string as lookup key\n  struct Hash {\n    size_t operator()(int v) { return v * 7; }\n    size_t operator()(const std::string \u0026v) { return std::stoi(v) * 7; }\n  };\n\n  // Equal comparison for using std::string as lookup key\n  struct Equal {\n    bool operator()(int lhs, int rhs) { return lhs == rhs; }\n    bool operator()(int lhs, const std::string \u0026rhs) {\n      return lhs == std::stoi(rhs);\n    }\n  };\n\n  // Create a HashMap with 16 buckets and 0 as the empty key\n  HashMap\u003cint, int, Hash, Equal\u003e hm(16, 0);\n  hm.emplace(1, 1);\n  hm[2] = 2;\n\n  // Iterate and print key-value pairs\n  for (const auto \u0026e : hm) {\n    std::cout \u003c\u003c e.first \u003c\u003c \" = \" \u003c\u003c e.second \u003c\u003c \"\\n\";\n  }\n\n  // Lookup using std::string\n  std::cout \u003c\u003c hm.at(\"1\") \u003c\u003c \"\\n\";\n\n  // Erase entry\n  hm.erase(1);\n```\n\n## Benchmark\n\nA benchmark `src/HashMapBenchmark.cpp` is included with the sources. The\nbenchmark simulates a delete heavy workload where items are repeatedly inserted\nand deleted. \n\nI ran this benchmark on the following configuration:\n\n- AMD Ryzen 9 3900X\n- Linux 5.8.4-200.fc32.x86_64\n- gcc (GCC) 10.2.1 20200723 (Red Hat 10.2.1-1)\n- Isolated a core complex (CCX) using `isolcpus` for running the benchmark\n\nWhen working set fits in L3 cache (`HashMapBenchmark -c 100000 -i 100000000`):\n\n| Implementation         | mean ns/iter | max ns/iter |\n| ---------------------- | -----------: | ----------: |\n| HashMap                |           24 |        1082 |\n| absl::flat_hash_map    |           24 |        2074 |\n| google::dense_hash_map |           49 |      689846 |\n| std::unordered_map     |           67 |       10299 |\n\nWhen working set is larger than L3 cache (`HashMapBenchmark -c 10000000 -i 1000000000`):\n\n| Implementation         | mean ns/iter | max ns/iter |\n| ---------------------- | -----------: | ----------: |\n| HashMap                |           75 |       19026 |\n| absl::flat_hash_map    |          101 |       19848 |\n| google::dense_hash_map |          111 |   226083255 |\n| std::unordered_map     |          408 |       22422 |\n\n\n## Cited by\n\nHashMap has been cited by the following papers:\n- Koppl, Dominik. “Separate Chaining Meets Compact Hashing.” (2019).\n  https://arxiv.org/abs/1905.00163 \n\n## About\n\nThis project was created by [Erik Rigtorp](http://rigtorp.se)\n\u003c[erik@rigtorp.se](mailto:erik@rigtorp.se)\u003e.\n","funding_links":[],"categories":["C++"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frigtorp%2FHashMap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frigtorp%2FHashMap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frigtorp%2FHashMap/lists"}