Modularizando o src
Dependencies: EALib EthernetInterface_vz mbed-rtos mbed
Fork of header_main_colinas_V0-20-09-14 by
vector.cpp@89:0fe315117b00, 2015-01-07 (annotated)
- Committer:
- klauss
- Date:
- Wed Jan 07 21:48:53 2015 +0000
- Revision:
- 89:0fe315117b00
- Parent:
- 87:679ee0d594a9
- Child:
- 109:a5b8264ffbbc
wip
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
klauss | 0:4d17cd9c8f9d | 1 | #include "vector.h" |
klauss | 0:4d17cd9c8f9d | 2 | |
klauss | 0:4d17cd9c8f9d | 3 | Vector::Vector(){ |
klauss | 0:4d17cd9c8f9d | 4 | this->objects = NULL; |
klauss | 0:4d17cd9c8f9d | 5 | this->elements = 0; |
klauss | 0:4d17cd9c8f9d | 6 | } |
klauss | 0:4d17cd9c8f9d | 7 | |
klauss | 0:4d17cd9c8f9d | 8 | Vector::~Vector(){ |
klauss | 0:4d17cd9c8f9d | 9 | if( this->objects != NULL ) free( this->objects ); |
klauss | 0:4d17cd9c8f9d | 10 | } |
klauss | 0:4d17cd9c8f9d | 11 | |
klauss | 87:679ee0d594a9 | 12 | void Vector::add( Object * e ){ |
klauss | 87:679ee0d594a9 | 13 | if( e != NULL ){ |
klauss | 87:679ee0d594a9 | 14 | this->objects = ( Object ** )realloc( this->objects, sizeof ( Object * ) * ++this->elements ); |
klauss | 87:679ee0d594a9 | 15 | if ( this->objects == NULL ){ |
klauss | 87:679ee0d594a9 | 16 | memory_is_over = true; |
klauss | 89:0fe315117b00 | 17 | if( debug_memory ) debug_msg("Vector add fail"); |
klauss | 87:679ee0d594a9 | 18 | }else{ |
klauss | 87:679ee0d594a9 | 19 | this->objects[ this->elements - 1 ] = e; |
klauss | 87:679ee0d594a9 | 20 | } |
klauss | 87:679ee0d594a9 | 21 | } |
klauss | 0:4d17cd9c8f9d | 22 | } |
klauss | 0:4d17cd9c8f9d | 23 | |
klauss | 0:4d17cd9c8f9d | 24 | Object * Vector::get_element( int position ){ |
klauss | 0:4d17cd9c8f9d | 25 | return( position > -1 && position < this->elements ) ? (Object *) * ( this->objects + position ) : NULL; |
klauss | 0:4d17cd9c8f9d | 26 | } |
klauss | 0:4d17cd9c8f9d | 27 | |
klauss | 0:4d17cd9c8f9d | 28 | void Vector::remove_element( int position ){ |
klauss | 29:7246460b73f8 | 29 | if( debug_vector ) debug_msg( "Removing element %d", position ); |
klauss | 0:4d17cd9c8f9d | 30 | if( position > -1 && position < this->elements ){ |
klauss | 0:4d17cd9c8f9d | 31 | if( position == 0 && this->elements == 1 ) { |
klauss | 0:4d17cd9c8f9d | 32 | this->elements = 0; |
klauss | 0:4d17cd9c8f9d | 33 | free( this->objects ); |
klauss | 0:4d17cd9c8f9d | 34 | this->objects = NULL; |
klauss | 0:4d17cd9c8f9d | 35 | }else{ |
klauss | 0:4d17cd9c8f9d | 36 | this->objects[ position ] = this->objects[ --this->elements ]; |
klauss | 4:de46f0d9b14d | 37 | this->objects = ( Object ** )realloc( this->objects, sizeof ( Object * ) * this->elements ); |
klauss | 87:679ee0d594a9 | 38 | if( this->objects == NULL ) memory_is_over = true; |
klauss | 89:0fe315117b00 | 39 | if( debug_memory ) debug_msg("Resize Vector fail"); |
klauss | 0:4d17cd9c8f9d | 40 | } |
klauss | 0:4d17cd9c8f9d | 41 | } |
klauss | 29:7246460b73f8 | 42 | if( debug_vector ) debug_msg( "Removed" ); |
klauss | 0:4d17cd9c8f9d | 43 | } |
klauss | 0:4d17cd9c8f9d | 44 | |
klauss | 0:4d17cd9c8f9d | 45 | int Vector::find_element( Object * e ){ |
klauss | 87:679ee0d594a9 | 46 | if( e != NULL ) |
klauss | 87:679ee0d594a9 | 47 | { |
klauss | 87:679ee0d594a9 | 48 | for( int i = 0; i < this->elements; i++ ) |
klauss | 87:679ee0d594a9 | 49 | if( this->objects[ i ] == e ) return ( i ); |
klauss | 87:679ee0d594a9 | 50 | return ( -3 ); |
klauss | 87:679ee0d594a9 | 51 | } |
klauss | 87:679ee0d594a9 | 52 | else |
klauss | 87:679ee0d594a9 | 53 | { |
klauss | 87:679ee0d594a9 | 54 | return ( -1 ); |
klauss | 87:679ee0d594a9 | 55 | } |
klauss | 0:4d17cd9c8f9d | 56 | } |
klauss | 0:4d17cd9c8f9d | 57 | |
klauss | 0:4d17cd9c8f9d | 58 | //Object * Vector::find_element( int ext, int port ){ |
klauss | 0:4d17cd9c8f9d | 59 | // for( int i = 0; i < this->elements; i++ ){ |
klauss | 0:4d17cd9c8f9d | 60 | // if( this->objects[ i ]->get_ext() == ext && |
klauss | 0:4d17cd9c8f9d | 61 | // this->objects[ i ]->get_port() == port ) return( this->objects[ i ] ); |
klauss | 0:4d17cd9c8f9d | 62 | // } |
klauss | 0:4d17cd9c8f9d | 63 | // return( NULL ); |
klauss | 0:4d17cd9c8f9d | 64 | //} |
klauss | 0:4d17cd9c8f9d | 65 | |
klauss | 0:4d17cd9c8f9d | 66 | int Vector::size(){ return this->elements; } |