ScummVM API documentation
stack.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 COMMON_STACK_H
23#define COMMON_STACK_H
24
25#include "common/scummsys.h"
26#include "common/array.h"
27
28namespace Common {
29
42template<class T, uint MAX_SIZE = 10>
44public:
45 typedef uint size_type;
46
47 FixedStack() : _size(0) {}
48
49 bool empty() const {
50 return _size <= 0;
51 }
52
53 void clear() {
54 _size = 0;
55 }
56
57 void push(const T &x) {
58 assert(_size < MAX_SIZE);
59 _stack[_size++] = x;
60 }
61
62 const T &top() const {
63 assert(_size > 0);
64 return _stack[_size - 1];
65 }
66
67 T &top() {
68 assert(_size > 0);
69 return _stack[_size - 1];
70 }
71
72 T pop() {
73 T tmp = top();
74 --_size;
75 return tmp;
76 }
77
78 size_type size() const {
79 return _size;
80 }
81
82 T &operator[](size_type i) {
83 assert(i < MAX_SIZE);
84 return _stack[i];
85 }
86
87 const T &operator[](size_type i) const {
88 assert(i < MAX_SIZE);
89 return _stack[i];
90 }
91
92protected:
93 T _stack[MAX_SIZE];
94 size_type _size;
95};
96
97
101template<class T>
102class Stack {
103private:
104 Array<T> _stack;
105
106public:
107 typedef typename Array<T>::size_type size_type;
108
109 Stack() {}
110 Stack(const Array<T> &stackContent) : _stack(stackContent) {}
111
112 bool empty() const {
113 return _stack.empty();
114 }
115
116 void clear() {
117 _stack.clear();
118 }
119
120 void push(const T &x) {
121 _stack.push_back(x);
122 }
123
124 T &top() {
125 return _stack.back();
126 }
127
128 const T &top() const {
129 return _stack.back();
130 }
131
132 T pop() {
133 T tmp = _stack.back();
134 _stack.pop_back();
135 return tmp;
136 }
137
138 size_type size() const {
139 return _stack.size();
140 }
141
142 T &operator[](size_type i) {
143 return _stack[i];
144 }
145
146 const T &operator[](size_type i) const {
147 return _stack[i];
148 }
149};
150
153} // End of namespace Common
154
155#endif
Definition: array.h:52
uint size_type
Definition: array.h:62
size_type size() const
Definition: array.h:316
T & back()
Definition: array.h:230
void clear()
Definition: array.h:321
void push_back(const T &element)
Definition: array.h:181
bool empty() const
Definition: array.h:352
void pop_back()
Definition: array.h:200
Definition: stack.h:43
Definition: stack.h:102
Definition: algorithm.h:29