SpaceCadetPinball/SpaceCadetPinball/objlist_class.cpp

95 lines
2.1 KiB
C++
Raw Normal View History

2020-10-18 17:08:41 +02:00
#include "pch.h"
2020-10-04 08:28:38 +02:00
#include "objlist_class.h"
#include <cstdlib>
2020-11-05 16:44:34 +01:00
#include "memory.h"
2020-10-04 08:28:38 +02:00
// v1 from Ida
objlist_class::objlist_class(int SizeInt, int growSize)
{
ListPtr = objlist_new(SizeInt);
GrowSize = growSize;
}
objlist_class::~objlist_class()
{
if (ListPtr)
2020-11-05 16:44:34 +01:00
memory::free(ListPtr);
2020-10-04 08:28:38 +02:00
}
void objlist_class::Add(void* value)
{
if (this->ListPtr->Count == this->ListPtr->Size)
Grow();
objlist_add_object(ListPtr, value);
2020-10-04 08:28:38 +02:00
}
void objlist_class::Grow()
{
this->ListPtr = objlist_grow(this->ListPtr, this->GrowSize);
}
int objlist_class::Delete(void* value)
2020-10-04 08:28:38 +02:00
{
return objlist_delete_object(ListPtr, value);
}
void* objlist_class::Get(int index)
{
if (index >= ListPtr->Count)
return nullptr;
return this->ListPtr->Array[index];
}
2020-10-04 08:28:38 +02:00
objlist_struct1* objlist_class::objlist_new(int sizeInt)
{
2020-11-05 16:44:34 +01:00
objlist_struct1* result = (objlist_struct1 *)memory::allocate(sizeof(void*) * sizeInt + sizeof(objlist_struct1));
2020-10-04 08:28:38 +02:00
if (!result)
return result;
result->Count = 0;
result->Size = sizeInt;
return result;
}
int objlist_class::objlist_add_object(objlist_struct1* ptrToStruct, void* value)
2020-10-04 08:28:38 +02:00
{
int addIndex = ptrToStruct->Count;
if (addIndex >= ptrToStruct->Size)
return 0;
ptrToStruct->Array[addIndex] = value;
return ++ptrToStruct->Count;
}
objlist_struct1* objlist_class::objlist_grow(objlist_struct1* ptrToStruct, int growSize)
{
2020-10-04 08:28:38 +02:00
objlist_struct1* resultPtr = ptrToStruct;
if (!ptrToStruct)
return resultPtr;
int newSizeInt = growSize + ptrToStruct->Count;
if (newSizeInt <= ptrToStruct->Size)
return resultPtr;
2020-11-05 16:44:34 +01:00
objlist_struct1* resultPtr2 = (objlist_struct1*)memory::realloc(ptrToStruct, sizeof(void*) * newSizeInt + sizeof(objlist_struct1));
2020-10-04 08:28:38 +02:00
if (!resultPtr2)
return resultPtr;
resultPtr = resultPtr2;
resultPtr2->Size = growSize + resultPtr2->Count;
return resultPtr;
}
int objlist_class::objlist_delete_object(objlist_struct1* ptrToStruct, void* value)
2020-10-04 08:28:38 +02:00
{
int count = ptrToStruct->Count;
int index = count - 1;
if (count - 1 < 0)
return 0;
for (void** i = &ptrToStruct->Array[index]; *i != value; --i)
2020-10-04 08:28:38 +02:00
{
if (--index < 0)
return 0;
}
ptrToStruct->Array[index] = ptrToStruct->Array[count - 1];
--ptrToStruct->Count;
return 1;
}