
#include <stdlib.h>

#include <stdbool.h>

#include <char.h>

#include <text.h>


class array{

	int capacity 		= 10;

	int total 			= 0;

	void * * items 		= malloc( 10000000 );

	int length()
	{

		return this->total;

	}

	void * * data() {

		return this->items;

	}


	void * get( int index )
	{
		
		if ( index >= 0 && index < this->total ){

			return this->items[index];

		}

		return NULL;

	}

	void set( int index, void * item )
	{

		if ( index >= 0 && index < this->total ){

			this->items[ index ] = item;

		}

	}

	void resize( int capacity )
	{

		void * * items = realloc( this->items, sizeof( void * ) * capacity );


		this->items 	= items;

		this->capacity 	= capacity;

	}

	void add( void * item )
	{

		if ( this->capacity == this->total ){

			this->resize( this->capacity * 2 );
	
		}

		this->items[ this->total++ ] = item;

	}

	char * join( char * separator ) {

		int count 				= this->length();

		text * result			= new text( "" );

		for (int i = 0; i < count; ++i)
		{

			char * currentPart = this->items[ i ];

			if( i > 0 ) {

				result->append( separator );

			}

			result->append( currentPart );

		}



		return result->value;

	}

	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( void * item ) {

		this->add( item );
		
		return  this->total;

	}

	void unshift( void * 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;

	}

	void * pop() {

		int length			= this->total;

		int lastIndex		= length - 1;

		void * lastItem		= this->get( lastIndex );

		this->delete( lastIndex );

		return lastItem;

	}

	bool includes( char * value ) {

		int count = this->length();

		for ( int index = 0; index < count; ++index )
		{

			char * currentText = this->get(index);

			//printf("test:	%s	\n", this->items[index]);

			if( currentText == value ) {

				return true;

			}

		}

		return false;

	}


}