Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include <AK/Assertions.h>
30#include <AK/StdLibExtras.h>
31#include <AK/Types.h>
32
33namespace AK {
34
35template<typename T, size_t Capacity>
36class CircularQueue {
37public:
38 CircularQueue()
39 {
40 }
41
42 ~CircularQueue()
43 {
44 clear();
45 }
46
47 void clear()
48 {
49 for (size_t i = 0; i < m_size; ++i)
50 elements()[(m_head + i) % Capacity].~T();
51
52 m_head = 0;
53 m_size = 0;
54 }
55
56 bool is_empty() const { return !m_size; }
57 size_t size() const { return m_size; }
58
59 size_t capacity() const { return Capacity; }
60
61 void enqueue(T&& value)
62 {
63 auto& slot = elements()[(m_head + m_size) % Capacity];
64 if (m_size == Capacity)
65 slot.~T();
66
67 new (&slot) T(move(value));
68 if (m_size == Capacity)
69 m_head = (m_head + 1) % Capacity;
70 else
71 ++m_size;
72 }
73
74 void enqueue(const T& value)
75 {
76 enqueue(T(value));
77 }
78
79 T dequeue()
80 {
81 ASSERT(!is_empty());
82 auto& slot = elements()[m_head];
83 T value = move(slot);
84 slot.~T();
85 m_head = (m_head + 1) % Capacity;
86 --m_size;
87 return value;
88 }
89
90 const T& at(size_t index) const { return elements()[(m_head + index) % Capacity]; }
91
92 const T& first() const { return at(0); }
93 const T& last() const { return at(size() - 1); }
94
95 class ConstIterator {
96 public:
97 bool operator!=(const ConstIterator& other) { return m_index != other.m_index; }
98 ConstIterator& operator++()
99 {
100 m_index = (m_index + 1) % Capacity;
101 if (m_index == m_queue.m_head)
102 m_index = m_queue.m_size;
103 return *this;
104 }
105
106 const T& operator*() const { return m_queue.elements()[m_index]; }
107
108 private:
109 friend class CircularQueue;
110 ConstIterator(const CircularQueue& queue, const size_t index)
111 : m_queue(queue)
112 , m_index(index)
113 {
114 }
115 const CircularQueue& m_queue;
116 size_t m_index { 0 };
117 };
118
119 ConstIterator begin() const { return ConstIterator(*this, m_head); }
120 ConstIterator end() const { return ConstIterator(*this, size()); }
121
122 size_t head_index() const { return m_head; }
123
124protected:
125 T* elements() { return reinterpret_cast<T*>(m_storage); }
126 const T* elements() const { return reinterpret_cast<const T*>(m_storage); }
127
128 friend class ConstIterator;
129 alignas(T) u8 m_storage[sizeof(T) * Capacity];
130 size_t m_size { 0 };
131 size_t m_head { 0 };
132};
133
134}
135
136using AK::CircularQueue;