Tessil / sparse-map

C++ implementation of a memory efficient hash map and hash set
MIT License
334 stars 36 forks source link

Supporting lambdas #14

Open MathieuDutSik opened 3 years ago

MathieuDutSik commented 3 years ago

The code

auto fctHash=[](size_t val) -> size_t {
  return val;
};
auto fctEqual=[](size_t val1, size_t val2) -> bool {
  return val1 == val2;
};
tsl::sparse_map<size_t, int, decltype(fctHash), decltype(fctEqual)> V({}, fctHash, fctEqual);

does not compile. This is not dramatic as the following does:

std::function<size_t(size_t)> fctHash=[](size_t val) -> size_t {
  return val;
};
std::function<bool(size_t,size_t)> fctEqual=[](size_t val1, size_t val2) -> bool {
  return val1 == val2;
};
tsl::sparse_map<size_t, int, std::function<size_t(size_t)>, std::function<bool(size_t,size_t)>> V({}, fctHash, fctEqual);

but could this be addressed? This would allow better compatibility with the std::unordered_map for which tsl::sparse_map is a replacement.

Tessil commented 3 years ago

Hi,

Thank you for the report, it can be fixed. Currently the rehash_impl creates a new sparse_hash and move/copy the values from the old hash map to the new one and then swap the two maps but unfortunately lambdas, and thus Hash and KeyEqual, are not swappable so sparse_hash::swap can't be called. It would be possible to just create a new m_sparse_buckets array and swap it instead of creating a full sparse_hash.

I will check to fix the problem when I can find some time to work on it.

Thibaut