source: translator/examples/vector_int.c @ 134b86a

aaron-thesisarm-ehcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationresolv-newstringwith_gc
Last change on this file since 134b86a was 134b86a, checked in by Peter A. Buhr <pabuhr@…>, 8 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"
4extern "C" {
5#include <stdlib.h>
6#include <assert.h>
7}
8
9#define DEFAULT_CAPACITY 20
10
11vector_int vector_int_allocate() {
12    return vector_int_allocate( DEFAULT_CAPACITY );
13}
14
15vector_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
23void vector_int_deallocate( vector_int vec ) {
24    free( vec.data );
25}
26
27void 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
34void 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
45lvalue int ?[?]( vector_int vec, int index ) {
46    return vec.data[ index ];
47}
48
49int last( vector_int vec ) {
50    return vec.last;
51}
52
Note: See TracBrowser for help on using the repository browser.