aboutsummaryrefslogtreecommitdiff
path: root/mp/src/public/arraystack.h
blob: 907fb61453a45d893cffabbdb1004d3378c7651f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $Workfile:     $
// $Date:         $
//
//-----------------------------------------------------------------------------
// $Log: $
//
// $NoKeywords: $
//=============================================================================//

#ifndef ARRAYSTACK_H
#define ARRAYSTACK_H
#pragma once

#include <assert.h>
#include "List.h"

template <class T> class ArrayStack
{
protected:
	T *data;
	int m_stackDepth;
	int m_maxNumElements;
	
public:
	ArrayStack( int maxNumElements )
	{
		data = new T[maxNumElements];
		m_maxNumElements = maxNumElements;
		m_stackDepth = 0;
		assert( data );
	}
	
	void Push( T elem )
	{
		data[m_stackDepth++] = elem;
		if( m_stackDepth > m_maxNumElements )
		{
			printf( "ArrayStack overflow\n" );
			assert( 0 );
		}
	}
	
	T Pop( void )
	{
		if( m_stackDepth == 0 )
		{
			printf( "ArrayStack underflow\n" );
			assert( 0 );
		}
		return data[--m_stackDepth];
	}

	bool IsEmpty()
	{
		return ( m_stackDepth == 0 );
	}
	
	int GetDepth()
	{
		return m_stackDepth;
	}
};


#endif // ARRAYSTACK_H