Polygon Crucher SDK - Documentation
Documentation
Loading...
Searching...
No Matches
StackAr.h
1// CStack class interface: array implementation
2//
3// Etype: must have zero-parameter constructor and operator=
4// CONSTRUCTION: with (a) no initializer;
5// copy construction of CStack objects is DISALLOWED
6// Deep copy is supported
7//
8// ******************PUBLIC OPERATIONS*********************
9// void Push( Etype X ) --> Insert X
10// void Pop( ) --> Remove most recently inserted item
11// Etype Top( ) --> Return most recently inserted item
12// int IsEmpty( ) --> Return 1 if empty; else return 0
13// int IsFull( ) --> Return 1 if full; else return 0
14// void MakeEmpty( ) --> Remove all items
15// ******************ERRORS********************************
16// Predefined exception is propogated if new fails
17// EXCEPTION is called for Top or Pop on empty stack
18
19#ifndef __STACKAR_H
20#define __STACKAR_H
21
22#include "AbsStack.h"
23
24BEGIN_MOOTOOLS_NAMESPACE
25
26template <class Etype>
27class CStack : public AbsStack<Etype>
28{
29 public:
30 CStack( );
31 virtual ~CStack( ) { xDeleteArray(Array, MaxSize); }
32
33 const CStack & operator=( const CStack & Rhs );
34
35 void Push( const Etype & X );
36 void Pop( );
37 void Pop(Etype & X);
38 Etype& Top( ) const;
39 Etype & GetAt( int i ) const;
40 bool Find(const Etype& X) const;
41 int IsEmpty( ) const { return TopOfStack == -1; }
42 int IsFull( ) const { return 0; }
43 void MakeEmpty( ) { TopOfStack = -1; }
44 int Size() const { return TopOfStack+1; }
45 void InitSize(int initSize);
46
47 protected:
48 // Copy constructor remains disabled by inheritance
49 enum { InitStackSize = 5 };
50
51 int MaxSize;
52 int TopOfStack;
53 Etype *Array;
54};
55
56template <class Etype>
58{
59 Array = NULL;
60 TopOfStack = -1;
61 MaxSize = 0;
62
63 InitSize(InitStackSize);
64}
65
66template <class Etype>
68{
69 XASSERT(TopOfStack == -1);
70 if (initSize < InitStackSize)
71 initSize = InitStackSize;
72
73 if (Array && MaxSize == initSize)
74 return;
75
76 xDeleteArray(Array, MaxSize);
77 TopOfStack = -1;
78 MaxSize = initSize;
79 Array = xNewArray(Etype, MaxSize);
80}
81
82template <class Etype>
83const CStack<Etype> &
85{
86 // Check for aliasing
87 if( this == &Rhs )
88 return *this;
89
90 // Get some memory
91 xDeleteArray(Array, MaxSize);
92 Array = xNewArray(Etype, Rhs.MaxSize );
93
94 // Do the copy
95 MaxSize = Rhs.MaxSize;
96 TopOfStack = Rhs.TopOfStack;
97 for( int i = 0; i <= TopOfStack; i++ )
98 Array[ i ] = Rhs.Array[ i ];
99
100 return *this;
101}
102
103template <class Etype>
104void
105CStack<Etype>::Push( const Etype & X )
106{
107 if( TopOfStack + 1 == MaxSize )
108 {
109 int OldSize = MaxSize;
110 Etype *Old = Array;
111
112 MaxSize *= 2;
113 Array = xNewArray(Etype , MaxSize);
114
115 for( int i = 0; i <= TopOfStack; i++ )
116 Array[ i ] = Old[ i ];
117
118 xDeleteArray(Old, OldSize);
119 }
120
121 Array[ ++TopOfStack ] = X;
122}
123
124template <class Etype>
125void
127{
128 TopOfStack--;
129}
130
131template <class Etype>
132void
134{
135 XASSERT(TopOfStack > -1);
136 X = Array[ TopOfStack ];
137
138 TopOfStack--;
139}
140
141template <class Etype>
142Etype&
143CStack<Etype>::Top( ) const
144{
145 XASSERT(TopOfStack > -1);
146 return Array[ TopOfStack ];
147}
148
149template <class Etype>
150Etype &
151CStack<Etype>::GetAt( int i ) const
152{
153 XASSERT(i <= TopOfStack);
154 return Array[ i ];
155}
156
157template <class Etype>
158bool
159CStack<Etype>::Find(const Etype& X) const
160{
161 for (int i = TopOfStack; i >= 0; i--)
162 if (Array[i] == X)
163 return true;
164
165 return false;
166}
167
168END_MOOTOOLS_NAMESPACE
169
170#endif // __STACKAR_H
The class defines an x, y, z 3D point which can use int, float or double.
Definition 3DPoint.h:27
Definition StackAr.h:28