greatbridf 2 жил өмнө
parent
commit
06e54f821f

+ 33 - 9
include/types/list.hpp

@@ -52,7 +52,7 @@ private:
         }
 
         explicit node(NodeValue&& v) noexcept
-            : value(v)
+            : value(move(v))
         {
         }
 
@@ -75,6 +75,7 @@ public:
         iterator(iterator&& iter) noexcept
             : n(iter.n)
         {
+            iter.n = nullptr;
         }
 
         iterator& operator=(const iterator& iter)
@@ -162,6 +163,13 @@ private:
         return (static_cast<sentry_node_type*>(head))->value;
     }
 
+    void destroy(void)
+    {
+        clear();
+        allocator_traits<sentry_allocator_type>::deconstruct_and_deallocate(static_cast<sentry_node_type*>(head));
+        allocator_traits<sentry_allocator_type>::deconstruct_and_deallocate(static_cast<sentry_node_type*>(tail));
+    }
+
 public:
     list() noexcept
         // size is stored in the 'head' node
@@ -179,6 +187,14 @@ public:
             push_back(item);
     }
 
+    list(list&& v)
+        : head(v.head)
+        , tail(v.tail)
+    {
+        v.head = nullptr;
+        v.tail = nullptr;
+    }
+
     list& operator=(const list& v)
     {
         clear();
@@ -187,13 +203,21 @@ public:
         return *this;
     }
 
+    list& operator=(list&& v)
+    {
+        destroy();
+
+        head = v.head;
+        tail = v.tail;
+        v.head = nullptr;
+        v.tail = nullptr;
+
+        return *this;
+    }
+
     ~list() noexcept
     {
-        for (auto iter = begin(); iter != end(); ++iter) {
-            erase(iter);
-        }
-        allocator_traits<sentry_allocator_type>::deconstruct_and_deallocate(static_cast<sentry_node_type*>(head));
-        allocator_traits<sentry_allocator_type>::deconstruct_and_deallocate(static_cast<sentry_node_type*>(tail));
+        destroy();
     }
 
     iterator_type find(const value_type& v) noexcept
@@ -235,7 +259,7 @@ public:
     // insert the value v in front of the given iterator
     iterator_type insert(const iterator_type& iter, value_type&& v) noexcept
     {
-        node_base_type* new_node = allocator_traits<allocator_type>::allocate_and_construct(types::move(v));
+        node_base_type* new_node = allocator_traits<allocator_type>::allocate_and_construct(move(v));
         iterator_type ret(new_node);
         iter._node()->prev->connect(new_node);
         new_node->connect(iter._node());
@@ -251,7 +275,7 @@ public:
 
     void push_back(value_type&& v) noexcept
     {
-        insert(end(), v);
+        insert(end(), move(v));
     }
 
     template <typename... Args>
@@ -267,7 +291,7 @@ public:
 
     void push_front(value_type&& v) noexcept
     {
-        insert(begin(), v);
+        insert(begin(), move(v));
     }
 
     template <typename... Args>

+ 12 - 0
include/types/string.hpp

@@ -31,6 +31,10 @@ public:
         : inner_vector_type((const inner_vector_type&)str)
     {
     }
+    string(string&& str)
+        : inner_vector_type((inner_vector_type &&) move(str))
+    {
+    }
     string& append(const char* str, size_type n = npos)
     {
         this->pop_back();
@@ -47,6 +51,10 @@ public:
     {
         return this->append(str.data());
     }
+    string& append(string&& str)
+    {
+        return this->append(str.data());
+    }
     string& operator+=(const char c)
     {
         *this->back() = c;
@@ -61,6 +69,10 @@ public:
     {
         return this->append(str);
     }
+    string& operator+=(string&& str)
+    {
+        return this->append(move(str));
+    }
     string substr(size_type pos, size_type n = npos)
     {
         return string(this->m_arr + pos, n);

+ 13 - 1
include/types/vector.hpp

@@ -38,6 +38,7 @@ public:
         iterator(iterator&& iter) noexcept
             : p(iter.p)
         {
+            iter.p = nullptr;
         }
 
         iterator& operator=(const iterator& iter)
@@ -128,6 +129,17 @@ public:
             push_back(item);
     }
 
+    vector(vector&& arr) noexcept
+    {
+        m_arr = arr.m_arr;
+        m_capacity = arr.m_capacity;
+        m_size = arr.m_size;
+
+        arr.m_arr = nullptr;
+        arr.m_capacity = 0;
+        arr.m_size = 0;
+    }
+
     ~vector() noexcept
     {
         resize(0);
@@ -226,7 +238,7 @@ public:
     {
         if (m_size == m_capacity)
             resize(m_capacity * 2);
-        allocator_traits<allocator_type>::construct(m_arr + m_size, v);
+        allocator_traits<allocator_type>::construct(m_arr + m_size, move(v));
         ++m_size;
     }