From 2fdde2e679b2138d70921f192a0c364be9510cbf Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Thu, 26 Jul 2018 10:55:47 +0900 Subject: [PATCH] [coco] Support 'prepend' operation (#822) 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 --- contrib/coco/core/include/coco/ADT/DLinkedList.h | 13 ++++++++++++ contrib/coco/core/src/ADT/DLinkedList.test.cpp | 26 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/contrib/coco/core/include/coco/ADT/DLinkedList.h b/contrib/coco/core/include/coco/ADT/DLinkedList.h index f29951f..20b8d3a 100644 --- a/contrib/coco/core/include/coco/ADT/DLinkedList.h +++ b/contrib/coco/core/include/coco/ADT/DLinkedList.h @@ -75,6 +75,19 @@ template struct DLinkedList } public: + void prepend(Child *child) + { + if (empty()) + { + enlist(child); + } + else + { + child->insertBefore(_head); + } + } + + public: void append(Child *child) { if (empty()) diff --git a/contrib/coco/core/src/ADT/DLinkedList.test.cpp b/contrib/coco/core/src/ADT/DLinkedList.test.cpp index 8138cce..0883838 100644 --- a/contrib/coco/core/src/ADT/DLinkedList.test.cpp +++ b/contrib/coco/core/src/ADT/DLinkedList.test.cpp @@ -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; +} -- 2.7.4