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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
//========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#ifndef UTLQUEUE_H
#define UTLQUEUE_H
#ifdef _WIN32
#pragma once
#endif
#include "utlvector.h"
// T is the type stored in the queue
template< class T, class M = CUtlMemory< T > >
class CUtlQueue
{
public:
// constructor: lessfunc is required, but may be set after the constructor with
// SetLessFunc
CUtlQueue( int growSize = 0, int initSize = 0 );
CUtlQueue( T *pMemory, int numElements );
// element access
T& operator[]( int i );
T const& operator[]( int i ) const;
T& Element( int i );
T const& Element( int i ) const;
// return the item from the front of the queue and delete it
T const& RemoveAtHead();
// return the item from the end of the queue and delete it
T const& RemoveAtTail();
// return item at the front of the queue
T const& Head();
// return item at the end of the queue
T const& Tail();
// put a new item on the queue to the tail.
void Insert( T const &element );
// checks if an element of this value already exists on the queue, returns true if it does
bool Check( T const element );
// Returns the count of elements in the queue
int Count() const { return m_heap.Count(); }
// Is element index valid?
bool IsIdxValid( int i ) const;
// doesn't deallocate memory
void RemoveAll() { m_heap.RemoveAll(); }
// Memory deallocation
void Purge() { m_heap.Purge(); }
protected:
CUtlVector<T, M> m_heap;
T m_current;
};
//-----------------------------------------------------------------------------
// The CUtlQueueFixed class:
// A queue class with a fixed allocation scheme
//-----------------------------------------------------------------------------
template< class T, size_t MAX_SIZE >
class CUtlQueueFixed : public CUtlQueue< T, CUtlMemoryFixed<T, MAX_SIZE > >
{
typedef CUtlQueue< T, CUtlMemoryFixed<T, MAX_SIZE > > BaseClass;
public:
// constructor, destructor
CUtlQueueFixed( int growSize = 0, int initSize = 0 ) : BaseClass( growSize, initSize ) {}
CUtlQueueFixed( T* pMemory, int numElements ) : BaseClass( pMemory, numElements ) {}
};
template< class T, class M >
inline CUtlQueue<T, M>::CUtlQueue( int growSize, int initSize ) :
m_heap(growSize, initSize)
{
}
template< class T, class M >
inline CUtlQueue<T, M>::CUtlQueue( T *pMemory, int numElements ) :
m_heap(pMemory, numElements)
{
}
//-----------------------------------------------------------------------------
// element access
//-----------------------------------------------------------------------------
template< class T, class M >
inline T& CUtlQueue<T,M>::operator[]( int i )
{
return m_heap[i];
}
template< class T, class M >
inline T const& CUtlQueue<T,M>::operator[]( int i ) const
{
return m_heap[i];
}
template< class T, class M >
inline T& CUtlQueue<T,M>::Element( int i )
{
return m_heap[i];
}
template< class T, class M >
inline T const& CUtlQueue<T,M>::Element( int i ) const
{
return m_heap[i];
}
//-----------------------------------------------------------------------------
// Is element index valid?
//-----------------------------------------------------------------------------
template< class T, class M >
inline bool CUtlQueue<T,M>::IsIdxValid( int i ) const
{
return (i >= 0) && (i < m_heap.Count());
}
template <class T, class M>
inline T const& CUtlQueue<T, M>::RemoveAtHead()
{
m_current = m_heap[0];
m_heap.Remove((int)0);
return m_current;
}
template <class T, class M>
inline T const& CUtlQueue<T, M>::RemoveAtTail()
{
m_current = m_heap[ m_heap.Count() - 1 ];
m_heap.Remove((int)(m_heap.Count() - 1));
return m_current;
}
template <class T, class M>
inline T const& CUtlQueue<T, M>::Head()
{
m_current = m_heap[0];
return m_current;
}
template <class T, class M>
inline T const& CUtlQueue<T, M>::Tail()
{
m_current = m_heap[ m_heap.Count() - 1 ];
return m_current;
}
template <class T, class M>
void CUtlQueue<T, M>::Insert( T const &element )
{
int index = m_heap.AddToTail();
m_heap[index] = element;
}
template <class T, class M>
bool CUtlQueue<T, M>::Check( T const element )
{
int index = m_heap.Find(element);
return ( index != -1 );
}
#endif // UTLQUEUE_H
|