Pārlūkot izejas kodu

AK: Add DoublyLinkedList::prepend()

Also make it possible to remove() with a value-type Iterator.
Andreas Kling 5 gadi atpakaļ
vecāks
revīzija
61f298faf3
1 mainītis faili ar 27 papildinājumiem un 1 dzēšanām
  1. 27 1
      AK/DoublyLinkedList.h

+ 27 - 1
AK/DoublyLinkedList.h

@@ -95,6 +95,16 @@ public:
         append_node(new Node(value));
         append_node(new Node(value));
     }
     }
 
 
+    void prepend(T&& value)
+    {
+        prepend_node(new Node(move(value)));
+    }
+
+    void prepend(const T& value)
+    {
+        prepend_node(new Node(value));
+    }
+
     bool contains_slow(const T& value) const
     bool contains_slow(const T& value) const
     {
     {
         for (auto* node = m_head; node; node = node->next) {
         for (auto* node = m_head; node; node = node->next) {
@@ -132,7 +142,7 @@ public:
         return end();
         return end();
     }
     }
 
 
-    void remove(Iterator& it)
+    void remove(Iterator it)
     {
     {
         ASSERT(it.m_node);
         ASSERT(it.m_node);
         auto* node = it.m_node;
         auto* node = it.m_node;
@@ -163,11 +173,27 @@ private:
             return;
             return;
         }
         }
         ASSERT(m_tail);
         ASSERT(m_tail);
+        ASSERT(!node->next);
         m_tail->next = node;
         m_tail->next = node;
         node->prev = m_tail;
         node->prev = m_tail;
         m_tail = node;
         m_tail = node;
     }
     }
 
 
+    void prepend_node(Node* node)
+    {
+        if (!m_head) {
+            ASSERT(!m_tail);
+            m_head = node;
+            m_tail = node;
+            return;
+        }
+        ASSERT(m_tail);
+        ASSERT(!node->prev);
+        m_head->prev = node;
+        node->next = m_head;
+        m_head = node;
+    }
+
     Node* head() { return m_head; }
     Node* head() { return m_head; }
     const Node* head() const { return m_head; }
     const Node* head() const { return m_head; }