[Scummvm-cvs-logs] CVS: scummvm/common stack.h,1.1,1.2

Max Horn fingolfin at users.sourceforge.net
Tue May 4 18:20:02 CEST 2004


Update of /cvsroot/scummvm/scummvm/common
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8806/common

Modified Files:
	stack.h 
Log Message:
Added generic variable size stack class - COMPLETELY UNTESTED. Really should add some unit tests for this...

Index: stack.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/common/stack.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- stack.h	29 Mar 2004 22:28:57 -0000	1.1
+++ stack.h	5 May 2004 01:19:42 -0000	1.2
@@ -23,6 +23,7 @@
 
 #include "common/scummsys.h"
 #include <assert.h>
+#include "common/array.h"
 
 namespace Common {
 
@@ -30,17 +31,20 @@
  * Extremly simple fixed size stack class.
  */
 template <class T, int MAX_SIZE = 10>
-class Stack {
+class FixedStack {
 protected:
 	T	_stack[MAX_SIZE];
 	int	_size;
 public:
-	Stack<T, MAX_SIZE>() : _size(0) {}
+	FixedStack<T, MAX_SIZE>() : _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 @@
 	}
 };
 
+
+/**
+ * Variable size stack class, implemented using our Array class.
+ */
+template <class T>
+class Stack {
+protected:
+	Array<T>	_stack;
+public:
+	Stack<T>() {}
+	
+	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





More information about the Scummvm-git-logs mailing list