[Glacier] Add HashMap move semantics.

This commit is contained in:
Drew Galbraith 2024-01-11 18:29:29 -08:00
parent d74918409c
commit be392252a4
1 changed files with 19 additions and 4 deletions

View File

@ -18,9 +18,8 @@ class HashMap {
HashMap() = default; HashMap() = default;
HashMap(const HashMap&) = delete; HashMap(const HashMap&) = delete;
HashMap& operator=(const HashMap&) = delete; HashMap& operator=(const HashMap&) = delete;
// TODO: Implement Move. HashMap(HashMap&&);
HashMap(HashMap&&) = delete; HashMap& operator=(HashMap&&);
HashMap& operator=(HashMap&&) = delete;
// Accessors. // Accessors.
uint64_t size() { return size_; } uint64_t size() { return size_; }
@ -63,6 +62,21 @@ class HashMap {
void ResizeIfNecessary(); void ResizeIfNecessary();
}; };
template <typename K, typename V, class H>
HashMap<K, V, H>::HashMap(HashMap&& other) {
data_ = glcr::Move(other.data_);
size_ = other.size_;
other.size_ = 0;
}
template <typename K, typename V, class H>
HashMap<K, V, H>& HashMap<K, V, H>::operator=(HashMap&& other) {
data_ = glcr::Move(other.data_);
size_ = other.size_;
other.size_ = 0;
return *this;
}
template <typename K, typename V, class H> template <typename K, typename V, class H>
V& HashMap<K, V, H>::at(const K& key) { V& HashMap<K, V, H>::at(const K& key) {
uint64_t hc = H()(key); uint64_t hc = H()(key);
@ -74,7 +88,8 @@ V& HashMap<K, V, H>::at(const K& key) {
} }
} }
// TODO: Add a failure mode here instead of constructing an object. // TODO: Add a failure mode here instead of constructing an object.
ll.PushFront({key, {}}); K k2 = key;
ll.PushFront({glcr::Move(k2), {}});
return ll.PeekFront().second(); return ll.PeekFront().second();
} }