voltando a versao de n aberturas e fechamentos de sockets
Dependencies: EthernetInterface NTPClient mbed-rtos_old mbed
Fork of header_main_public by
vector.cpp@0:4d17cd9c8f9d, 2014-09-09 (annotated)
- Committer:
- klauss
- Date:
- Tue Sep 09 20:01:24 2014 +0000
- Revision:
- 0:4d17cd9c8f9d
- Child:
- 4:de46f0d9b14d
20140726 copy
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 | 0:4d17cd9c8f9d | 12 | void Vector::add( Object * e){ |
klauss | 0:4d17cd9c8f9d | 13 | this->objects = ( Object ** )realloc( this->objects, sizeof ( Object * ) * ++this->elements ); |
klauss | 0:4d17cd9c8f9d | 14 | this->objects[ this->elements - 1 ] = e; |
klauss | 0:4d17cd9c8f9d | 15 | } |
klauss | 0:4d17cd9c8f9d | 16 | |
klauss | 0:4d17cd9c8f9d | 17 | Object * Vector::get_element( int position ){ |
klauss | 0:4d17cd9c8f9d | 18 | return( position > -1 && position < this->elements ) ? (Object *) * ( this->objects + position ) : NULL; |
klauss | 0:4d17cd9c8f9d | 19 | } |
klauss | 0:4d17cd9c8f9d | 20 | |
klauss | 0:4d17cd9c8f9d | 21 | void Vector::remove_element( int position ){ |
klauss | 0:4d17cd9c8f9d | 22 | if( position > -1 && position < this->elements ){ |
klauss | 0:4d17cd9c8f9d | 23 | if( position == 0 && this->elements == 1 ) { |
klauss | 0:4d17cd9c8f9d | 24 | this->elements = 0; |
klauss | 0:4d17cd9c8f9d | 25 | free( this->objects ); |
klauss | 0:4d17cd9c8f9d | 26 | this->objects = NULL; |
klauss | 0:4d17cd9c8f9d | 27 | }else{ |
klauss | 0:4d17cd9c8f9d | 28 | this->objects[ position ] = this->objects[ --this->elements ]; |
klauss | 0:4d17cd9c8f9d | 29 | this->objects = ( Object ** )realloc( this->objects, sizeof ( Object * ) * this->elements ); |
klauss | 0:4d17cd9c8f9d | 30 | } |
klauss | 0:4d17cd9c8f9d | 31 | } |
klauss | 0:4d17cd9c8f9d | 32 | } |
klauss | 0:4d17cd9c8f9d | 33 | |
klauss | 0:4d17cd9c8f9d | 34 | int Vector::find_element( Object * e ){ |
klauss | 0:4d17cd9c8f9d | 35 | for( int i = 0; i < this->elements; i++ ) |
klauss | 0:4d17cd9c8f9d | 36 | if( this->objects[ i ] == e ) return ( i ); |
klauss | 0:4d17cd9c8f9d | 37 | return ( -3 ); |
klauss | 0:4d17cd9c8f9d | 38 | } |
klauss | 0:4d17cd9c8f9d | 39 | |
klauss | 0:4d17cd9c8f9d | 40 | //Object * Vector::find_element( int ext, int port ){ |
klauss | 0:4d17cd9c8f9d | 41 | // for( int i = 0; i < this->elements; i++ ){ |
klauss | 0:4d17cd9c8f9d | 42 | // if( this->objects[ i ]->get_ext() == ext && |
klauss | 0:4d17cd9c8f9d | 43 | // this->objects[ i ]->get_port() == port ) return( this->objects[ i ] ); |
klauss | 0:4d17cd9c8f9d | 44 | // } |
klauss | 0:4d17cd9c8f9d | 45 | // return( NULL ); |
klauss | 0:4d17cd9c8f9d | 46 | //} |
klauss | 0:4d17cd9c8f9d | 47 | |
klauss | 0:4d17cd9c8f9d | 48 | int Vector::size(){ return this->elements; } |