Skip to content

Commit

Permalink
perf!: Remove pop-from-empty guard
Browse files Browse the repository at this point in the history
  • Loading branch information
oboukli committed Jan 18, 2025
1 parent 00b9fe0 commit 423c5e1
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 31 deletions.
7 changes: 3 additions & 4 deletions include/forfun/container/forward_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#ifndef FORFUN_CONTAINER_FORWARD_LIST_HPP_
#define FORFUN_CONTAINER_FORWARD_LIST_HPP_

#include <cassert>
#include <concepts>
#include <utility>

Expand Down Expand Up @@ -59,12 +60,10 @@ class forward_list final {
head_->next = aux;
}

/// The behavior is undefined when popping the front of an empty container.
auto pop_front() noexcept -> void
{
if (head_ == nullptr)
{
return;
}
assert(head_ != nullptr);

internal::forward_list_node<T>* const aux{head_};
head_ = head_->next;
Expand Down
10 changes: 2 additions & 8 deletions src/forfun/container/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,10 @@ auto list::push_back(list::value_type value) noexcept(false) -> void
end_->previous_ = n;
}

/// The behavior is undefined when popping the back of an empty container.
auto list::pop_back() noexcept -> void
{
if (tail_ == end_) [[unlikely]]
{
assert(head_ == end_);
assert(size_ == size_type{});

return;
}

assert(tail_ != end_);
assert(size_ > size_type{});
assert(head_ != end_);

Expand Down
9 changes: 0 additions & 9 deletions test/container/forward_list_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,6 @@ TEST_CASE("Forward list", "[container][forward_list][dynamic_allocation]")
REQUIRE_FALSE(forward_list.empty());
}

SECTION("Pop back one element out of empty list")
{
forfun::experimental::container::forward_list<int> forward_list{};

forward_list.pop_front();

REQUIRE(forward_list.empty());
}

SECTION("Pop back one element out of list of one element")
{
forfun::experimental::container::forward_list<int> forward_list{};
Expand Down
10 changes: 0 additions & 10 deletions test/container/list_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,6 @@ TEST_CASE("Linked list", "[container][list][dynamic_allocation]")
REQUIRE_FALSE(list.empty());
}

SECTION("Pop back one element out of empty list")
{
forfun::experimental::container::list list{};

list.pop_back();

REQUIRE(list.empty());
REQUIRE(list.size() == std::size_t{0U});
}

SECTION("Pop back one element out of list of one element")
{
forfun::experimental::container::list list{};
Expand Down

0 comments on commit 423c5e1

Please sign in to comment.