FreeNOS
Queue.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef __LIBSTD_QUEUE_H
19#define __LIBSTD_QUEUE_H
20
21#include "Types.h"
22#include "Macros.h"
23#include "Container.h"
24
36template <class T, Size N> class Queue : public Container
37{
38 public:
39
44 {
45 clear();
46 }
47
55 bool push(const T & item)
56 {
57 if (m_count >= N)
58 {
59 return false;
60 }
61
62 m_array[m_head] = item;
63 m_head = (m_head + 1) % N;
64 m_count++;
65
66 return true;
67 }
68
76 T & pop()
77 {
78 uint idx = m_tail;
79 m_tail = (m_tail + 1) % N;
80 m_count--;
81
82 return m_array[idx];
83 }
84
92 bool contains(const T & item) const
93 {
94 for (Size i = 0; i < m_count; i++)
95 {
96 if (m_array[(m_tail + i) % N] == item)
97 return true;
98 }
99
100 return false;
101 }
102
110 Size remove(T value)
111 {
112 const Size numItems = m_count;
113 Size numRemoved = 0;
114
115 for (Size i = 0; i < numItems; i++)
116 {
117 T & item = pop();
118
119 if (item != value)
120 push(item);
121 else
122 numRemoved++;
123 }
124
125 return numRemoved;
126 }
127
131 virtual void clear()
132 {
133 m_head = 0;
134 m_tail = 0;
135 m_count = 0;
136 }
137
143 virtual Size size() const
144 {
145 return N;
146 }
147
153 virtual Size count() const
154 {
155 return m_count;
156 }
157
158 private:
159
162
165
168
171};
172
178#endif /* __LIBSTD_QUEUE_H */
Containers provide access to stored items.
Definition Container.h:36
Array of items as a First-In-First-Out (FIFO) datastructure.
Definition Queue.h:37
bool contains(const T &item) const
Look if an item exists on the Queue.
Definition Queue.h:92
virtual void clear()
Removes all items from the Queue.
Definition Queue.h:131
Size remove(T value)
Remove all items with the given value.
Definition Queue.h:110
T & pop()
Remove item from the tail of the Queue.
Definition Queue.h:76
virtual Size count() const
Returns the number of items in the Queue.
Definition Queue.h:153
bool push(const T &item)
Add item to the head of the Queue.
Definition Queue.h:55
T m_array[N]
The actual array where the data is stored.
Definition Queue.h:161
Queue()
Default constructor.
Definition Queue.h:43
uint m_tail
Tail of the queue.
Definition Queue.h:167
virtual Size size() const
Returns the maximum size of this Queue.
Definition Queue.h:143
uint m_head
Head of the queue.
Definition Queue.h:164
uint m_count
Number of items in the queue.
Definition Queue.h:170
unsigned int uint
Unsigned integer number.
Definition Types.h:44
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128