Browse Source

When copy-constructing a HashMap, HashSet or List, reserve initial capacity according to the size of the object being copied.
Fixed WeakPtr's operator < using wrong type argument.

Lasse Öörni 12 years ago
parent
commit
949bc97ffe

+ 2 - 4
Source/Engine/Container/HashMap.h

@@ -166,8 +166,8 @@ public:
     /// Construct from another hash map.
     HashMap(const HashMap<T, U>& map)
     {
-        // Reserve the tail node
-        allocator_ = AllocatorInitialize(sizeof(Node));
+        // Reserve the tail node + initial capacity according to the map's size
+        allocator_ = AllocatorInitialize(sizeof(Node), map.Size() + 1);
         head_ = tail_ = ReserveNode();
         *this = map;
     }
@@ -587,7 +587,6 @@ private:
     /// Reserve a node.
     Node* ReserveNode()
     {
-        assert(allocator_);
         Node* newNode = static_cast<Node*>(AllocatorReserve(allocator_));
         new(newNode) Node();
         return newNode;
@@ -596,7 +595,6 @@ private:
     /// Reserve a node with specified key and value.
     Node* ReserveNode(const T& key, const U& value)
     {
-        assert(allocator_);
         Node* newNode = static_cast<Node*>(AllocatorReserve(allocator_));
         new(newNode) Node(key, value);
         return newNode;

+ 2 - 4
Source/Engine/Container/HashSet.h

@@ -136,8 +136,8 @@ public:
     /// Construct from another hash set.
     HashSet(const HashSet<T>& set)
     {
-        // Reserve the tail node
-        allocator_ = AllocatorInitialize(sizeof(Node));
+        // Reserve the tail node + initial capacity according to the set's size
+        allocator_ = AllocatorInitialize(sizeof(Node), set.Size() + 1);
         head_ = tail_ = ReserveNode();
         *this = set;
     }
@@ -517,8 +517,6 @@ private:
     /// Reserve a node with specified key.
     Node* ReserveNode(const T& key)
     {
-        if (!allocator_)
-            allocator_ = AllocatorInitialize(sizeof(Node));
         Node* newNode = static_cast<Node*>(AllocatorReserve(allocator_));
         new(newNode) Node(key);
         return newNode;

+ 2 - 1
Source/Engine/Container/List.h

@@ -130,7 +130,8 @@ public:
     /// Construct from another list.
     List(const List<T>& list)
     {
-        allocator_ = AllocatorInitialize(sizeof(Node));
+        // Reserve the tail node + initial capacity according to the list's size
+        allocator_ = AllocatorInitialize(sizeof(Node), list.Size() + 1);
         head_ = tail_ = ReserveNode();
         *this = list;
     }

+ 1 - 1
Source/Engine/Container/Ptr.h

@@ -308,7 +308,7 @@ public:
     /// Test for inequality with another weak pointer.
     bool operator != (const WeakPtr<T>& rhs) const { return ptr_ != rhs.ptr_ || refCount_ != rhs.refCount_; }
     /// Test for less than with another weak pointer.
-    bool operator < (const SharedPtr<T>& rhs) const { return ptr_ < rhs.ptr_; }
+    bool operator < (const WeakPtr<T>& rhs) const { return ptr_ < rhs.ptr_; }
     /// Convert to a raw pointer, null if the object is expired.
     operator T* () const { return Get(); }