Nebula
Loading...
Searching...
No Matches
priorityarray.h
Go to the documentation of this file.
1#ifndef N_PRIORITYARRAY_H
2#define N_PRIORITYARRAY_H
3//------------------------------------------------------------------------------
20#include "core/types.h"
21
22namespace Util
23{
24
25//------------------------------------------------------------------------------
26template<class TYPE> class PriorityArray
27{
28public:
30 PriorityArray(int size);
38 TYPE& operator[](int index) const;
40 void Clear();
42 void Add(const TYPE& elm, float pri);
44 int Size() const;
46 TYPE& At(int index);
48 bool IsEmpty() const;
49
50private:
54 void Copy(const PriorityArray<TYPE>& src);
56 void Delete();
58 void Destroy(TYPE* elm);
59
61 struct Element
62 {
63 TYPE element;
64 float priority;
65 };
66
71};
72
73//------------------------------------------------------------------------------
76template<class TYPE>
78 numElements(0),
79 maxElements(size),
80 minPriElementIndex(0)
81{
82 n_assert(size > 0);
83 this->elements = n_new_array(Element, size);
84}
85
86//------------------------------------------------------------------------------
89template<class TYPE>
90void
92{
93 n_assert(0 == this->elements);
94 this->numElements = src.numElements;
95 this->maxElements = src.maxElements;
96 this->minPriElementIndex = src.minPriElementIndex;
97
98 this->elements = n_new_array(Element, this->maxElements);
99 int i;
100 for (i = 0; i < this->numElements; i++)
101 {
102 this->elements[i] = src.elements[i];
103 }
104}
105
106//------------------------------------------------------------------------------
109template<class TYPE>
110void
112{
113 this->numElements = 0;
114 this->maxElements = 0;
115 this->minPriElementIndex = 0;
116 if (this->elements)
117 {
118 n_delete_array(this->elements);
119 this->elements = 0;
120 }
121}
122
123//------------------------------------------------------------------------------
126template<class TYPE>
128 numElements(0),
129 maxElements(0),
130 minPriElementIndex(0),
131 elements(0)
132{
133 this->Copy(rhs);
134}
135
136//------------------------------------------------------------------------------
139template<class TYPE>
141{
142 this->Delete();
143}
144
145//------------------------------------------------------------------------------
148template<class TYPE>
149void
151{
152 elm->~TYPE();
153}
154
155//------------------------------------------------------------------------------
158template<class TYPE>
159void
161{
162 n_assert(this->elements);
163
164 // call element destructors
165 int i;
166 for (i = 0; i < this->numElements; i++)
167 {
168 this->Destroy(&(this->elements[i].element));
169 }
170 this->numElements = 0;
171 this->minPriElementIndex = 0;
172}
173
174//------------------------------------------------------------------------------
177template<class TYPE>
178void
180{
181 int i;
182 this->minPriElementIndex = 0;
183 float minPri = this->elements[0].priority;
184 for (i = 1; i < this->numElements; i++)
185 {
186 if (this->elements[i].priority < minPri)
187 {
188 minPri = this->elements[i].priority;
189 this->minPriElementIndex = i;
190 }
191 }
192}
193
194//------------------------------------------------------------------------------
197template<class TYPE>
198void
199PriorityArray<TYPE>::Add(const TYPE& elm, float pri)
200{
201 if (this->numElements < this->maxElements)
202 {
203 this->elements[this->numElements].element = elm;
204 this->elements[this->numElements].priority = pri;
205 this->numElements++;
206 if (this->numElements == this->maxElements)
207 {
208 this->UpdateMinPriElementIndex();
209 }
210 }
211 else
212 {
213 if (pri > this->elements[this->minPriElementIndex].priority)
214 {
215 this->elements[this->minPriElementIndex].element = elm;
216 this->elements[this->minPriElementIndex].priority = pri;
217 this->UpdateMinPriElementIndex();
218 }
219 }
220}
221
222//------------------------------------------------------------------------------
225template<class TYPE>
226int
228{
229 return this->numElements;
230}
231
232//------------------------------------------------------------------------------
235template<class TYPE>
236TYPE&
238{
239 n_assert((index >= 0) && (index < this->numElements));
240 return this->elements[index].element;
241}
242
243//------------------------------------------------------------------------------
246template<class TYPE>
247TYPE&
249{
250 n_assert((index >= 0) && (index < this->numElements));
251 return this->elements[index].element;
252}
253
254//------------------------------------------------------------------------------
257template<class TYPE>
260{
261 this->Delete();
262 this->Copy(rhs);
263 return *this;
264}
265
266//------------------------------------------------------------------------------
269template<class TYPE>
270bool
272{
273 return (0 == this->numElements);
274}
275
276} // namespace Util
277//------------------------------------------------------------------------------
278#endif
279
280
281
Definition priorityarray.h:27
~PriorityArray()
destructor
Definition priorityarray.h:140
PriorityArray(int size)
constructor
Definition priorityarray.h:77
int Size() const
get number of elements in array
Definition priorityarray.h:227
void Destroy(TYPE *elm)
destroy an element (call destructor without freeing memory)
Definition priorityarray.h:150
TYPE & At(int index)
return n'th array element
Definition priorityarray.h:237
int numElements
Definition priorityarray.h:67
PriorityArray< TYPE > & operator=(const PriorityArray< TYPE > &rhs)
assignment operator
Definition priorityarray.h:259
void Delete()
delete content
Definition priorityarray.h:111
void UpdateMinPriElementIndex()
update the min pri element index
Definition priorityarray.h:179
Element * elements
Definition priorityarray.h:70
void Add(const TYPE &elm, float pri)
add element to array
Definition priorityarray.h:199
int minPriElementIndex
Definition priorityarray.h:69
int maxElements
Definition priorityarray.h:68
void Clear()
clear the array
Definition priorityarray.h:160
TYPE & operator[](int index) const
[] operator
Definition priorityarray.h:248
bool IsEmpty() const
return true if empty
Definition priorityarray.h:271
void Copy(const PriorityArray< TYPE > &src)
copy content
Definition priorityarray.h:91
#define n_assert(exp)
Definition debug.h:50
A pinned array is an array which manages its own virtual memory.
Definition String.cs:6
#define n_delete_array(ptr)
Definition osxmemory.h:116
#define n_new_array(type, size)
Definition osxmemory.h:114
an element class
Definition priorityarray.h:62
TYPE element
Definition priorityarray.h:63
float priority
Definition priorityarray.h:64