Loading...
Searching...
No Matches
Queue.h
1//
2// Created by Henri on 02/04/2026.
3//
4
5#ifndef IDOL_QUEUE_H
6#define IDOL_QUEUE_H
7
8namespace idol {
9 template<class T> class Queue;
10}
11
12template<class T>
14 std::vector<std::unique_ptr<T>> m_queue;
15 unsigned int m_first_index = 0;
16 unsigned int m_size = 0;
17public:
18 Queue(unsigned int t_size);
19
20 [[nodiscard]] unsigned int size() const { return m_size; }
21 [[nodiscard]] bool empty() const { return bool(m_size == 0); }
22
23 void push(const T& t_x);
24 void clear();
25
26 const T& front() const { return *m_queue.at(m_first_index); }
27 T& front() { return *m_queue.at(m_first_index); }
28
29 const T& at(unsigned int t_index) const { return *m_queue.at((m_first_index + m_size - 1 - t_index) % m_queue.size()); }
30 T& at(unsigned int t_index) { return *m_queue.at((m_first_index + m_size - 1 - t_index) % m_queue.size()); }
31};
32
33template <class T>
34idol::Queue<T>::Queue(unsigned int t_size) : m_queue(t_size) {
35
36}
37
38template <class T>
39void idol::Queue<T>::push(const T& t_x) {
40
41 if (m_size < m_queue.size()) {
42 const unsigned int pos = (m_first_index + m_size) % m_queue.size();
43 m_queue[pos] = std::make_unique<T>(t_x);
44 ++m_size;
45 } else {
46 m_queue[m_first_index] = std::make_unique<T>(t_x);
47 m_first_index = (m_first_index + 1) % m_queue.size();
48 }
49
50}
51
52template <class T>
53void idol::Queue<T>::clear() {
54 m_size = 0;
55 m_first_index = 0;
56 m_queue = std::vector<std::unique_ptr<T>>(m_queue.size());
57}
58
59#endif //IDOL_QUEUE_H