summaryrefslogtreecommitdiff
path: root/AK/CircularDeque.h
blob: 5e6732a2f4fa1fe7fdab1b85f9b037c3b8553ef1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/CircularQueue.h>
#include <AK/Types.h>

namespace AK {

template<typename T, size_t Capacity>
class CircularDeque : public CircularQueue<T, Capacity> {
public:
    template<typename U = T>
    void enqueue_begin(U&& 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(forward<U>(value));
        this->m_head = new_head;
    }

    T dequeue_end()
    {
        VERIFY(!this->is_empty());
        auto& slot = this->elements()[(this->m_head + this->m_size - 1) % Capacity];
        T value = move(slot);
        slot.~T();
        this->m_size--;
        return value;
    }
};

}

using AK::CircularDeque;