1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:57:35 +00:00

AK: Combine SinglyLinkedList and SinglyLinkedListWithCount

Using policy based design `SinglyLinkedList` and
`SinglyLinkedListWithCount` can be combined into one class which takes
a policy to determine how to keep track of the size of the list. The
default policy is to use list iteration to count the items in the list
each time. The `WithCount` form is a different policy which tracks the
size, but comes with the overhead of storing the count and
incrementing/decrementing on each modification.

This model is extensible to have other forms of counting by
implementing only a new policy instead of implementing a totally new
type.
This commit is contained in:
Lenny Maiorani 2022-12-17 18:06:29 -07:00 committed by Sam Atkins
parent 0105600120
commit e0ab7763da
6 changed files with 183 additions and 156 deletions

View file

@ -63,10 +63,129 @@ TEST_CASE(should_find_const_with_predicate)
TEST_CASE(removal_during_iteration)
{
auto list = make_list();
auto size = list.size_slow();
auto size = list.size();
for (auto it = list.begin(); it != list.end(); ++it, --size) {
VERIFY(list.size_slow() == size);
VERIFY(list.size() == size);
it.remove(list);
}
}
static size_t calls_to_increase { 0 };
static size_t calls_to_decrease { 0 };
static size_t calls_to_reset { 0 };
static size_t calls_to_get_size { 0 };
static void setup()
{
calls_to_increase = 0;
calls_to_decrease = 0;
calls_to_reset = 0;
calls_to_get_size = 0;
}
struct TestSizeCalculationPolicy {
void increase_size(auto const&) { ++calls_to_increase; }
void decrease_size(auto const&) { ++calls_to_decrease; }
void reset() { ++calls_to_reset; }
size_t size(auto const*) const
{
++calls_to_get_size;
return 42;
}
};
TEST_CASE(should_increase_size_when_appending)
{
setup();
SinglyLinkedList<int, TestSizeCalculationPolicy> list {};
list.append(0);
EXPECT_EQ(1u, calls_to_increase);
}
TEST_CASE(should_decrease_size_when_removing)
{
setup();
SinglyLinkedList<int, TestSizeCalculationPolicy> list {};
list.append(0);
auto begin = list.begin();
list.remove(begin);
EXPECT_EQ(1u, calls_to_decrease);
}
TEST_CASE(should_reset_size_when_clearing)
{
setup();
SinglyLinkedList<int, TestSizeCalculationPolicy> list {};
list.append(0);
list.clear();
EXPECT_EQ(1u, calls_to_reset);
}
TEST_CASE(should_get_size_from_policy)
{
setup();
SinglyLinkedList<int, TestSizeCalculationPolicy> list {};
EXPECT_EQ(42u, list.size());
EXPECT_EQ(1u, calls_to_get_size);
}
TEST_CASE(should_decrease_size_when_taking_first)
{
setup();
SinglyLinkedList<int, TestSizeCalculationPolicy> list {};
list.append(0);
list.take_first();
EXPECT_EQ(1u, calls_to_decrease);
}
TEST_CASE(should_increase_size_when_try_appending)
{
setup();
SinglyLinkedList<int, TestSizeCalculationPolicy> list {};
MUST(list.try_append(0));
EXPECT_EQ(1u, calls_to_increase);
}
TEST_CASE(should_increase_size_when_try_prepending)
{
setup();
SinglyLinkedList<int, TestSizeCalculationPolicy> list {};
MUST(list.try_prepend(0));
EXPECT_EQ(1u, calls_to_increase);
}
TEST_CASE(should_increase_size_when_try_inserting_before)
{
setup();
SinglyLinkedList<int, TestSizeCalculationPolicy> list {};
MUST(list.try_insert_before(list.begin(), 42));
EXPECT_EQ(1u, calls_to_increase);
}
TEST_CASE(should_increase_size_when_try_inserting_after)
{
setup();
SinglyLinkedList<int, TestSizeCalculationPolicy> list {};
MUST(list.try_insert_after(list.begin(), 42));
EXPECT_EQ(1u, calls_to_increase);
}
TEST_CASE(should_increase_size_when_inserting_before)
{
setup();
SinglyLinkedList<int, TestSizeCalculationPolicy> list {};
list.insert_before(list.begin(), 42);
EXPECT_EQ(1u, calls_to_increase);
}
TEST_CASE(should_increase_size_when_inserting_after)
{
setup();
SinglyLinkedList<int, TestSizeCalculationPolicy> list {};
list.insert_after(list.begin(), 42);
EXPECT_EQ(1u, calls_to_increase);
}