ADT
aaron-thesis
arm-eh
ast-experimental
cleanup-dtors
ctor
deferred_resn
demangler
enum
forall-pointer-decay
gc_noraii
jacob/cs343-translation
jenkins-sandbox
memory
new-ast
new-ast-unique-expr
new-env
no_list
persistent-indexer
pthread-emulation
qualifiedEnum
resolv-new
string
with_gc
|
Last change
on this file since fe3b61b was 134b86a, checked in by Peter A. Buhr <pabuhr@…>, 11 years ago |
|
add compiler flag to driver, update examples, fix unnamed bit fields
|
-
Property mode
set to
100644
|
|
File size:
1.1 KB
|
| Line | |
|---|
| 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 |
|
|---|
| 11 | vector_int vector_int_allocate() {
|
|---|
| 12 | return vector_int_allocate( DEFAULT_CAPACITY );
|
|---|
| 13 | }
|
|---|
| 14 |
|
|---|
| 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;
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | void vector_int_deallocate( vector_int vec ) {
|
|---|
| 24 | free( vec.data );
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 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 | }
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 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;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | // implement bounded_array
|
|---|
| 44 |
|
|---|
| 45 | lvalue int ?[?]( vector_int vec, int index ) {
|
|---|
| 46 | return vec.data[ index ];
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | int last( vector_int vec ) {
|
|---|
| 50 | return vec.last;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.