VZTECH / Mbed 2 deprecated main_src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers vector.cpp Source File

vector.cpp

00001 #include "vector.h"
00002 
00003 Vector::Vector ()
00004 {
00005     this -> objects = NULL;
00006     this -> elements = 0;
00007 }
00008 
00009 Vector::~Vector ()
00010 {
00011     if ( this -> objects != NULL ) free ( this -> objects );  
00012 }
00013 
00014 void Vector::add ( Object  * e )
00015 {
00016     if ( e != NULL )
00017     {
00018         this -> objects = ( Object  ** ) realloc ( this -> objects, sizeof ( Object  * ) * ++( this -> elements ) );
00019         if ( this -> objects == NULL )
00020         {
00021             memory_is_over = true;
00022             if ( debug_memory ) vz_debug ( "Vector add fail" );
00023         } else {
00024             this -> objects [ this -> elements - 1 ] = e;
00025         }
00026     }
00027 }
00028 
00029 Object  * Vector::get_element ( int position )
00030 {
00031     return ( position > -1 && position < this->elements ) ? ( Object  * ) * ( this -> objects + position ) : NULL;
00032 }
00033 
00034 void Vector::remove_element ( int position )
00035 {
00036     if ( debug_vector ) vz_debug ( "Removing element %d", position );
00037     if ( position > -1 && position < this -> elements )
00038     {
00039         if ( position == 0 && this -> elements == 1 )
00040         {
00041             this -> elements = 0;
00042             free ( this -> objects );
00043             this -> objects = NULL;
00044         } else {
00045             this -> objects [ position ] = this -> objects[ -- ( this -> elements ) ];
00046             this -> objects = ( Object  ** ) realloc ( this -> objects, sizeof ( Object  * ) * this -> elements );
00047             if ( this -> objects == NULL ) memory_is_over = true;
00048             if ( debug_memory ) vz_debug ( "Resize Vector fail" );
00049         }
00050     }
00051     if ( debug_vector ) vz_debug ( "Removed" );
00052 }
00053 
00054 int Vector::size ( void ) { return this -> elements; }
00055 
00056 int Vector::print_yourself ( void )
00057 {   
00058     vz_printf ("\r\n");
00059     vz_printf ("Elements :: %d ( %p )", this -> elements, &( this->elements ) );   
00060     vz_printf ("Values ::\r\n");
00061     
00062     if ( this -> elements == 0 )
00063     {
00064         vz_printf ( "Objects :: %p", this -> objects );
00065     }
00066         else
00067     {
00068         for ( register int i = 0; i < this -> elements; i++ )
00069         {
00070             vz_printf ("[ %i ] :: %p", i, ( this -> objects +  i ) );
00071         }
00072     }
00073     vz_printf ("\r\n");
00074     
00075     return ( sizeof ( Vector  ) );
00076 }