source: src/examples/vector_int.c @ 09a9a8b

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 09a9a8b was 843054c2, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

licencing: seventh groups of files

  • Property mode set to 100644
File size: 1.1 KB
RevLine 
[51b7345]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
[134b86a]11vector_int vector_int_allocate() {
12    return vector_int_allocate( DEFAULT_CAPACITY );
[51b7345]13}
14
[134b86a]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;
[51b7345]21}
22
[134b86a]23void vector_int_deallocate( vector_int vec ) {
24    free( vec.data );
[51b7345]25}
26
[134b86a]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    }
[51b7345]32}
33
[134b86a]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;
[51b7345]41}
42
43// implement bounded_array
44
[134b86a]45lvalue int ?[?]( vector_int vec, int index ) {
46    return vec.data[ index ];
[51b7345]47}
48
[134b86a]49int last( vector_int vec ) {
50    return vec.last;
[51b7345]51}
52
Note: See TracBrowser for help on using the repository browser.