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:
parent
4ed2ba264d
commit
2a30a020c1
2 changed files with 102 additions and 0 deletions
|
@ -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());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue