Initial commit
This commit is contained in:
178
application/source/arrayText.c
Normal file
178
application/source/arrayText.c
Normal file
@@ -0,0 +1,178 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "stdbool.h"
|
||||
|
||||
#include "char.h"
|
||||
|
||||
|
||||
class arrayText{
|
||||
|
||||
int capacity = 40;
|
||||
|
||||
int total = 0;
|
||||
|
||||
char * * items = malloc( 10000000 );
|
||||
|
||||
int length()
|
||||
{
|
||||
|
||||
return this->total;
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
char * get( int index )
|
||||
{
|
||||
|
||||
if ( index >= 0 && index < this->total ){
|
||||
|
||||
return this->items[index];
|
||||
|
||||
}
|
||||
|
||||
return "NULL";
|
||||
|
||||
}
|
||||
|
||||
void set( int index, char * item )
|
||||
{
|
||||
|
||||
if ( index >= 0 && index < this->total ){
|
||||
|
||||
this->items[ index ] = item;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void resize( int capacity )
|
||||
{
|
||||
|
||||
char * * items = realloc( this->items, sizeof( char * ) * capacity );
|
||||
|
||||
|
||||
this->items = items;
|
||||
|
||||
this->capacity = capacity;
|
||||
|
||||
}
|
||||
|
||||
void add( char * item )
|
||||
{
|
||||
|
||||
|
||||
if ( this->capacity == this->total ){
|
||||
|
||||
this->resize( this->capacity * 2 );
|
||||
|
||||
}
|
||||
|
||||
|
||||
this->items[ this->total ] = ( char * ) malloc( 200 * sizeof( char ) );
|
||||
|
||||
strcpy(this->items[ this->total ], item);
|
||||
|
||||
|
||||
this->total++;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void delete( int index )
|
||||
{
|
||||
if ( index < 0 || index >= this->total ){
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
//this->items[index] = "";
|
||||
|
||||
for ( int i = index; i < this->total - 1; i++ ) {
|
||||
|
||||
this->items[i] = this->items[i + 1];
|
||||
|
||||
this->items[i + 1] = "";
|
||||
|
||||
}
|
||||
|
||||
this->total--;
|
||||
|
||||
if ( this->total > 0 && this->total == this->capacity / 4 ){
|
||||
|
||||
this->resize( this->capacity / 2 );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
unsigned int array_push( char * item ) {
|
||||
|
||||
this->add( item );
|
||||
|
||||
return this->total;
|
||||
|
||||
}
|
||||
|
||||
void unshift( char * 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;
|
||||
|
||||
}
|
||||
|
||||
char * pop() {
|
||||
|
||||
int length = this->total;
|
||||
|
||||
int lastIndex = length - 1;
|
||||
|
||||
char * lastItem = this->get( lastIndex );
|
||||
|
||||
this->delete( lastIndex );
|
||||
|
||||
return lastItem;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user