martinus / robin-hood-hashing

Fast & memory efficient hashtable based on robin hood hashing for C++11/14/17/20
https://gitter.im/martinus/robin-hood-hashing
MIT License
1.5k stars 142 forks source link

Can I use cereal with robin_hood? #128

Closed MitraDarja closed 3 years ago

MitraDarja commented 3 years ago

Hi,

is there a way to use robin_hood::unordered_map with cereal?

I used the example from this website (https://reposhub.com/cpp/serialization/USCiLab-cereal.html) for a really simple cereal example with std::unordered_map and exchanged std::unordered_map with robin_hood::unordered_map, but run in a static assertion.

static assertion failed: cereal could not find any output serialization functions for the provided type and archive combination. 

Any help or hint, how to make this work, would be highly appreciated. Thanks. :)

slavenf commented 3 years ago

Hi.

See last several lines of file robin_hood.h. There are these aliases:

template <typename Key, typename T, typename Hash = hash<Key>,
          typename KeyEqual = std::equal_to<Key>, size_t MaxLoadFactor100 = 80>
using unordered_flat_map = detail::Table<true, MaxLoadFactor100, Key, T, Hash, KeyEqual>;

template <typename Key, typename T, typename Hash = hash<Key>,
          typename KeyEqual = std::equal_to<Key>, size_t MaxLoadFactor100 = 80>
using unordered_node_map = detail::Table<false, MaxLoadFactor100, Key, T, Hash, KeyEqual>;

You have to write serialization functions for these containers, something like this:

template <typename Archive, typename Key, typename T, typename Hash, typename KeyEqual, size_t MaxLoadFactor100>
void serialize
(
    Archive& archive, 
    robin_hood::unordered_flat_map<Key, T, Hash, KeyEqual, MaxLoadFactor100>& map
{
    // ...
}

Similar is for other containers.

You can use separate save and load functions. See documentation at https://uscilab.github.io/cereal/serialization_functions.html

P.S. I have never used cereal serialization library but I think that in this case separate save and load functions must be defined. My intuition tells me that this problem cannot be solved by single serialize function.

MitraDarja commented 3 years ago

Ah okay, I got confused reading robin_hood.h. Thanks for the help! :D