
template <class ITEM>
class Stack {

public:
	Stack(int chunksize) {
		size = ChunkSize = chunksize;
		base = new ITEM[size];
		height = 0;
	}
	virtual ~Stack() {
		delete[] base;
	}

	int  Size(void) {
		return(height);
	}
	void Add(ITEM *item) {
		if ( height == size )
			Expand();
		memcpy(&base[height++], item, sizeof(ITEM));
	}
	void Pop(ITEM *item) {
		if ( ! height ) {
			memset(item, 0, sizeof(ITEM));
			return;
		}
		memcpy(item, &base[--height], sizeof(ITEM));
	}
	/* "dumpfunc()" has direct access to the stack items */
	// This function isn't really used.
	void Dump(void (*dumpfunc)(ITEM *)) {
		for ( int x=height; x; )
			(*dumpfunc)(&base[--x]);
	}
	void Flush(void) {
		height = 0;
	}

protected:
	int   ChunkSize;
	ITEM *base;
	int   size;
	int   height;
	
	Expand(void) {
		ITEM *old_base;

		old_base  = base;
		base = new ITEM[size+ChunkSize];
		memcpy(base, old_base, size*sizeof(ITEM));
		size += ChunkSize;
		delete[] old_base;
	}
};

