[map] Make keys moveable

This commit is contained in:
Behdad Esfahbod 2022-11-21 20:53:44 -07:00
parent cf20d2ec5d
commit 86a763c651
2 changed files with 8 additions and 8 deletions

View File

@ -186,7 +186,7 @@ struct hb_hashmap_t
{ {
if (old_items[i].is_real ()) if (old_items[i].is_real ())
{ {
set_with_hash (old_items[i].key, set_with_hash (std::move (old_items[i].key),
old_items[i].hash, old_items[i].hash,
std::move (old_items[i].value)); std::move (old_items[i].value));
} }
@ -198,8 +198,8 @@ struct hb_hashmap_t
return true; return true;
} }
template <typename VV> template <typename KK, typename VV>
bool set_with_hash (const K &key, uint32_t hash, VV&& value, bool is_delete=false) bool set_with_hash (KK&& key, uint32_t hash, VV&& value, bool is_delete=false)
{ {
if (unlikely (!successful)) return false; if (unlikely (!successful)) return false;
if (unlikely ((occupancy + occupancy / 2) >= mask && !resize ())) return false; if (unlikely ((occupancy + occupancy / 2) >= mask && !resize ())) return false;
@ -215,7 +215,7 @@ struct hb_hashmap_t
population--; population--;
} }
item.key = key; item.key = std::forward<KK> (key);
item.value = std::forward<VV> (value); item.value = std::forward<VV> (value);
item.hash = hash; item.hash = hash;
item.set_used (true); item.set_used (true);
@ -230,6 +230,8 @@ struct hb_hashmap_t
template <typename VV> template <typename VV>
bool set (const K &key, VV&& value) { return set_with_hash (key, hb_hash (key), std::forward<VV> (value)); } bool set (const K &key, VV&& value) { return set_with_hash (key, hb_hash (key), std::forward<VV> (value)); }
template <typename VV>
bool set (K &&key, VV&& value) { return set_with_hash (std::move (key), hb_hash (key), std::forward<VV> (value)); }
const V& get_with_hash (const K &key, uint32_t hash) const const V& get_with_hash (const K &key, uint32_t hash) const
{ {

View File

@ -228,10 +228,8 @@ main (int argc, char **argv)
{ {
hb_hashmap_t<hb::unique_ptr<hb_set_t>, hb::unique_ptr<hb_set_t>> m; hb_hashmap_t<hb::unique_ptr<hb_set_t>, hb::unique_ptr<hb_set_t>> m;
// Doesn't work. m.set (hb::unique_ptr<hb_set_t> (hb_set_get_empty ()),
// See commit f657ef7e57c889309c2d9d37934368ca255f9d5b and its revert. hb::unique_ptr<hb_set_t> (hb_set_get_empty ()));
//m.set (hb::unique_ptr<hb_set_t> (hb_set_get_empty ()),
// hb::unique_ptr<hb_set_t> (hb_set_get_empty ()));
m.get (hb::unique_ptr<hb_set_t> (hb_set_get_empty ())); m.get (hb::unique_ptr<hb_set_t> (hb_set_get_empty ()));
m.iter_ref (); m.iter_ref ();
m.keys_ref (); m.keys_ref ();