From 5d0f0ea0c6afd7defaba3df69b39879a63256776 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Wed, 5 May 2004 01:19:42 +0000 Subject: Added generic variable size stack class - COMPLETELY UNTESTED. Really should add some unit tests for this... svn-id: r13776 --- common/stack.h | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) (limited to 'common') diff --git a/common/stack.h b/common/stack.h index 926c61f592..acc8ae6c89 100644 --- a/common/stack.h +++ b/common/stack.h @@ -23,6 +23,7 @@ #include "common/scummsys.h" #include +#include "common/array.h" namespace Common { @@ -30,17 +31,20 @@ namespace Common { * Extremly simple fixed size stack class. */ template -class Stack { +class FixedStack { protected: T _stack[MAX_SIZE]; int _size; public: - Stack() : _size(0) {} + FixedStack() : _size(0) {} bool empty() const { return _size <= 0; } - void push(T x) { + void clear() { + _size = 0; + } + void push(const T& x) { assert(_size < MAX_SIZE); _stack[_size++] = x; } @@ -63,6 +67,44 @@ public: } }; + +/** + * Variable size stack class, implemented using our Array class. + */ +template +class Stack { +protected: + Array _stack; +public: + Stack() {} + + bool empty() const { + return _stack.isEmpty(); + } + void clear() { + _stack.clear(); + } + void push(const T& x) { + _stack.push_back(x); + } + T top() const { + const int s = size(); + if (s > 0) + return _stack[s - 1]; + else + return 0; + } + void pop() { + _stack.remove_at(size() - 1); + } + int size() const { + return _stack.size(); + } + T operator [](int i) { + return _stack[i]; + } +}; + } // End of namespace Common #endif -- cgit v1.2.3