31# include "../fgame/g_local.h"
33# define CONTAINER_Error gi.Error
34# define CONTAINER_DPrintf gi.DPrintf
35# define CONTAINER_WDPrintf(text) gi.DPrintf(text)
36# define CONTAINER_Alloc gi.Malloc
37# define CONTAINER_Free gi.Free
39#elif defined(CGAME_DLL)
43# include "../cgame/cg_local.h"
45# define CONTAINER_Error cgi.Error
46# define CONTAINER_DPrintf cgi.DPrintf
47# define CONTAINER_WDPrintf(text) cgi.DPrintf(text)
48# define CONTAINER_Alloc cgi.Malloc
49# define CONTAINER_Free cgi.Free
53# include "../renderercommon/tr_common.h"
58# define CONTAINER_Error Com_Error
59# define CONTAINER_DPrintf Com_DPrintf
60# define CONTAINER_WDPrintf(text) Com_DPrintf(text)
61# define CONTAINER_Alloc ri.Malloc
62# define CONTAINER_Free ri.Free
71# define CONTAINER_Error Com_Error
72# define CONTAINER_DPrintf Com_DPrintf
73# define CONTAINER_WDPrintf(text) Com_DPrintf(text)
74# define CONTAINER_Alloc Z_Malloc
75# define CONTAINER_Free Z_Free
92 void Copy(
const Container<Type>& container);
97 Container(
const Container<Type>& container);
98 Container<Type>& operator=(
const Container<Type>& container);
100 Container(Container<Type>&& container);
101 Container<Type>& operator=(Container<Type>&& container);
108 int AddObject(
const Type& obj);
109 int AddUniqueObject(
const Type& obj);
110 void AddObjectAt(
int index,
const Type& obj);
111 Type *AddressOfObjectAt(
int index);
112 void ClearObjectList(
void);
113 void FreeObjectList(
void);
114 int IndexOfObject(
const Type& obj);
115 void InsertObjectAt(
int index,
const Type& obj);
116 int MaxObjects(
void)
const;
117 int NumObjects(
void)
const;
118 Type& ObjectAt(
const size_t index)
const;
119 bool ObjectInList(
const Type& obj);
120 void RemoveObjectAt(
int index);
121 void RemoveObject(
const Type& obj);
123 void Resize(
int maxelements);
124 void SetObjectAt(
int index,
const Type& obj);
125 void Sort(
int (*compare)(
const void *elem1,
const void *elem2));
126 Type& operator[](
const uintptr_t index)
const;
130Container<Type>::Container()
156 objlist = container.objlist;
157 numobjects = container.numobjects;
158 maxobjects = container.maxobjects;
159 container.objlist = NULL;
160 container.numobjects = 0;
161 container.maxobjects = 0;
169 objlist = container.objlist;
170 numobjects = container.numobjects;
171 maxobjects = container.maxobjects;
172 container.objlist = NULL;
173 container.numobjects = 0;
174 container.maxobjects = 0;
180Container<Type>::~Container()
186int Container<Type>::AddObject(
const Type& obj)
192 if (numobjects >= maxobjects) {
193 Resize(numobjects * 2);
196 new (objlist + numobjects) Type(obj);
203int Container<Type>::AddUniqueObject(
const Type& obj)
207 index = IndexOfObject(obj);
210 index = AddObject(obj);
217void Container<Type>::AddObjectAt(
int index,
const Type& obj)
221 if (index > maxobjects) {
225 if (index > numobjects) {
226 for (i = numobjects; i < index; i++) {
227 new (objlist + i) Type();
233 SetObjectAt(index, obj);
237Type *Container<Type>::AddressOfObjectAt(
int index)
239 if (index > maxobjects) {
240 CONTAINER_Error(ERR_DROP,
"Container::AddressOfObjectAt : index is greater than maxobjects");
243 if (index > numobjects) {
247 return &objlist[index - 1];
251void Container<Type>::ClearObjectList(
void)
255 if (objlist && numobjects) {
256 for (i = 0; i < numobjects; ++i) {
266void Container<Type>::FreeObjectList(
void)
271 for (i = 0; i < numobjects; ++i) {
275 CONTAINER_Free(objlist);
284int Container<Type>::IndexOfObject(
const Type& obj)
292 for (i = 0; i < numobjects; i++) {
293 if (objlist[i] == obj) {
302void Container<Type>::InsertObjectAt(
int index,
const Type& obj)
306 if ((index <= 0) || (index > numobjects + 1)) {
307 CONTAINER_Error(ERR_DROP,
"Container::InsertObjectAt : index out of range");
312 intptr_t arrayIndex = index - 1;
314 if (numobjects > maxobjects) {
315 maxobjects = numobjects;
317 objlist = (Type *)CONTAINER_Alloc(
sizeof(Type) * maxobjects);
319 for (i = 0; i < arrayIndex; ++i) {
320 new (objlist + i) Type();
323 new (objlist + arrayIndex) Type(obj);
325 Type *temp = objlist;
326 if (maxobjects < numobjects) {
327 maxobjects = numobjects;
330 objlist = (Type *)CONTAINER_Alloc(
sizeof(Type) * maxobjects);
332 for (i = 0; i < arrayIndex; ++i) {
333 new (objlist + i) Type(std::move(temp[i]));
336 new (objlist + arrayIndex) Type(obj);
337 for (i = arrayIndex; i < numobjects - 1; ++i) {
338 new (objlist + i + 1) Type(std::move(temp[i]));
341 CONTAINER_Free(temp);
344 for (i = numobjects - 1; i > arrayIndex; i--) {
345 objlist[i] = std::move(objlist[i - 1]);
347 objlist[arrayIndex] = obj;
352int Container<Type>::MaxObjects(
void)
const
358int Container<Type>::NumObjects(
void)
const
364Type& Container<Type>::ObjectAt(
const size_t index)
const
366 if ((index <= 0) || (index > numobjects)) {
367 CONTAINER_Error(ERR_DROP,
"Container::ObjectAt : index out of range");
370 return objlist[index - 1];
374bool Container<Type>::ObjectInList(
const Type& obj)
376 if (!IndexOfObject(obj)) {
384void Container<Type>::RemoveObjectAt(
int index)
392 if ((index <= 0) || (index > numobjects)) {
399 for (i = index - 1; i < numobjects; i++) {
400 objlist[i] = std::move(objlist[i + 1]);
404 objlist[numobjects].~Type();
408void Container<Type>::RemoveObject(
const Type& obj)
412 index = IndexOfObject(obj);
416 CONTAINER_WDPrintf(
"Container::RemoveObject : Object not in list\n");
420 RemoveObjectAt(index);
424void Container<Type>::Reset()
432void Container<Type>::Resize(
int maxelements)
437 if (maxelements <= 0) {
443 maxobjects = maxelements;
444 objlist = (Type *)CONTAINER_Alloc(
sizeof(Type) * maxobjects);
448 maxobjects = maxelements;
450 if (maxobjects < numobjects) {
451 maxobjects = numobjects;
454 objlist = (Type *)CONTAINER_Alloc(
sizeof(Type) * maxobjects);
456 for (i = 0; i < numobjects; i++) {
458 new (objlist + i) Type(std::move(temp[i]));
464 CONTAINER_Free(temp);
469void Container<Type>::SetObjectAt(
int index,
const Type& obj)
475 if ((index <= 0) || (index > numobjects)) {
476 CONTAINER_Error(ERR_DROP,
"Container::SetObjectAt : index out of range");
479 objlist[index - 1] = obj;
483void Container<Type>::Sort(
int (*compare)(
const void *elem1,
const void *elem2))
489 qsort((
void *)objlist, (
size_t)numobjects,
sizeof(Type), compare);
493Type& Container<Type>::operator[](
const uintptr_t index)
const
495 return ObjectAt(index + 1);
503 if (&container ==
this) {
509 numobjects = container.numobjects;
510 maxobjects = container.maxobjects;
513 if (container.objlist == NULL || !container.maxobjects) {
519 if (!container.numobjects) {
523 for (i = 0; i < container.numobjects; i++) {
524 new(objlist + i) Type(container.objlist[i]);
531void *
operator new(
size_t count,
Container<T>& container)
535 assert(count ==
sizeof(T));
536 return &container.ObjectAt(container.AddObject());
542 container.RemoveObject((T *)ptr);
Definition container.h:85