Polygon Crucher SDK - Documentation
Documentation
Loading...
Searching...
No Matches
QueueAr.h
1// CQueue class interface: array implementation
2//
3// Etype: must have zero-parameter constructor and operator=
4// CONSTRUCTION: with (a) no initializer;
5// copy construction of CQueue objects is DISALLOWED
6// Deep copy is supported
7//
8// ******************PUBLIC OPERATIONS*********************
9// void Enqueue( Etype X )--> Insert X
10// void Dequeue( ) --> Remove least recently inserted item
11// Etype GetFront( ) --> Return least 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 GetFront or Dequeue on empty queue
18
19#ifndef __QUEUEAR_H
20#define __QUEUEAR_H
21
22#include "AbsQueue.h"
23
24BEGIN_MOOTOOLS_NAMESPACE
25
26 // Array-based queue
27 template <class Etype>
28 class CQueue : public AbsQueue<Etype>
29 {
30 public:
31 CQueue( );
32 ~CQueue( ) { xDeleteArray(Array, MaxSize); }
33
34 const CQueue & operator=( const CQueue & Rhs );
35
36 void Enqueue( const Etype & X );
37 void Dequeue( );
38 const Etype & GetFront( ) const;
39 int IsEmpty( ) const { return CurrentSize == 0; }
40 int GetSize( ) const { return CurrentSize; }
41 int IsFull( ) const { return 0; }
42 void MakeEmpty( );
43
44 protected:
45 // Copy constructor remains disabled by inheritance
46 int MaxSize;
47 int CurrentSize;
48 int Front;
49 int Back;
50 Etype *Array;
51
52 void DoubleQueue( ); // Double array -- complicated
53 void Increment( int & X ) const; // Add one with wraparound
54 };
55
56 #define QUEUE_SIZE 3
57
58 // Add one with wraparound
59 template <class Etype>
60 void
61 CQueue<Etype>::Increment( int & X ) const
62 {
63 if( ++X == MaxSize )
64 X = 0;
65 }
66
67 template <class Etype>
68 CQueue<Etype>::CQueue( ) : MaxSize( QUEUE_SIZE ),
69 CurrentSize( 0 ), Front( 0 ), Back( -1 )
70 {
71 Array = xNewArray(Etype , MaxSize);
72 }
73
74 template <class Etype>
75 const CQueue<Etype> &
77 {
78 if( this == &Rhs )
79 return Rhs;
80
81 xDeleteArray(Array, MaxSize);
82 Array = xNewArray(Etype, Rhs.MaxSize);
83
84 CurrentSize = Rhs.CurrentSize;
85 MaxSize = Rhs.MaxSize;
86 Front = 0;
87 Back = CurrentSize - 1;
88
89 for( int i = 0, j = Rhs.Front; i < CurrentSize; i++, Increment( j ) )
90 Array[ i ] = Rhs.Array[ j ];
91
92 return *this;
93 }
94
95 template <class Etype>
96 void
98 {
99 int OldSize = MaxSize;
100 int NewSize = MaxSize * 2;
101 Etype *Old = Array;
102
103 Array = xNewArray(Etype, NewSize);
104
105 for( int i = 0, j = Front; i < CurrentSize; i++, Increment( j ) )
106 Array[ i ] = Old[ j ];
107
108 Front = 0;
109 Back = CurrentSize - 1;
110 MaxSize = NewSize;
111
112 xDeleteArray(Old, OldSize);
113 }
114
115 template <class Etype>
116 void
117 CQueue<Etype>::Enqueue( const Etype & X )
118 {
119 if( CurrentSize == MaxSize )
120 DoubleQueue( );
121
122 Increment( Back );
123 Array[ Back ] = X;
124 CurrentSize++;
125 }
126
127 template <class Etype>
128 void
130 {
131 CurrentSize--;
132 Increment( Front );
133 }
134
135 template <class Etype>
136 const Etype &
138 {
139 return Array[ Front ];
140 }
141
142 template <class Etype>
143 void
145 {
146 CurrentSize = 0;
147 Front = 0;
148 Back = -1;
149 }
150
151END_MOOTOOLS_NAMESPACE
152
153#endif // __QUEUEAR_H
The class defines an x, y, z 3D point which can use int, float or double.
Definition 3DPoint.h:27
Definition QueueAr.h:29