[coco] Support 'prepend' operation (#822)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 26 Jul 2018 01:55:47 +0000 (10:55 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Thu, 26 Jul 2018 01:55:47 +0000 (10:55 +0900)
This commit implements 'prepend' operation in 'DLinkedList' trait.

Now, users are able to insert Child element at the head of a list.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/coco/core/include/coco/ADT/DLinkedList.h
contrib/coco/core/src/ADT/DLinkedList.test.cpp

index f29951f..20b8d3a 100644 (file)
@@ -75,6 +75,19 @@ template<typename Child, typename Parent> struct DLinkedList
     }
 
   public:
+    void prepend(Child *child)
+    {
+      if (empty())
+      {
+        enlist(child);
+      }
+      else
+      {
+        child->insertBefore(_head);
+      }
+    }
+
+  public:
     void append(Child *child)
     {
       if (empty())
index 8138cce..0883838 100644 (file)
@@ -114,3 +114,29 @@ TEST(ADT_DLINKED_LINK, insertBefore)
   delete parent;
   delete link;
 }
+
+TEST(ADT_DLINKED_LINK, prepend_after_append)
+{
+  auto link = new coco::PtrLink<::Child, ::Parent>{};
+  auto parent = new ::Parent{link};
+
+  auto child_1 = new ::Child{link};
+  auto child_2 = new ::Child{link};
+
+  parent->children()->append(child_1);
+  parent->children()->prepend(child_2);
+
+  ASSERT_EQ(child_2->next(), child_1);
+
+  ASSERT_EQ(child_1->parent(), parent);
+  ASSERT_EQ(child_1->prev(), child_2);
+  ASSERT_EQ(child_1->next(), nullptr);
+
+  ASSERT_EQ(parent->children()->head(), child_2);
+  ASSERT_EQ(parent->children()->tail(), child_1);
+
+  delete child_2;
+  delete child_1;
+  delete parent;
+  delete link;
+}