ScummVM API documentation
fixed_queue.h
1/* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#ifndef TITANIC_FIXED_QUEUE_H
23#define TITANIC_FIXED_QUEUE_H
24
25#include "common/scummsys.h"
26#include "common/array.h"
27
28namespace Titanic {
29
33template<class T, uint MAX_SIZE = 10>
35 typedef uint size_type;
36protected:
37 Common::Array<T> _data;
38 size_type _topIndex;
39public:
40 FixedQueue() : _topIndex(0) {
41 _data.reserve(MAX_SIZE);
42 }
43
47 size_type size() const { return _data.size() - _topIndex; }
48
52 size_type freeSize() const { return MAX_SIZE - size(); }
53
57 bool empty() const {
58 return size() == 0;
59 }
60
64 bool full() const {
65 return freeSize() == 0;
66 }
67
71 void clear() {
72 _data.clear();
73 _topIndex = 0;
74 }
75
80 void compact() {
81 if (_data.size() == MAX_SIZE && _topIndex > 0) {
82 if (_topIndex < MAX_SIZE)
83 Common::copy(&_data[_topIndex], &_data[0] + MAX_SIZE, &_data[0]);
84 _data.resize(size());
85 _topIndex = 0;
86 }
87 }
88
92 void push(const T &v) {
93 assert(size() < MAX_SIZE);
94 compact();
95 _data.push_back(v);
96 }
97
101 const T &top() const {
102 assert(size() > 0);
103 return _data[_topIndex];
104 }
105
109 T &top() {
110 assert(size() > 0);
111 return _data[_topIndex];
112 }
113
117 T pop() {
118 T tmp = top();
119 ++_topIndex;
120 return tmp;
121 }
122
126 T &operator[](size_type i) {
127 assert(i < size());
128 return _data[_topIndex + i];
129 }
130
134 const T &operator[](size_type i) const {
135 assert(i < size());
136 return _data[_topIndex + i];
137 }
138};
139
140} // End of namespace Titanic
141
142#endif
Definition: array.h:52
void reserve(size_type newCapacity)
Definition: array.h:397
size_type size() const
Definition: array.h:316
void clear()
Definition: array.h:321
void push_back(const T &element)
Definition: array.h:181
void resize(size_type newSize)
Definition: array.h:412
Definition: fixed_queue.h:34
T & top()
Definition: fixed_queue.h:109
void clear()
Definition: fixed_queue.h:71
T pop()
Definition: fixed_queue.h:117
bool empty() const
Definition: fixed_queue.h:57
void compact()
Definition: fixed_queue.h:80
size_type freeSize() const
Definition: fixed_queue.h:52
size_type size() const
Definition: fixed_queue.h:47
void push(const T &v)
Definition: fixed_queue.h:92
const T & top() const
Definition: fixed_queue.h:101
const T & operator[](size_type i) const
Definition: fixed_queue.h:134
T & operator[](size_type i)
Definition: fixed_queue.h:126
bool full() const
Definition: fixed_queue.h:64
Out copy(In first, In last, Out dst)
Definition: algorithm.h:52