Browse Source

Change the signature of the new HashMap Insert() function. Do not allow user to specify findExisting flag. As the meaning of the bool return value is reverse to std::map::insert(), do not return it in a pair to avoid confusion, but rather in a separate bool value which is passed by reference. Add corresponding function to HashSet. Edit comments.

Lasse Öörni 9 years ago
parent
commit
91b799d666

+ 10 - 37
Source/Urho3D/Container/HashMap.h

@@ -362,6 +362,15 @@ public:
         return Iterator(InsertNode(pair.first_, pair.second_));
         return Iterator(InsertNode(pair.first_, pair.second_));
     }
     }
 
 
+    /// Insert a pair. Return iterator and set exists flag according to whether the key already existed.
+    Iterator Insert(const Pair<T, U>& pair, bool& exists)
+    {
+        unsigned oldSize = Size();
+        Iterator ret(InsertNode(pair.first_, pair.second_));
+        exists = (Size() == oldSize);
+        return ret;
+    }
+
     /// Insert a map.
     /// Insert a map.
     void Insert(const HashMap<T, U>& map)
     void Insert(const HashMap<T, U>& map)
     {
     {
@@ -385,42 +394,6 @@ public:
             InsertNode(*it++);
             InsertNode(*it++);
     }
     }
 
 
-    /// Insert a key and value and return iterator to the value and if the value was already added.
-    Pair<Iterator, bool> Insert(const T& key, const U& value, bool findExisting = true)
-    {
-        // If no pointers yet, allocate with minimum bucket count
-        if (!ptrs_)
-        {
-            AllocateBuckets(Size(), MIN_BUCKETS);
-            Rehash();
-        }
-
-        unsigned hashKey = Hash(key);
-
-        if (findExisting)
-        {
-            // If exists, just change the value
-            Node* existing = FindNode(key, hashKey);
-            if (existing)
-            {
-                existing->pair_.second_ = value;
-                return  Pair<T, U>(Iterator(existing), true);
-            }
-        }
-
-        Node* newNode = InsertNode(Tail(), key, value);
-        newNode->down_ = Ptrs()[hashKey];
-        Ptrs()[hashKey] = newNode;
-
-        // Rehash if the maximum load factor has been exceeded
-        if (Size() > NumBuckets() * MAX_LOAD_FACTOR)
-        {
-            AllocateBuckets(Size(), NumBuckets() << 1);
-            Rehash();
-        }
-        return  Pair<T, U>(Iterator(newNode), false);
-    }
-
     /// Erase a pair by key. Return true if was found.
     /// Erase a pair by key. Return true if was found.
     bool Erase(const T& key)
     bool Erase(const T& key)
     {
     {
@@ -581,7 +554,7 @@ public:
         return FindNode(key, hashKey) != 0;
         return FindNode(key, hashKey) != 0;
     }
     }
 
 
-    /// Return true if key found.
+    /// Try to copy value to output. Return true if was found.
     bool TryGetValue(const T& key, U& out)
     bool TryGetValue(const T& key, U& out)
     {
     {
         if (!ptrs_)
         if (!ptrs_)

+ 9 - 0
Source/Urho3D/Container/HashSet.h

@@ -300,6 +300,15 @@ public:
         return Iterator(newNode);
         return Iterator(newNode);
     }
     }
 
 
+    /// Insert a key. Return an iterator and set exists flag according to whether the key already existed.
+    Iterator Insert(const T& key, bool& exists)
+    {
+        unsigned oldSize = Size();
+        Iterator ret = Insert(key);
+        exists = (Size() == oldSize);
+        return ret;
+    }
+
     /// Insert a set.
     /// Insert a set.
     void Insert(const HashSet<T>& set)
     void Insert(const HashSet<T>& set)
     {
     {

+ 6 - 10
Source/Urho3D/Container/Vector.h

@@ -300,8 +300,7 @@ public:
         Resize(size_ - length, 0);
         Resize(size_ - length, 0);
     }
     }
 
 
-    /// Erase a range of elements by swapping elements from the end of the array in order to reduce the amount of
-    /// data copied. The order of existing elements will be changed, if ordering needs to be retained use Erase!
+    /// Erase a range of elements by swapping elements from the end of the array.
     void EraseSwap(unsigned pos, unsigned length = 1)
     void EraseSwap(unsigned pos, unsigned length = 1)
     {
     {
         unsigned shiftStartIndex = pos + length;
         unsigned shiftStartIndex = pos + length;
@@ -348,7 +347,7 @@ public:
         return Begin() + pos;
         return Begin() + pos;
     }
     }
 
 
-    /// Erase an element if found.
+    /// Erase an element by value. Return true if was found and erased.
     bool Remove(const T& value)
     bool Remove(const T& value)
     {
     {
         Iterator i = Find(value);
         Iterator i = Find(value);
@@ -361,8 +360,7 @@ public:
             return false;
             return false;
     }
     }
 
 
-    /// Erase an element if found by swapping with the last element in order to reduce the amount of
-    /// data copied. The order of existing elements will be changed, if ordering needs to be retained use Remove!
+    /// Erase an element by value by swapping with the last element. Return true if was found and erased.
     bool RemoveSwap(const T& value)
     bool RemoveSwap(const T& value)
     {
     {
         Iterator i = Find(value);
         Iterator i = Find(value);
@@ -842,8 +840,7 @@ public:
         return Begin() + pos;
         return Begin() + pos;
     }
     }
 
 
-    /// Erase a range of elements by swapping elements from the end of the array in order to reduce the amount of
-    /// data copied. The order of existing elements will be changed, if ordering needs to be retained use Erase!
+    /// Erase a range of elements by swapping elements from the end of the array.
     void EraseSwap(unsigned pos, unsigned length = 1)
     void EraseSwap(unsigned pos, unsigned length = 1)
     {
     {
         unsigned shiftStartIndex = pos + length;
         unsigned shiftStartIndex = pos + length;
@@ -867,7 +864,7 @@ public:
         Resize(newSize);
         Resize(newSize);
     }
     }
 
 
-    /// Erase an element if found.
+    /// Erase an element by value. Return true if was found and erased.
     bool Remove(const T& value)
     bool Remove(const T& value)
     {
     {
         Iterator i = Find(value);
         Iterator i = Find(value);
@@ -880,8 +877,7 @@ public:
             return false;
             return false;
     }
     }
 
 
-    /// Erase an element if found by swapping with the last element in order to reduce the amount of
-    /// data copied. The order of existing elements will be changed, if ordering needs to be retained use Remove!
+    /// Erase an element by value by swapping with the last element. Return true if was found and erased.
     bool RemoveSwap(const T& value)
     bool RemoveSwap(const T& value)
     {
     {
         Iterator i = Find(value);
         Iterator i = Find(value);