From c2041e22219d6a3231e09846b2f2507c7d1df4d2 Mon Sep 17 00:00:00 2001 From: Tim Gilevich Date: Thu, 12 Sep 2024 23:14:21 +0300 Subject: [PATCH] WIP --- HW3/cc_lib/include/cc/forward_list.h | 21 ++++++++------------- HW3/src/main.cpp | 9 +++------ HW3/tests/forward_list_test.cpp | 17 +++++++++++++++-- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/HW3/cc_lib/include/cc/forward_list.h b/HW3/cc_lib/include/cc/forward_list.h index 56d9a2d..bf7c335 100644 --- a/HW3/cc_lib/include/cc/forward_list.h +++ b/HW3/cc_lib/include/cc/forward_list.h @@ -19,7 +19,7 @@ template struct DataNode : Node { DataNode() = default; - + DataNode(const T& aData) : data { aData } {} DataNode(const T& aData, Node* nextNode) : Node { nextNode }, data { aData } {} T data; @@ -127,27 +127,22 @@ ForwardList::ForwardList() template auto ForwardList::insert_after(iterator position, const value_type& value) -> iterator { - using AllocatorTraits = std::allocator_traits; - - using NodeAllcoator = typename std::allocator_traits::template rebind_alloc>; + using namespace details; + using NodeAllcoator = typename std::allocator_traits::template rebind_alloc>; NodeAllcoator nodeAllocator = allocator; auto* newNode = nodeAllocator.allocate(1); - using NodeAllocatorTraits = std::allocator_traits; - // NodeAllocatorTraits::construct(nodeAllocator, newNode, value); - if(position == begin()){ - NodeAllocatorTraits::construct(nodeAllocator, newNode, value, nullptr); - } else{ - auto next = std::next(position); - NodeAllocatorTraits::construct(nodeAllocator, newNode, value, next.getNode()); - } + using NodeAllocatorTraits = std::allocator_traits; + NodeAllocatorTraits::construct(nodeAllocator, newNode, value); if (empty()) { head.nextNode = newNode; } else { + newNode->nextNode = position.getNode()->nextNode; position.getNode()->nextNode = newNode; } + ++listSize; - return {this, newNode}; + return { this, newNode }; } template diff --git a/HW3/src/main.cpp b/HW3/src/main.cpp index ac3aa94..a28aa7d 100644 --- a/HW3/src/main.cpp +++ b/HW3/src/main.cpp @@ -47,14 +47,11 @@ int main() } cc::ForwardList list; - auto iter = list.begin(); - list.insert_after(iter, 77); + //auto iter = list.begin(); + //list.insert_after(iter, 77); std::forward_list l; - auto it1 = l.cbefore_begin(); - auto it2 = l.cbegin(); - //++it2; - auto it3 = l.cend(); + l.insert_after(l.before_begin(), 88); /*std::cout << std::boolalpha << (l.cbegin() == l.cbefore_begin()) << '\n'; std::cout << sizeof(list) << ' ' << sizeof(l) << '\n'; std::forward_list l2; diff --git a/HW3/tests/forward_list_test.cpp b/HW3/tests/forward_list_test.cpp index 32d6986..5a5ddcf 100644 --- a/HW3/tests/forward_list_test.cpp +++ b/HW3/tests/forward_list_test.cpp @@ -8,7 +8,20 @@ TEST(ForwardListTest, ConstructingEmpty) EXPECT_TRUE(list.empty()); } -TEST(ForwardListTest, InsertionAfterInBeginning) +TEST(ForwardListTest, InsertionBeforeBegin) +{ + cc::ForwardList list; + list.insert_after(list.before_begin(), 1); + EXPECT_EQ(*list.begin(), 1); + + list.insert_after(list.before_begin(), 2); + EXPECT_EQ(*list.begin(), 2); + + list.insert_after(list.before_begin(), 3); + EXPECT_EQ(*list.begin(), 3); +} + +TEST(ForwardListTest, InsertionAfterBegin) { cc::ForwardList list; list.insert_after(list.begin(), 1); @@ -23,7 +36,7 @@ TEST(ForwardListTest, InsertionAfterInBeginning) TEST(ForwardListTest, ConsequtiveInsertion) { - const int inserts = 4; + const int inserts = 5; cc::ForwardList list; auto iter = list.begin();