1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 08:47:34 +00:00

AK: Add enqueue_begin() for the CircularDeque class (#1320)

Also add tests for CircularDeque.
This commit is contained in:
howar6hill 2020-03-02 16:50:43 +08:00 committed by GitHub
parent 4ed2ba264d
commit 2a30a020c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 102 additions and 0 deletions

View file

@ -35,6 +35,24 @@ namespace AK {
template<typename T, size_t Capacity>
class CircularDeque : public CircularQueue<T, Capacity> {
public:
void enqueue_begin(T&& value)
{
const auto new_head = (this->m_head - 1 + Capacity) % Capacity;
auto& slot = this->elements()[new_head];
if (this->m_size == Capacity)
slot.~T();
else
++this->m_size;
new (&slot) T(move(value));
this->m_head = new_head;
}
void enqueue_begin(const T& value)
{
enqueue_begin(T(value));
}
T dequeue_end()
{
ASSERT(!this->is_empty());