Modularizando o src
Dependencies: EALib EthernetInterface_vz mbed-rtos mbed
Fork of header_main_colinas_V0-20-09-14 by
vector.cpp
- Committer:
- klauss
- Date:
- 2015-01-07
- Revision:
- 89:0fe315117b00
- Parent:
- 87:679ee0d594a9
- Child:
- 109:a5b8264ffbbc
File content as of revision 89:0fe315117b00:
#include "vector.h" Vector::Vector(){ this->objects = NULL; this->elements = 0; } Vector::~Vector(){ if( this->objects != NULL ) free( this->objects ); } void Vector::add( Object * e ){ if( e != NULL ){ this->objects = ( Object ** )realloc( this->objects, sizeof ( Object * ) * ++this->elements ); if ( this->objects == NULL ){ memory_is_over = true; if( debug_memory ) debug_msg("Vector add fail"); }else{ this->objects[ this->elements - 1 ] = e; } } } Object * Vector::get_element( int position ){ return( position > -1 && position < this->elements ) ? (Object *) * ( this->objects + position ) : NULL; } void Vector::remove_element( int position ){ if( debug_vector ) debug_msg( "Removing element %d", position ); if( position > -1 && position < this->elements ){ if( position == 0 && this->elements == 1 ) { this->elements = 0; free( this->objects ); this->objects = NULL; }else{ this->objects[ position ] = this->objects[ --this->elements ]; this->objects = ( Object ** )realloc( this->objects, sizeof ( Object * ) * this->elements ); if( this->objects == NULL ) memory_is_over = true; if( debug_memory ) debug_msg("Resize Vector fail"); } } if( debug_vector ) debug_msg( "Removed" ); } int Vector::find_element( Object * e ){ if( e != NULL ) { for( int i = 0; i < this->elements; i++ ) if( this->objects[ i ] == e ) return ( i ); return ( -3 ); } else { return ( -1 ); } } //Object * Vector::find_element( int ext, int port ){ // for( int i = 0; i < this->elements; i++ ){ // if( this->objects[ i ]->get_ext() == ext && // this->objects[ i ]->get_port() == port ) return( this->objects[ i ] ); // } // return( NULL ); //} int Vector::size(){ return this->elements; }