2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
2004-03-29 22:28:57 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2014-02-18 01:34:18 +00:00
|
|
|
*
|
2004-03-29 22:28:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef COMMON_STACK_H
|
|
|
|
#define COMMON_STACK_H
|
|
|
|
|
|
|
|
#include "common/scummsys.h"
|
2004-05-05 01:19:42 +00:00
|
|
|
#include "common/array.h"
|
2004-03-29 22:28:57 +00:00
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extremly simple fixed size stack class.
|
|
|
|
*/
|
2012-02-22 19:07:40 +00:00
|
|
|
template<class T, uint MAX_SIZE = 10>
|
2004-05-05 01:19:42 +00:00
|
|
|
class FixedStack {
|
2004-03-29 22:28:57 +00:00
|
|
|
public:
|
2012-02-22 19:07:40 +00:00
|
|
|
typedef uint size_type;
|
|
|
|
|
2004-05-05 01:19:42 +00:00
|
|
|
FixedStack<T, MAX_SIZE>() : _size(0) {}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-03-29 22:28:57 +00:00
|
|
|
bool empty() const {
|
|
|
|
return _size <= 0;
|
|
|
|
}
|
2012-02-22 19:08:40 +00:00
|
|
|
|
2004-05-05 01:19:42 +00:00
|
|
|
void clear() {
|
|
|
|
_size = 0;
|
|
|
|
}
|
2012-02-22 19:08:40 +00:00
|
|
|
|
2008-05-03 23:02:05 +00:00
|
|
|
void push(const T &x) {
|
2004-03-29 22:28:57 +00:00
|
|
|
assert(_size < MAX_SIZE);
|
|
|
|
_stack[_size++] = x;
|
|
|
|
}
|
2012-02-22 19:08:40 +00:00
|
|
|
|
2008-05-03 23:02:05 +00:00
|
|
|
const T &top() const {
|
2006-12-25 23:13:13 +00:00
|
|
|
assert(_size > 0);
|
|
|
|
return _stack[_size - 1];
|
2004-03-29 22:28:57 +00:00
|
|
|
}
|
2012-02-22 19:08:40 +00:00
|
|
|
|
2008-05-03 23:02:05 +00:00
|
|
|
T &top() {
|
2004-03-29 22:28:57 +00:00
|
|
|
assert(_size > 0);
|
2006-12-25 23:13:13 +00:00
|
|
|
return _stack[_size - 1];
|
|
|
|
}
|
2012-02-22 19:08:40 +00:00
|
|
|
|
2006-12-25 23:13:13 +00:00
|
|
|
T pop() {
|
|
|
|
T tmp = top();
|
|
|
|
--_size;
|
2004-08-12 21:33:59 +00:00
|
|
|
return tmp;
|
2004-03-29 22:28:57 +00:00
|
|
|
}
|
2012-02-22 19:08:40 +00:00
|
|
|
|
2012-02-22 19:07:40 +00:00
|
|
|
size_type size() const {
|
2004-03-29 22:28:57 +00:00
|
|
|
return _size;
|
|
|
|
}
|
2012-02-22 19:08:40 +00:00
|
|
|
|
2012-02-22 19:07:40 +00:00
|
|
|
T &operator[](size_type i) {
|
|
|
|
assert(i < MAX_SIZE);
|
2006-12-25 23:13:13 +00:00
|
|
|
return _stack[i];
|
|
|
|
}
|
2012-02-22 19:08:40 +00:00
|
|
|
|
2012-02-22 19:07:40 +00:00
|
|
|
const T &operator[](size_type i) const {
|
|
|
|
assert(i < MAX_SIZE);
|
2004-03-29 22:28:57 +00:00
|
|
|
return _stack[i];
|
|
|
|
}
|
2012-02-22 19:08:40 +00:00
|
|
|
|
2012-02-22 19:07:40 +00:00
|
|
|
protected:
|
2012-02-22 19:08:40 +00:00
|
|
|
T _stack[MAX_SIZE];
|
|
|
|
size_type _size;
|
2004-03-29 22:28:57 +00:00
|
|
|
};
|
|
|
|
|
2004-05-05 01:19:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Variable size stack class, implemented using our Array class.
|
|
|
|
*/
|
2008-05-03 23:02:05 +00:00
|
|
|
template<class T>
|
2004-05-05 01:19:42 +00:00
|
|
|
class Stack {
|
2009-04-21 17:02:09 +00:00
|
|
|
private:
|
2012-02-22 19:08:40 +00:00
|
|
|
Array<T> _stack;
|
2009-04-21 17:02:09 +00:00
|
|
|
|
2004-05-05 01:19:42 +00:00
|
|
|
public:
|
2012-02-22 19:07:40 +00:00
|
|
|
typedef typename Array<T>::size_type size_type;
|
|
|
|
|
2004-05-05 01:19:42 +00:00
|
|
|
Stack<T>() {}
|
2007-02-18 18:08:38 +00:00
|
|
|
Stack<T>(const Array<T> &stackContent) : _stack(stackContent) {}
|
2009-05-24 15:17:42 +00:00
|
|
|
|
2004-05-05 01:19:42 +00:00
|
|
|
bool empty() const {
|
2006-03-28 09:42:54 +00:00
|
|
|
return _stack.empty();
|
2004-05-05 01:19:42 +00:00
|
|
|
}
|
2012-02-22 19:08:40 +00:00
|
|
|
|
2004-05-05 01:19:42 +00:00
|
|
|
void clear() {
|
|
|
|
_stack.clear();
|
|
|
|
}
|
2012-02-22 19:08:40 +00:00
|
|
|
|
2008-05-03 23:02:05 +00:00
|
|
|
void push(const T &x) {
|
2004-05-05 01:19:42 +00:00
|
|
|
_stack.push_back(x);
|
|
|
|
}
|
2012-02-22 19:08:40 +00:00
|
|
|
|
2009-01-21 02:23:09 +00:00
|
|
|
T &top() {
|
2009-05-03 09:19:46 +00:00
|
|
|
return _stack.back();
|
2009-01-21 02:23:09 +00:00
|
|
|
}
|
2012-02-22 19:08:40 +00:00
|
|
|
|
2009-01-21 02:23:09 +00:00
|
|
|
const T &top() const {
|
2009-05-03 09:19:46 +00:00
|
|
|
return _stack.back();
|
2004-05-05 01:19:42 +00:00
|
|
|
}
|
2012-02-22 19:08:40 +00:00
|
|
|
|
2004-08-12 21:33:59 +00:00
|
|
|
T pop() {
|
2009-05-03 09:19:46 +00:00
|
|
|
T tmp = _stack.back();
|
|
|
|
_stack.pop_back();
|
2004-08-12 21:33:59 +00:00
|
|
|
return tmp;
|
2004-05-05 01:19:42 +00:00
|
|
|
}
|
2012-02-22 19:08:40 +00:00
|
|
|
|
2012-02-22 19:07:40 +00:00
|
|
|
size_type size() const {
|
2004-05-05 01:19:42 +00:00
|
|
|
return _stack.size();
|
|
|
|
}
|
2012-02-22 19:08:40 +00:00
|
|
|
|
2012-02-22 19:07:40 +00:00
|
|
|
T &operator[](size_type i) {
|
2009-01-21 02:23:09 +00:00
|
|
|
return _stack[i];
|
|
|
|
}
|
2012-02-22 19:08:40 +00:00
|
|
|
|
2012-02-22 19:07:40 +00:00
|
|
|
const T &operator[](size_type i) const {
|
2004-05-05 01:19:42 +00:00
|
|
|
return _stack[i];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2004-03-29 22:28:57 +00:00
|
|
|
} // End of namespace Common
|
|
|
|
|
|
|
|
#endif
|