// "cfa vector_int.c" #include "vector_int.h" #undef __cplusplus extern "C" { #include #include } #define DEFAULT_CAPACITY 20 vector_int vector_int_allocate() { return vector_int_allocate( DEFAULT_CAPACITY ); } vector_int vector_int_allocate( int reserve ) { vector_int new_vector; new_vector.last = -1; new_vector.capacity = reserve; new_vector.data = malloc( sizeof( int ) * reserve ); return new_vector; } void vector_int_deallocate( vector_int vec ) { free( vec.data ); } void reserve( vector_int *vec, int reserve ) { if( reserve > vec->capacity ) { vec->data = realloc( vec->data, sizeof( int ) * reserve ); vec->capacity = reserve; } } void append( vector_int *vec, int element ) { vec->last++; if( vec->last == vec->capacity ) { vec->capacity *= 2; vec->data = realloc( vec->data, sizeof( int ) * vec->capacity ); } vec->data[ vec->last ] = element; } // implement bounded_array lvalue int ?[?]( vector_int vec, int index ) { return vec.data[ index ]; } int array_last( vector_int vec ) { return vec.last; }