Files
c-prime/application/source/engine/floatArray.c
2025-11-17 10:28:09 +01:00

167 lines
1.9 KiB
C

#include <stdlib.h>
#include <char.h>
#include <text.h>
#include <vector3.h>
#include <vector2.h>
class floatArray{
int capacity = 10;
int total = 0;
float * items = malloc( 10000000 );
int length()
{
return this->total;
}
float get( int index )
{
if ( index >= 0 && index < this->total ){
return this->items[index];
}
return NULL;
}
void set( int index, float item )
{
if ( index >= 0 && index < this->total ){
this->items[ index ] = item;
}
}
void resize( int capacity )
{
float * items = realloc( this->items, sizeof( float ) * capacity );
this->items = items;
this->capacity = capacity;
}
void addVector2( struct vector2 * item ) {
this->add( item->x );
this->add( item->y );
}
void addVector3( struct vector3 * item ) {
this->add( item->x );
this->add( item->y );
this->add( item->z );
}
void add( float item )
{
if ( this->capacity == this->total ){
this->resize( this->capacity * 2 );
}
this->items[ this->total++ ] = item;
}
void delete( int index )
{
if ( index < 0 || index >= this->total ){
return;
}
this->items[index] = NULL;
for ( int i = index; i < this->total - 1; i++ ) {
this->items[i] = this->items[i + 1];
this->items[i + 1] = NULL;
}
this->total--;
if ( this->total > 0 && this->total == this->capacity / 4 ){
this->resize( this->capacity / 2 );
}
}
int array_push( float item ) {
this->add( item );
return this->total;
}
void unshift( float item ) {
int length = this->total;
this->total++;
if ( this->capacity == this->total ){
this->resize( this->capacity * 2 );
}
for ( int i = length - 1; i >= 0; --i ) {
this->items[ i + 1 ] = this->items[ i ];
}
this->items[ 0 ] = item;
}
float pop() {
int length = this->total;
int lastIndex = length - 1;
float lastItem = this->get( lastIndex );
this->delete( lastIndex );
return lastItem;
}
}