Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dwarfovich committed Sep 12, 2024
1 parent 9d093b5 commit c2041e2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
21 changes: 8 additions & 13 deletions HW3/cc_lib/include/cc/forward_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ template<typename T>
struct DataNode : Node
{
DataNode() = default;

DataNode(const T& aData) : data { aData } {}
DataNode(const T& aData, Node* nextNode) : Node { nextNode }, data { aData } {}

T data;
Expand Down Expand Up @@ -127,27 +127,22 @@ ForwardList<T, Allocator>::ForwardList()
template<typename T, typename Allocator>
auto ForwardList<T, Allocator>::insert_after(iterator position, const value_type& value) -> iterator
{
using AllocatorTraits = std::allocator_traits<Allocator>;

using NodeAllcoator = typename std::allocator_traits<Allocator>::template rebind_alloc<details::DataNode<T>>;
using namespace details;
using NodeAllcoator = typename std::allocator_traits<Allocator>::template rebind_alloc<DataNode<T>>;
NodeAllcoator nodeAllocator = allocator;
auto* newNode = nodeAllocator.allocate(1);
using NodeAllocatorTraits = std::allocator_traits<decltype(nodeAllocator)>;
// 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<NodeAllcoator>;
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<typename T, typename Allocator>
Expand Down
9 changes: 3 additions & 6 deletions HW3/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,11 @@ int main()
}

cc::ForwardList<int> list;
auto iter = list.begin();
list.insert_after(iter, 77);
//auto iter = list.begin();
//list.insert_after(iter, 77);

std::forward_list<short> 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<int> l2;
Expand Down
17 changes: 15 additions & 2 deletions HW3/tests/forward_list_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,20 @@ TEST(ForwardListTest, ConstructingEmpty)
EXPECT_TRUE(list.empty());
}

TEST(ForwardListTest, InsertionAfterInBeginning)
TEST(ForwardListTest, InsertionBeforeBegin)
{
cc::ForwardList<int> 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<int> list;
list.insert_after(list.begin(), 1);
Expand All @@ -23,7 +36,7 @@ TEST(ForwardListTest, InsertionAfterInBeginning)

TEST(ForwardListTest, ConsequtiveInsertion)
{
const int inserts = 4;
const int inserts = 5;

cc::ForwardList<int> list;
auto iter = list.begin();
Expand Down

0 comments on commit c2041e2

Please sign in to comment.