From a81b52a787af202604d72e11d7029fef22a4df9f Mon Sep 17 00:00:00 2001 From: KliPPy Date: Thu, 12 Jul 2018 20:25:22 +0200 Subject: [PATCH] Add move semantics for StringHashMap (#589) * Move semantics for StringHashMap * style * Update sm_stringhashmap.h --- public/sm_stringhashmap.h | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/public/sm_stringhashmap.h b/public/sm_stringhashmap.h index def1540b..96e57e16 100644 --- a/public/sm_stringhashmap.h +++ b/public/sm_stringhashmap.h @@ -129,6 +129,16 @@ public: return true; } + bool retrieve(const char *aKey, T **aResult) + { + CharsAndLength key(aKey); + Result r = internal_.find(key); + if (!r.found()) + return false; + *aResult = &r->value; + return true; + } + Result find(const char *aKey) { CharsAndLength key(aKey); @@ -142,32 +152,31 @@ public: return r.found(); } - bool replace(const char *aKey, const T &value) + template + bool replace(const char *aKey, UV &&value) { CharsAndLength key(aKey); Insert i = internal_.findForAdd(key); if (!i.found()) { memory_used_ += key.length() + 1; - if (!internal_.add(i)) + if (!internal_.add(i, aKey)) return false; - i->key = aKey; } - i->value = value; + i->value = ke::Forward(value); return true; } - bool insert(const char *aKey, const T &value) + template + bool insert(const char *aKey, UV &&value) { CharsAndLength key(aKey); Insert i = internal_.findForAdd(key); if (i.found()) return false; - if (!internal_.add(i)) + if (!internal_.add(i, aKey, ke::Forward(value))) return false; memory_used_ += key.length() + 1; - i->key = aKey; - i->value = value; return true; } @@ -225,9 +234,8 @@ public: // Only value needs to be set after. bool add(Insert &i, const char *aKey) { - if (!internal_.add(i)) + if (!internal_.add(i, aKey)) return false; - i->key = aKey; return true; }