2020-08-05 04:31:46 +00:00
|
|
|
/*
|
2021-04-22 19:11:04 +00:00
|
|
|
* Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
|
2020-08-05 04:31:46 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-05 04:31:46 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/SinglyLinkedList.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class SinglyLinkedListWithCount : private SinglyLinkedList<T> {
|
|
|
|
|
|
|
|
public:
|
2021-01-10 23:29:28 +00:00
|
|
|
SinglyLinkedListWithCount() = default;
|
|
|
|
~SinglyLinkedListWithCount() = default;
|
2020-08-05 04:31:46 +00:00
|
|
|
|
|
|
|
using List = SinglyLinkedList<T>;
|
|
|
|
|
|
|
|
using List::is_empty;
|
|
|
|
using List::size_slow;
|
|
|
|
|
|
|
|
inline size_t size() const
|
|
|
|
{
|
|
|
|
return m_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
List::clear();
|
|
|
|
m_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
T& first()
|
|
|
|
{
|
|
|
|
return List::first();
|
|
|
|
}
|
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
T const& first() const
|
2020-08-05 04:31:46 +00:00
|
|
|
{
|
|
|
|
return List::first();
|
|
|
|
}
|
|
|
|
|
|
|
|
T& last()
|
|
|
|
{
|
|
|
|
return List::last();
|
|
|
|
}
|
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
T const& last() const
|
2020-08-05 04:31:46 +00:00
|
|
|
{
|
|
|
|
return List::last();
|
|
|
|
}
|
|
|
|
|
|
|
|
T take_first()
|
|
|
|
{
|
|
|
|
m_count--;
|
|
|
|
return List::take_first();
|
|
|
|
}
|
|
|
|
|
2022-11-01 09:04:13 +00:00
|
|
|
template<typename U = T>
|
|
|
|
ErrorOr<void> try_append(U&& value)
|
|
|
|
{
|
|
|
|
auto result = List::try_append(forward<T>(value));
|
|
|
|
if (!result.is_error())
|
|
|
|
m_count++;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef KERNEL
|
2021-01-15 16:51:05 +00:00
|
|
|
template<typename U = T>
|
|
|
|
void append(U&& value)
|
2020-08-05 04:31:46 +00:00
|
|
|
{
|
2022-11-01 09:04:13 +00:00
|
|
|
MUST(try_append(forward<T>(value)));
|
2020-08-05 04:31:46 +00:00
|
|
|
}
|
2022-11-01 09:04:13 +00:00
|
|
|
#endif
|
2020-08-05 04:31:46 +00:00
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
bool contains_slow(T const& value) const
|
2020-08-05 04:31:46 +00:00
|
|
|
{
|
|
|
|
return List::contains_slow(value);
|
|
|
|
}
|
|
|
|
|
2020-11-22 01:02:56 +00:00
|
|
|
using Iterator = typename List::Iterator;
|
2020-08-05 04:31:46 +00:00
|
|
|
friend Iterator;
|
|
|
|
Iterator begin() { return List::begin(); }
|
|
|
|
Iterator end() { return List::end(); }
|
|
|
|
|
2020-11-22 01:02:56 +00:00
|
|
|
using ConstIterator = typename List::ConstIterator;
|
2020-08-05 04:31:46 +00:00
|
|
|
friend ConstIterator;
|
|
|
|
ConstIterator begin() const { return List::begin(); }
|
|
|
|
ConstIterator end() const { return List::end(); }
|
|
|
|
|
2021-01-10 18:27:41 +00:00
|
|
|
template<typename TUnaryPredicate>
|
|
|
|
ConstIterator find(TUnaryPredicate&& pred) const
|
2020-08-05 04:31:46 +00:00
|
|
|
{
|
2021-01-10 18:27:41 +00:00
|
|
|
return List::find_if(forward<TUnaryPredicate>(pred));
|
2020-08-05 04:31:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 18:27:41 +00:00
|
|
|
template<typename TUnaryPredicate>
|
|
|
|
Iterator find(TUnaryPredicate&& pred)
|
2020-08-05 04:31:46 +00:00
|
|
|
{
|
2021-01-10 18:27:41 +00:00
|
|
|
return List::find_if(forward<TUnaryPredicate>(pred));
|
2020-08-05 04:31:46 +00:00
|
|
|
}
|
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
ConstIterator find(T const& value) const
|
2020-08-05 04:31:46 +00:00
|
|
|
{
|
|
|
|
return List::find(value);
|
|
|
|
}
|
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
Iterator find(T const& value)
|
2020-08-05 04:31:46 +00:00
|
|
|
{
|
|
|
|
return List::find(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove(Iterator iterator)
|
|
|
|
{
|
|
|
|
m_count--;
|
|
|
|
return List::remove(iterator);
|
|
|
|
}
|
|
|
|
|
2021-01-15 16:51:05 +00:00
|
|
|
template<typename U = T>
|
|
|
|
void insert_before(Iterator iterator, U&& value)
|
2020-08-05 04:31:46 +00:00
|
|
|
{
|
|
|
|
m_count++;
|
2021-01-15 16:51:05 +00:00
|
|
|
List::insert_before(iterator, forward<T>(value));
|
2020-08-05 04:31:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 16:51:05 +00:00
|
|
|
template<typename U = T>
|
|
|
|
void insert_after(Iterator iterator, U&& value)
|
2020-08-05 04:31:46 +00:00
|
|
|
{
|
|
|
|
m_count++;
|
2021-01-15 16:51:05 +00:00
|
|
|
List::insert_after(iterator, forward<T>(value));
|
2020-08-05 04:31:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t m_count { 0 };
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2020-08-05 04:31:46 +00:00
|
|
|
using AK::SinglyLinkedListWithCount;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|