{"id":17325658,"url":"https://github.com/tessil/sparse-map","last_synced_at":"2025-04-09T06:10:20.099Z","repository":{"id":50928867,"uuid":"119572880","full_name":"Tessil/sparse-map","owner":"Tessil","description":"C++ implementation of a memory efficient hash map and hash set","archived":false,"fork":false,"pushed_at":"2024-09-22T10:36:35.000Z","size":727,"stargazers_count":345,"open_issues_count":8,"forks_count":37,"subscribers_count":22,"default_branch":"master","last_synced_at":"2025-04-02T04:06:11.587Z","etag":null,"topics":["c-plus-plus","cpp","data-structures","hash-map","hash-table","header-only"],"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/Tessil.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":"2018-01-30T17:54:42.000Z","updated_at":"2025-03-30T00:44:22.000Z","dependencies_parsed_at":"2023-02-09T23:45:59.309Z","dependency_job_id":"f759f248-957f-4edf-9c19-7cce17f76472","html_url":"https://github.com/Tessil/sparse-map","commit_stats":{"total_commits":103,"total_committers":6,"mean_commits":"17.166666666666668","dds":0.06796116504854366,"last_synced_commit":"1bc4561e5a267c0ae0e1e543ba64e7436946a765"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tessil%2Fsparse-map","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tessil%2Fsparse-map/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tessil%2Fsparse-map/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tessil%2Fsparse-map/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Tessil","download_url":"https://codeload.github.com/Tessil/sparse-map/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247987285,"owners_count":21028895,"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":["c-plus-plus","cpp","data-structures","hash-map","hash-table","header-only"],"created_at":"2024-10-15T14:13:59.363Z","updated_at":"2025-04-09T06:10:20.070Z","avatar_url":"https://github.com/Tessil.png","language":"C++","readme":"[![CI](https://github.com/Tessil/sparse-map/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/Tessil/sparse-map/actions/workflows/ci.yml)\n\n## A C++ implementation of a memory efficient hash map and hash set\n\nThe sparse-map library is a C++ implementation of a memory efficient hash map and hash set. It uses open-addressing with sparse quadratic probing. The goal of the library is to be the most memory efficient possible, even at low load factor, while keeping reasonable performances. You can find an [article](https://smerity.com/articles/2015/google_sparsehash.html) of Stephen Merity which explains the idea behind `google::sparse_hash_map` and this project.\n\nFour classes are provided: `tsl::sparse_map`, `tsl::sparse_set`, `tsl::sparse_pg_map` and `tsl::sparse_pg_set`. The first two are faster and use a power of two growth policy, the last two use a prime growth policy instead and are able to cope better with a poor hash function. Use the prime version if there is a chance of repeating patterns in the lower bits of your hash (e.g. you are storing pointers with an identity hash function). See [GrowthPolicy](#growth-policy) for details.\n\nA **benchmark** of `tsl::sparse_map` against other hash maps may be found [here](https://tessil.github.io/2016/08/29/benchmark-hopscotch-map.html). The benchmark, in its additional tests page, notably includes `google::sparse_hash_map` and `spp::sparse_hash_map` to which `tsl::sparse_map` is an alternative. This page also gives some advices on which hash table structure you should try for your use case (useful if you are a bit lost with the multiple hash tables implementations in the `tsl` namespace).\n\n### Key features\n\n- Header-only library, just add the [include](include/) directory to your include path and you are ready to go. If you use CMake, you can also use the `tsl::sparse_map` exported target from the [CMakeLists.txt](CMakeLists.txt).\n- Memory efficient while keeping good lookup speed, see the [benchmark](https://tessil.github.io/2016/08/29/benchmark-hopscotch-map.html) for some numbers.\n- Support for heterogeneous lookups allowing the usage of `find` with a type different than `Key` (e.g. if you have a map that uses `std::unique_ptr\u003cfoo\u003e` as key, you can use a `foo*` or a `std::uintptr_t` as key parameter to `find` without constructing a `std::unique_ptr\u003cfoo\u003e`, see [example](#heterogeneous-lookups)).\n- No need to reserve any sentinel value from the keys.\n- If the hash is known before a lookup, it is possible to pass it as parameter to speed-up the lookup (see `precalculated_hash` parameter in [API](https://tessil.github.io/sparse-map/classtsl_1_1sparse__map.html)).\n- Support for efficient serialization and deserialization (see [example](#serialization) and the `serialize/deserialize` methods in the [API](https://tessil.github.io/sparse-map/classtsl_1_1sparse__map.html) for details).\n- Possibility to control the balance between insertion speed and memory usage with the `Sparsity` template parameter. A high sparsity means less memory but longer insertion times, and vice-versa for low sparsity. The default medium sparsity offers a good compromise (see [API](https://tessil.github.io/sparse-map/classtsl_1_1sparse__map.html#details) for details). For reference, with simple 64 bits integers as keys and values, a low sparsity offers ~15% faster insertions times but uses ~12% more memory. Nothing change regarding lookup speed.\n- API closely similar to `std::unordered_map` and `std::unordered_set`.\n\n### Differences compared to `std::unordered_map`\n\n`tsl::sparse_map` tries to have an interface similar to `std::unordered_map`, but some differences exist.\n\n- **By default only the basic exception safety is guaranteed** which mean that, in case of exception, all resources used by the hash map will be freed (no memory leaks) but the hash map may end-up in an undefined state (undefined here means that some elements may be missing). This can ONLY happen on rehash (either on insert or if `rehash` is called explicitly) and will occur if the `Allocator` can't allocate memory (`std::bad_alloc`) or if the copy constructor (when a nothrow move constructor is not available) throws and exception. This can be avoided by calling `reserve` beforehand. It is the same guarantee that the one provided by `google::sparse_hash_map` and `spp::sparse_hash_map` which don't provide the strong exception guarantee. For more information and if you need the strong exception guarantee, check the `ExceptionSafety` template parameter (see [API](https://tessil.github.io/sparse-map/classtsl_1_1sparse__map.html#details) for details).\n- Iterator invalidation doesn't behave in the same way, any operation modifying the hash table invalidate them (see [API](https://tessil.github.io/sparse-map/classtsl_1_1sparse__map.html#details) for details).\n- References and pointers to keys or values in the map are invalidated in the same way as iterators to these keys-values.\n- For iterators, `operator*()` and `operator-\u003e()` return a reference and a pointer to `const std::pair\u003cKey, T\u003e` instead of `std::pair\u003cconst Key, T\u003e` making the value `T` not modifiable. To modify the value you have to call the `value()` method of the iterator to get a mutable reference. Example:\n```c++\ntsl::sparse_map\u003cint, int\u003e map = {{1, 1}, {2, 1}, {3, 1}};\nfor(auto it = map.begin(); it != map.end(); ++it) {\n    //it-\u003esecond = 2; // Illegal\n    it.value() = 2; // Ok\n}\n```\n- Move-only types must have a nothrow move constructor.\n- No support for some buckets related methods (like `bucket_size`, `bucket`, ...).\n\nThese differences also apply between `std::unordered_set` and `tsl::sparse_set`.\n\nThread-safety guarantees are the same as `std::unordered_map/set` (i.e. possible to have multiple readers with no writer).\n\n### Optimization\n\n#### Popcount\nThe library relies heavily on the [popcount](https://en.wikipedia.org/wiki/Hamming_weight) operation. \n\nWith Clang and GCC, the library uses the `__builtin_popcount` function which will use the fast CPU instruction `POPCNT` when the library is compiled with `-mpopcnt`. Using the `POPCNT` instruction offers an improvement of ~15% to ~30% on lookups. So if you are compiling your code for a specific architecture that support the operation, don't forget the `-mpopcnt` (or `-march=native`) flag of your compiler.\n\nOn Windows with MSVC, the detection is done at runtime.\n\n#### Move constructor\nMake sure that your key `Key` and potential value `T` have a `noexcept` move constructor. The library will work without it but insertions will be much slower if the copy constructor is expensive (the structure often needs to move some values around on insertion).\n\n### Growth policy\n\nThe library supports multiple growth policies through the `GrowthPolicy` template parameter. Three policies are provided by the library but you can easily implement your own if needed.\n\n* **[tsl::sh::power_of_two_growth_policy.](https://tessil.github.io/sparse-map/classtsl_1_1sh_1_1power__of__two__growth__policy.html)** Default policy used by `tsl::sparse_map/set`. This policy keeps the size of the bucket array of the hash table to a power of two. This constraint allows the policy to avoid the usage of the slow modulo operation to map a hash to a bucket, instead of \u003ccode\u003ehash % 2\u003csup\u003en\u003c/sup\u003e\u003c/code\u003e, it uses \u003ccode\u003ehash \u0026 (2\u003csup\u003en\u003c/sup\u003e - 1)\u003c/code\u003e (see [fast modulo](https://en.wikipedia.org/wiki/Modulo_operation#Performance_issues)). Fast but this may cause a lot of collisions with a poor hash function as the modulo with a power of two only masks the most significant bits in the end.\n* **[tsl::sh::prime_growth_policy.](https://tessil.github.io/sparse-map/classtsl_1_1sh_1_1prime__growth__policy.html)** Default policy used by `tsl::sparse_pg_map/set`. The policy keeps the size of the bucket array of the hash table to a prime number. When mapping a hash to a bucket, using a prime number as modulo will result in a better distribution of the hash across the buckets even with a poor hash function. To allow the compiler to optimize the modulo operation, the policy use a lookup table with constant primes modulos (see [API](https://tessil.github.io/sparse-map/classtsl_1_1sh_1_1prime__growth__policy.html#details) for details). Slower than `tsl::sh::power_of_two_growth_policy` but more secure.\n* **[tsl::sh::mod_growth_policy.](https://tessil.github.io/sparse-map/classtsl_1_1sh_1_1mod__growth__policy.html)** The policy grows the map by a customizable growth factor passed in parameter. It then just use the modulo operator to map a hash to a bucket. Slower but more flexible.\n\n\nTo implement your own policy, you have to implement the following interface.\n\n```c++\nstruct custom_policy {\n    // Called on hash table construction and rehash, min_bucket_count_in_out is the minimum buckets\n    // that the hash table needs. The policy can change it to a higher number of buckets if needed \n    // and the hash table will use this value as bucket count. If 0 bucket is asked, then the value\n    // must stay at 0.    \n    explicit custom_policy(std::size_t\u0026 min_bucket_count_in_out);\n    \n    // Return the bucket [0, bucket_count()) to which the hash belongs. \n    // If bucket_count() is 0, it must always return 0.\n    std::size_t bucket_for_hash(std::size_t hash) const noexcept;\n    \n    // Return the number of buckets that should be used on next growth\n    std::size_t next_bucket_count() const;\n    \n    // Return the maximum number of buckets supported by the policy.\n    std::size_t max_bucket_count() const;\n    \n    // Reset the growth policy as if it was created with a bucket count of 0.\n    // After a clear, the policy must always return 0 when bucket_for_hash is called.\n    void clear() noexcept;\n}\n```\n\n### Installation\n\nTo use sparse-map, just add the [include](include/) directory to your include path. It is a **header-only** library.\n\nIf you use CMake, you can also use the `tsl::sparse_map` exported target from the [CMakeLists.txt](CMakeLists.txt) with `target_link_libraries`. \n```cmake\n# Example where the sparse-map project is stored in a third-party directory\nadd_subdirectory(third-party/sparse-map)\ntarget_link_libraries(your_target PRIVATE tsl::sparse_map)  \n```\n\nIf the project has been installed through `make install`, you can also use `find_package(tsl-sparse-map REQUIRED)` instead of `add_subdirectory`.\n\nThe code should work with any C++17 standard-compliant compiler.\n\nTo run the tests you will need the Boost Test library and CMake.\n\n```bash\ngit clone https://github.com/Tessil/sparse-map.git\ncd sparse-map/tests\nmkdir build\ncd build\ncmake ..\ncmake --build .\n./tsl_sparse_map_tests \n```\n\n### Usage\n\nThe API can be found [here](https://tessil.github.io/sparse-map/). \n\nAll methods are not documented yet, but they replicate the behaviour of the ones in `std::unordered_map` and `std::unordered_set`, except if specified otherwise.\n\n### Example\n\n```c++\n#include \u003ccstdint\u003e\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\n#include \u003ctsl/sparse_map.h\u003e\n#include \u003ctsl/sparse_set.h\u003e\n\nint main() {\n    tsl::sparse_map\u003cstd::string, int\u003e map = {{\"a\", 1}, {\"b\", 2}};\n    map[\"c\"] = 3;\n    map[\"d\"] = 4;\n    \n    map.insert({\"e\", 5});\n    map.erase(\"b\");\n    \n    for(auto it = map.begin(); it != map.end(); ++it) {\n        //it-\u003esecond += 2; // Not valid.\n        it.value() += 2;\n    }\n    \n    // {d, 6} {a, 3} {e, 7} {c, 5}\n    for(const auto\u0026 key_value : map) {\n        std::cout \u003c\u003c \"{\" \u003c\u003c key_value.first \u003c\u003c \", \" \u003c\u003c key_value.second \u003c\u003c \"}\" \u003c\u003c std::endl;\n    }\n    \n    \n    if(map.find(\"a\") != map.end()) {\n        std::cout \u003c\u003c \"Found \\\"a\\\".\" \u003c\u003c std::endl;\n    }\n    \n    const std::size_t precalculated_hash = std::hash\u003cstd::string\u003e()(\"a\");\n    // If we already know the hash beforehand, we can pass it as argument to speed-up the lookup.\n    if(map.find(\"a\", precalculated_hash) != map.end()) {\n        std::cout \u003c\u003c \"Found \\\"a\\\" with hash \" \u003c\u003c precalculated_hash \u003c\u003c \".\" \u003c\u003c std::endl;\n    }\n    \n    \n    \n    \n    tsl::sparse_set\u003cint\u003e set;\n    set.insert({1, 9, 0});\n    set.insert({2, -1, 9});\n    \n    // {0} {1} {2} {9} {-1}\n    for(const auto\u0026 key : set) {\n        std::cout \u003c\u003c \"{\" \u003c\u003c key \u003c\u003c \"}\" \u003c\u003c std::endl;\n    }\n}\n```\n\n#### Heterogeneous lookups\n\nHeterogeneous overloads allow the usage of other types than `Key` for lookup and erase operations as long as the used types are hashable and comparable to `Key`.\n\nTo activate the heterogeneous overloads in `tsl::sparse_map/set`, the qualified-id `KeyEqual::is_transparent` must be valid. It works the same way as for [`std::map::find`](http://en.cppreference.com/w/cpp/container/map/find). You can either use [`std::equal_to\u003c\u003e`](http://en.cppreference.com/w/cpp/utility/functional/equal_to_void) or define your own function object.\n\nBoth `KeyEqual` and `Hash` will need to be able to deal with the different types.\n\n```c++\n#include \u003cfunctional\u003e\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\n#include \u003ctsl/sparse_map.h\u003e\n\n\nstruct employee {\n    employee(int id, std::string name) : m_id(id), m_name(std::move(name)) {\n    }\n    \n    // Either we include the comparators in the class and we use `std::equal_to\u003c\u003e`...\n    friend bool operator==(const employee\u0026 empl, int empl_id) {\n        return empl.m_id == empl_id;\n    }\n    \n    friend bool operator==(int empl_id, const employee\u0026 empl) {\n        return empl_id == empl.m_id;\n    }\n    \n    friend bool operator==(const employee\u0026 empl1, const employee\u0026 empl2) {\n        return empl1.m_id == empl2.m_id;\n    }\n    \n    \n    int m_id;\n    std::string m_name;\n};\n\n// ... or we implement a separate class to compare employees.\nstruct equal_employee {\n    using is_transparent = void;\n    \n    bool operator()(const employee\u0026 empl, int empl_id) const {\n        return empl.m_id == empl_id;\n    }\n    \n    bool operator()(int empl_id, const employee\u0026 empl) const {\n        return empl_id == empl.m_id;\n    }\n    \n    bool operator()(const employee\u0026 empl1, const employee\u0026 empl2) const {\n        return empl1.m_id == empl2.m_id;\n    }\n};\n\nstruct hash_employee {\n    std::size_t operator()(const employee\u0026 empl) const {\n        return std::hash\u003cint\u003e()(empl.m_id);\n    }\n    \n    std::size_t operator()(int id) const {\n        return std::hash\u003cint\u003e()(id);\n    }\n};\n\n\nint main() {\n    // Use std::equal_to\u003c\u003e which will automatically deduce and forward the parameters\n    tsl::sparse_map\u003cemployee, int, hash_employee, std::equal_to\u003c\u003e\u003e map; \n    map.insert({employee(1, \"John Doe\"), 2001});\n    map.insert({employee(2, \"Jane Doe\"), 2002});\n    map.insert({employee(3, \"John Smith\"), 2003});\n\n    // John Smith 2003\n    auto it = map.find(3);\n    if(it != map.end()) {\n        std::cout \u003c\u003c it-\u003efirst.m_name \u003c\u003c \" \" \u003c\u003c it-\u003esecond \u003c\u003c std::endl;\n    }\n\n    map.erase(1);\n\n\n\n    // Use a custom KeyEqual which has an is_transparent member type\n    tsl::sparse_map\u003cemployee, int, hash_employee, equal_employee\u003e map2;\n    map2.insert({employee(4, \"Johnny Doe\"), 2004});\n\n    // 2004\n    std::cout \u003c\u003c map2.at(4) \u003c\u003c std::endl;\n}\n```\n\n#### Serialization\n\nThe library provides an efficient way to serialize and deserialize a map or a set so that it can be saved to a file or send through the network.\nTo do so, it requires the user to provide a function object for both serialization and deserialization.\n\n```c++\nstruct serializer {\n    // Must support the following types for U: std::uint64_t, float \n    // and std::pair\u003cKey, T\u003e if a map is used or Key for a set.\n    template\u003ctypename U\u003e\n    void operator()(const U\u0026 value);\n};\n```\n\n```c++\nstruct deserializer {\n    // Must support the following types for U: std::uint64_t, float \n    // and std::pair\u003cKey, T\u003e if a map is used or Key for a set.\n    template\u003ctypename U\u003e\n    U operator()();\n};\n```\n\nNote that the implementation leaves binary compatibility (endianness, float binary representation, size of int, ...) of the types it serializes/deserializes in the hands of the provided function objects if compatibility is required.\n\nMore details regarding the `serialize` and `deserialize` methods can be found in the [API](https://tessil.github.io/sparse-map/classtsl_1_1sparse__map.html).\n\n```c++\n#include \u003ccassert\u003e\n#include \u003ccstdint\u003e\n#include \u003cfstream\u003e\n#include \u003ctype_traits\u003e\n#include \u003ctsl/sparse_map.h\u003e\n\n\nclass serializer {\npublic:\n    serializer(const char* file_name) {\n        m_ostream.exceptions(m_ostream.badbit | m_ostream.failbit);\n        m_ostream.open(file_name, std::ios::binary);\n    }\n    \n    template\u003cclass T,\n             typename std::enable_if\u003cstd::is_arithmetic\u003cT\u003e::value\u003e::type* = nullptr\u003e\n    void operator()(const T\u0026 value) {\n        m_ostream.write(reinterpret_cast\u003cconst char*\u003e(\u0026value), sizeof(T));\n    }\n    \n    void operator()(const std::pair\u003cstd::int64_t, std::int64_t\u003e\u0026 value) {\n        (*this)(value.first);\n        (*this)(value.second);\n    }\n\nprivate:\n    std::ofstream m_ostream;\n};\n\nclass deserializer {\npublic:\n    deserializer(const char* file_name) {\n        m_istream.exceptions(m_istream.badbit | m_istream.failbit | m_istream.eofbit);\n        m_istream.open(file_name, std::ios::binary);\n    }\n    \n    template\u003cclass T\u003e\n    T operator()() {\n        T value;\n        deserialize(value);\n        \n        return value;\n    }\n    \nprivate:\n    template\u003cclass T,\n             typename std::enable_if\u003cstd::is_arithmetic\u003cT\u003e::value\u003e::type* = nullptr\u003e\n    void deserialize(T\u0026 value) {\n        m_istream.read(reinterpret_cast\u003cchar*\u003e(\u0026value), sizeof(T));\n    }\n    \n    void deserialize(std::pair\u003cstd::int64_t, std::int64_t\u003e\u0026 value) {\n        deserialize(value.first);\n        deserialize(value.second);\n    }\n\nprivate:\n    std::ifstream m_istream;\n};\n\n\nint main() {\n    const tsl::sparse_map\u003cstd::int64_t, std::int64_t\u003e map = {{1, -1}, {2, -2}, {3, -3}, {4, -4}};\n    \n    \n    const char* file_name = \"sparse_map.data\";\n    {\n        serializer serial(file_name);\n        map.serialize(serial);\n    }\n    \n    {\n        deserializer dserial(file_name);\n        auto map_deserialized = tsl::sparse_map\u003cstd::int64_t, std::int64_t\u003e::deserialize(dserial);\n        \n        assert(map == map_deserialized);\n    }\n    \n    {\n        deserializer dserial(file_name);\n        \n        /**\n         * If the serialized and deserialized map are hash compatibles (see conditions in API), \n         * setting the argument to true speed-up the deserialization process as we don't have \n         * to recalculate the hash of each key. We also know how much space each bucket needs.\n         */\n        const bool hash_compatible = true;\n        auto map_deserialized = \n            tsl::sparse_map\u003cstd::int64_t, std::int64_t\u003e::deserialize(dserial, hash_compatible);\n        \n        assert(map == map_deserialized);\n    }\n} \n```\n\n##### Serialization with Boost Serialization and compression with zlib\n\nIt's possible to use a serialization library to avoid the boilerplate. \n\nThe following example uses Boost Serialization with the Boost zlib compression stream to reduce the size of the resulting serialized file. The example requires C++20 due to the usage of the template parameter list syntax in lambdas, but it can be adapted to less recent versions.\n\n```c++\n#include \u003cboost/archive/binary_iarchive.hpp\u003e\n#include \u003cboost/archive/binary_oarchive.hpp\u003e\n#include \u003cboost/iostreams/filter/zlib.hpp\u003e\n#include \u003cboost/iostreams/filtering_stream.hpp\u003e\n#include \u003cboost/serialization/split_free.hpp\u003e\n#include \u003cboost/serialization/utility.hpp\u003e\n#include \u003ccassert\u003e\n#include \u003ccstdint\u003e\n#include \u003cfstream\u003e\n#include \u003ctsl/sparse_map.h\u003e\n\n\nnamespace boost { namespace serialization {\n    template\u003cclass Archive, class Key, class T\u003e\n    void serialize(Archive \u0026 ar, tsl::sparse_map\u003cKey, T\u003e\u0026 map, const unsigned int version) {\n        split_free(ar, map, version); \n    }\n\n    template\u003cclass Archive, class Key, class T\u003e\n    void save(Archive \u0026 ar, const tsl::sparse_map\u003cKey, T\u003e\u0026 map, const unsigned int /*version*/) {\n        auto serializer = [\u0026ar](const auto\u0026 v) { ar \u0026 v; };\n        map.serialize(serializer);\n    }\n\n    template\u003cclass Archive, class Key, class T\u003e\n    void load(Archive \u0026 ar, tsl::sparse_map\u003cKey, T\u003e\u0026 map, const unsigned int /*version*/) {\n        auto deserializer = [\u0026ar]\u003ctypename U\u003e() { U u; ar \u0026 u; return u; };\n        map = tsl::sparse_map\u003cKey, T\u003e::deserialize(deserializer);\n    }\n}}\n\n\nint main() {\n    tsl::sparse_map\u003cstd::int64_t, std::int64_t\u003e map = {{1, -1}, {2, -2}, {3, -3}, {4, -4}};\n    \n    \n    const char* file_name = \"sparse_map.data\";\n    {\n        std::ofstream ofs;\n        ofs.exceptions(ofs.badbit | ofs.failbit);\n        ofs.open(file_name, std::ios::binary);\n        \n        boost::iostreams::filtering_ostream fo;\n        fo.push(boost::iostreams::zlib_compressor());\n        fo.push(ofs);\n        \n        boost::archive::binary_oarchive oa(fo);\n        \n        oa \u003c\u003c map;\n    }\n    \n    {\n        std::ifstream ifs;\n        ifs.exceptions(ifs.badbit | ifs.failbit | ifs.eofbit);\n        ifs.open(file_name, std::ios::binary);\n        \n        boost::iostreams::filtering_istream fi;\n        fi.push(boost::iostreams::zlib_decompressor());\n        fi.push(ifs);\n        \n        boost::archive::binary_iarchive ia(fi);\n     \n        tsl::sparse_map\u003cstd::int64_t, std::int64_t\u003e map_deserialized;   \n        ia \u003e\u003e map_deserialized;\n        \n        assert(map == map_deserialized);\n    }\n}\n```\n\n### License\n\nThe code is licensed under the MIT license, see the [LICENSE file](LICENSE) for details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftessil%2Fsparse-map","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftessil%2Fsparse-map","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftessil%2Fsparse-map/lists"}