ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change
on this file since bdd516a was
134b86a,
checked in by Peter A. Buhr <pabuhr@…>, 10 years ago
|
add compiler flag to driver, update examples, fix unnamed bit fields
|
-
Property mode set to
100644
|
File size:
1.1 KB
|
Rev | Line | |
---|
[51b7345] | 1 | // "cfa vector_int.c" |
---|
| 2 | |
---|
| 3 | #include "vector_int.h" |
---|
| 4 | extern "C" { |
---|
| 5 | #include <stdlib.h> |
---|
| 6 | #include <assert.h> |
---|
| 7 | } |
---|
| 8 | |
---|
| 9 | #define DEFAULT_CAPACITY 20 |
---|
| 10 | |
---|
[134b86a] | 11 | vector_int vector_int_allocate() { |
---|
| 12 | return vector_int_allocate( DEFAULT_CAPACITY ); |
---|
[51b7345] | 13 | } |
---|
| 14 | |
---|
[134b86a] | 15 | vector_int vector_int_allocate( int reserve ) { |
---|
| 16 | vector_int new_vector; |
---|
| 17 | new_vector.last = -1; |
---|
| 18 | new_vector.capacity = reserve; |
---|
| 19 | new_vector.data = malloc( sizeof( int ) * reserve ); |
---|
| 20 | return new_vector; |
---|
[51b7345] | 21 | } |
---|
| 22 | |
---|
[134b86a] | 23 | void vector_int_deallocate( vector_int vec ) { |
---|
| 24 | free( vec.data ); |
---|
[51b7345] | 25 | } |
---|
| 26 | |
---|
[134b86a] | 27 | void reserve( vector_int *vec, int reserve ) { |
---|
| 28 | if ( reserve > vec->capacity ) { |
---|
| 29 | vec->data = realloc( vec->data, sizeof( int ) * reserve ); |
---|
| 30 | vec->capacity = reserve; |
---|
| 31 | } |
---|
[51b7345] | 32 | } |
---|
| 33 | |
---|
[134b86a] | 34 | void append( vector_int *vec, int element ) { |
---|
| 35 | vec->last++; |
---|
| 36 | if ( vec->last == vec->capacity ) { |
---|
| 37 | vec->capacity *= 2; |
---|
| 38 | vec->data = realloc( vec->data, sizeof( int ) * vec->capacity ); |
---|
| 39 | } |
---|
| 40 | vec->data[ vec->last ] = element; |
---|
[51b7345] | 41 | } |
---|
| 42 | |
---|
| 43 | // implement bounded_array |
---|
| 44 | |
---|
[134b86a] | 45 | lvalue int ?[?]( vector_int vec, int index ) { |
---|
| 46 | return vec.data[ index ]; |
---|
[51b7345] | 47 | } |
---|
| 48 | |
---|
[134b86a] | 49 | int last( vector_int vec ) { |
---|
| 50 | return vec.last; |
---|
[51b7345] | 51 | } |
---|
| 52 | |
---|
Note: See
TracBrowser
for help on using the repository browser.