Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dwarfovich committed Sep 13, 2024
1 parent ed63ec4 commit 2520da5
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 17 deletions.
53 changes: 38 additions & 15 deletions HW3/cc_lib/include/cc/forward_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,16 @@ class ForwardList
ForwardList();

allocator_type get_allocator() const noexcept;
iterator before_begin();
const_iterator cbefore_begin() const;
iterator begin();
const_iterator cbegin() const;
iterator end();
const_iterator cend() const;
iterator insert_after(iterator position, const value_type& value);
iterator before_begin() noexcept;
const_iterator cbefore_begin() const noexcept;
iterator begin() noexcept;
const_iterator cbegin() const noexcept;
iterator end() noexcept;
const_iterator cend() const noexcept;
bool empty() const noexcept;
std::size_t size();
std::size_t size() noexcept;
iterator insert_after(iterator position, const value_type& value);
iterator erase_after(iterator position);

private: // data
Allocator allocator;
Expand Down Expand Up @@ -146,37 +147,59 @@ auto ForwardList<T, Allocator>::insert_after(iterator position, const value_type
}

template<typename T, typename Allocator>
auto ForwardList<T, Allocator>::before_begin() -> iterator
auto ForwardList<T, Allocator>::erase_after(iterator position) -> iterator
{
auto iterToErase = std::next(position);
auto nextIter = std::next(iterToErase);
using namespace details;
using NodeAllcoator = typename std::allocator_traits<Allocator>::template rebind_alloc<DataNode<T>>;
using NodeAllocatorTraits = std::allocator_traits<NodeAllcoator>;
NodeAllcoator nodeAllocator = allocator;
auto* addressToErase = static_cast<DataNode<T>*>(iterToErase.getNode());
NodeAllocatorTraits::destroy(nodeAllocator, addressToErase);
nodeAllocator.deallocate(addressToErase, 1);
if (nextIter == end()) {
position.getNode()->nextNode = nullptr;
} else {
position.getNode()->nextNode = nextIter.getNode();
}
--listSize;

return { this, position.getNode()->nextNode };
}

template<typename T, typename Allocator>
auto ForwardList<T, Allocator>::before_begin() noexcept -> iterator
{
return { this, &head };
}

template<typename T, typename Allocator>
auto ForwardList<T, Allocator>::cbefore_begin() const -> const_iterator
auto ForwardList<T, Allocator>::cbefore_begin() const noexcept -> const_iterator
{
return { this, &head };
}

template<typename T, typename Allocator>
auto ForwardList<T, Allocator>::begin() -> iterator
auto ForwardList<T, Allocator>::begin() noexcept -> iterator
{
return std::next(before_begin());
}

template<typename T, typename Allocator>
auto ForwardList<T, Allocator>::cbegin() const -> const_iterator
auto ForwardList<T, Allocator>::cbegin() const noexcept -> const_iterator
{
return begin();
}

template<typename T, typename Allocator>
auto ForwardList<T, Allocator>::end() -> iterator
auto ForwardList<T, Allocator>::end() noexcept -> iterator
{
return { this };
}

template<typename T, typename Allocator>
auto ForwardList<T, Allocator>::cend() const -> const_iterator
auto ForwardList<T, Allocator>::cend() const noexcept -> const_iterator
{
return end();
}
Expand All @@ -188,7 +211,7 @@ bool ForwardList<T, Allocator>::empty() const noexcept
}

template<typename T, typename Allocator>
std::size_t ForwardList<T, Allocator>::size()
std::size_t ForwardList<T, Allocator>::size() noexcept
{
return listSize;
}
Expand Down
35 changes: 33 additions & 2 deletions HW3/tests/forward_list_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,40 @@ TEST(ForwardListTest, Size)
iter = list.insert_after(iter, i);
EXPECT_EQ(list.size(), static_cast<std::size_t>(i + 1));
}
}

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

cc::ForwardList<int> list;
auto iter = list.begin();
for (int i = 0; i < inserts; ++i) {
iter = list.insert_after(iter, i);
}

for (int i = 0; i < inserts; ++i) {
EXPECT_EQ(*list.begin(), i);
list.erase_after(list.before_begin());
}
EXPECT_TRUE(list.empty());
}

TEST(ForwardListTest, EraseInMiddle)
{
const int inserts = 11;

cc::ForwardList<int> list;
auto iter = list.begin();
for (int i = 0; i < inserts; ++i) {
iter = list.insert_after(iter, i);
}

iter = list.begin();
for (int i = 0; iter != list.end(); ++i, ++iter) {
EXPECT_EQ(*iter, i);
std::advance(iter, inserts/2);
for (int i = inserts - inserts/2; i < inserts - 1; ++i) {
EXPECT_EQ(*iter, inserts - inserts / 2 - 1);
EXPECT_EQ(*std::next(iter), i );
list.erase_after(iter);
}
}

0 comments on commit 2520da5

Please sign in to comment.