Nebula
Toggle main menu visibility
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
22
namespace
Util
23
{
24
25
//------------------------------------------------------------------------------
26
template
<
class
TYPE>
class
PriorityArray
27
{
28
public
:
30
PriorityArray
(
int
size);
32
PriorityArray
(
const
PriorityArray<TYPE>
& rhs);
34
~PriorityArray
();
36
PriorityArray<TYPE>
&
operator=
(
const
PriorityArray<TYPE>
& rhs);
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
50
private
:
52
void
UpdateMinPriElementIndex
();
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
67
int
numElements
;
68
int
maxElements
;
69
int
minPriElementIndex
;
70
Element
*
elements
;
71
};
72
73
//------------------------------------------------------------------------------
76
template
<
class
TYPE>
77
PriorityArray<TYPE>::PriorityArray
(
int
size) :
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
//------------------------------------------------------------------------------
89
template
<
class
TYPE>
90
void
91
PriorityArray<TYPE>::Copy
(
const
PriorityArray<TYPE>
& src)
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
//------------------------------------------------------------------------------
109
template
<
class
TYPE>
110
void
111
PriorityArray<TYPE>::Delete
()
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
//------------------------------------------------------------------------------
126
template
<
class
TYPE>
127
PriorityArray<TYPE>::PriorityArray
(
const
PriorityArray<TYPE>
& rhs) :
128
numElements
(0),
129
maxElements
(0),
130
minPriElementIndex
(0),
131
elements
(0)
132
{
133
this->
Copy
(rhs);
134
}
135
136
//------------------------------------------------------------------------------
139
template
<
class
TYPE>
140
PriorityArray<TYPE>::~PriorityArray
()
141
{
142
this->
Delete
();
143
}
144
145
//------------------------------------------------------------------------------
148
template
<
class
TYPE>
149
void
150
PriorityArray<TYPE>::Destroy
(TYPE* elm)
151
{
152
elm->~TYPE();
153
}
154
155
//------------------------------------------------------------------------------
158
template
<
class
TYPE>
159
void
160
PriorityArray<TYPE>::Clear
()
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
//------------------------------------------------------------------------------
177
template
<
class
TYPE>
178
void
179
PriorityArray<TYPE>::UpdateMinPriElementIndex
()
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
//------------------------------------------------------------------------------
197
template
<
class
TYPE>
198
void
199
PriorityArray<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
//------------------------------------------------------------------------------
225
template
<
class
TYPE>
226
int
227
PriorityArray<TYPE>::Size
()
const
228
{
229
return
this->
numElements
;
230
}
231
232
//------------------------------------------------------------------------------
235
template
<
class
TYPE>
236
TYPE&
237
PriorityArray<TYPE>::At
(
int
index)
238
{
239
n_assert
((index >= 0) && (index < this->
numElements
));
240
return
this->
elements
[index].element;
241
}
242
243
//------------------------------------------------------------------------------
246
template
<
class
TYPE>
247
TYPE&
248
PriorityArray<TYPE>::operator[]
(
int
index)
const
249
{
250
n_assert
((index >= 0) && (index < this->
numElements
));
251
return
this->
elements
[index].element;
252
}
253
254
//------------------------------------------------------------------------------
257
template
<
class
TYPE>
258
PriorityArray<TYPE>
&
259
PriorityArray<TYPE>::operator=
(
const
PriorityArray<TYPE>
& rhs)
260
{
261
this->
Delete
();
262
this->
Copy
(rhs);
263
return
*
this
;
264
}
265
266
//------------------------------------------------------------------------------
269
template
<
class
TYPE>
270
bool
271
PriorityArray<TYPE>::IsEmpty
()
const
272
{
273
return
(0 == this->
numElements
);
274
}
275
276
}
// namespace Util
277
//------------------------------------------------------------------------------
278
#endif
279
280
281
Util::PriorityArray
Definition
priorityarray.h:27
Util::PriorityArray::IsEmpty
bool IsEmpty() const
return true if empty
Definition
priorityarray.h:271
Util::PriorityArray::Delete
void Delete()
delete content
Definition
priorityarray.h:111
Util::PriorityArray::operator[]
TYPE & operator[](int index) const
[] operator
Definition
priorityarray.h:248
Util::PriorityArray::Add
void Add(const TYPE &elm, float pri)
add element to array
Definition
priorityarray.h:199
Util::PriorityArray::numElements
int numElements
Definition
priorityarray.h:67
Util::PriorityArray::operator=
PriorityArray< TYPE > & operator=(const PriorityArray< TYPE > &rhs)
assignment operator
Definition
priorityarray.h:259
Util::PriorityArray::PriorityArray
PriorityArray(int size)
constructor
Definition
priorityarray.h:77
Util::PriorityArray::~PriorityArray
~PriorityArray()
destructor
Definition
priorityarray.h:140
Util::PriorityArray::UpdateMinPriElementIndex
void UpdateMinPriElementIndex()
update the min pri element index
Definition
priorityarray.h:179
Util::PriorityArray::elements
Element * elements
Definition
priorityarray.h:70
Util::PriorityArray::Clear
void Clear()
clear the array
Definition
priorityarray.h:160
Util::PriorityArray::Copy
void Copy(const PriorityArray< TYPE > &src)
copy content
Definition
priorityarray.h:91
Util::PriorityArray::minPriElementIndex
int minPriElementIndex
Definition
priorityarray.h:69
Util::PriorityArray::Destroy
void Destroy(TYPE *elm)
destroy an element (call destructor without freeing memory)
Definition
priorityarray.h:150
Util::PriorityArray::maxElements
int maxElements
Definition
priorityarray.h:68
Util::PriorityArray::Size
int Size() const
get number of elements in array
Definition
priorityarray.h:227
Util::PriorityArray::At
TYPE & At(int index)
return n'th array element
Definition
priorityarray.h:237
n_assert
#define n_assert(exp)
Definition
debug.h:50
Util
A quad tree designed to return regions of free 2D space.
Definition
String.cs:6
n_delete_array
#define n_delete_array(ptr)
Definition
osxmemory.h:116
n_new_array
#define n_new_array(type, size)
Definition
osxmemory.h:114
Util::PriorityArray::Element
an element class
Definition
priorityarray.h:62
Util::PriorityArray::Element::element
TYPE element
Definition
priorityarray.h:63
Util::PriorityArray::Element::priority
float priority
Definition
priorityarray.h:64
types.h
code
foundation
util
priorityarray.h
Generated on
for Nebula. Dark theme by
Tilen Majerle
. All rights reserved.