From f19ca23f866b908625190394d419790e9f32737b Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 17 Apr 2021 16:38:44 -0500 Subject: [PATCH] [ox/std] Add move operators to HashMap and cleanup noexcept correctness (synced from 39dc09e3d35f4b5914daddcf86c4aea931a97f60) --- src/ox/std/hashmap.hpp | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/src/ox/std/hashmap.hpp b/src/ox/std/hashmap.hpp index 62e22d9c7..5c9a3bf34 100644 --- a/src/ox/std/hashmap.hpp +++ b/src/ox/std/hashmap.hpp @@ -30,13 +30,17 @@ class HashMap { public: explicit HashMap(std::size_t size = 100); - HashMap(HashMap &other); + HashMap(const HashMap &other); + + HashMap(HashMap &&other); ~HashMap(); bool operator==(const HashMap &other) const; - HashMap &operator=(HashMap &other); + HashMap &operator=(const HashMap &other); + + HashMap &operator=(HashMap &&other); /** * K is assumed to be a null terminated string. @@ -46,14 +50,14 @@ class HashMap { /** * K is assumed to be a null terminated string. */ - Result at(K key); + Result at(K key) noexcept; /** * K is assumed to be a null terminated string. */ - Result at(K key) const; + Result at(K key) const noexcept; - bool contains(K key) const; + bool contains(K key) const noexcept; std::size_t size() const noexcept; @@ -84,10 +88,18 @@ HashMap::HashMap(std::size_t size): m_pairs(size) { } template -HashMap::HashMap(HashMap &other) { +HashMap::HashMap(const HashMap &other) { m_pairs = other.m_pairs; } +template +HashMap::HashMap(HashMap &&other) { + m_keys = ox::move(other.m_keys); + m_pairs = ox::move(other.m_pairs); + other.m_keys = {}; + other.m_pairs = {}; +} + template HashMap::~HashMap() { for (std::size_t i = 0; i < m_pairs.size(); i++) { @@ -110,13 +122,23 @@ bool HashMap::operator==(const HashMap &other) const { } template -HashMap &HashMap::operator=(HashMap &other) { +HashMap &HashMap::operator=(const HashMap &other) { this->~HashMap(); m_keys = other.m_keys; m_pairs = other.m_pairs; return *this; } +template +HashMap &HashMap::operator=(HashMap &&other) { + this->~HashMap(); + m_keys = ox::move(other.m_keys); + m_pairs = ox::move(other.m_pairs); + other.m_keys = {}; + other.m_pairs = {}; + return *this; +} + template T &HashMap::operator[](K k) { auto &p = access(m_pairs, k); @@ -132,7 +154,7 @@ T &HashMap::operator[](K k) { } template -Result HashMap::at(K k) { +Result HashMap::at(K k) noexcept { auto p = access(m_pairs, k); if (!p) { AllocAlias v; @@ -142,7 +164,7 @@ Result HashMap::at(K k) { } template -Result HashMap::at(K k) const { +Result HashMap::at(K k) const noexcept { auto p = access(m_pairs, k); if (!p) { AllocAlias v; @@ -152,7 +174,7 @@ Result HashMap::at(K k) const { } template -bool HashMap::contains(K k) const { +bool HashMap::contains(K k) const noexcept { return access(m_pairs, k) != nullptr; }