[map] Allow std::move-ing keys into the map

This commit is contained in:
Behdad Esfahbod 2022-11-18 19:17:03 -07:00
parent a1768ad829
commit f657ef7e57
2 changed files with 11 additions and 6 deletions

View File

@ -186,7 +186,7 @@ struct hb_hashmap_t
{
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,
std::move (old_items[i].value));
}
@ -199,7 +199,9 @@ struct hb_hashmap_t
}
template <typename VV>
bool set (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 (hb_ridentity (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 (K key) const
{
@ -208,7 +210,8 @@ struct hb_hashmap_t
return item.is_real () && item == key ? item.value : item_t::default_value ();
}
void del (K key) { set_with_hash (key, hb_hash (key), item_t::default_value (), true); }
void del (const K &key) { set_with_hash (hb_ridentity (key), hb_hash (key), item_t::default_value (), true); }
void del (K&& key) { set_with_hash (std::move (key), hb_hash (key), item_t::default_value (), true); }
/* Has interface. */
typedef const V& value_t;
@ -320,8 +323,8 @@ struct hb_hashmap_t
protected:
template <typename VV>
bool set_with_hash (K key, uint32_t hash, VV&& value, bool is_delete=false)
template <typename KK, typename VV>
bool set_with_hash (KK&& key, uint32_t hash, VV&& value, bool is_delete=false)
{
if (unlikely (!successful)) return false;
if (unlikely ((occupancy + occupancy / 2) >= mask && !resize ())) return false;
@ -337,7 +340,7 @@ struct hb_hashmap_t
population--;
}
item.key = key;
item.key = std::forward<KK> (key);
item.value = std::forward<VV> (value);
item.hash = hash;
item.set_used (true);

View File

@ -224,6 +224,8 @@ main (int argc, char **argv)
{
hb_hashmap_t<hb::unique_ptr<hb_set_t>, hb::unique_ptr<hb_set_t>> m;
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.iter_ref ();
m.keys_ref ();