source: translator/examples/vector_int.c @ 93885663

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 93885663 was 51b7345, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

initial commit

  • Property mode set to 100644
File size: 1.1 KB
Line 
1// "cfa vector_int.c"
2
3#include "vector_int.h"
4
5#undef __cplusplus
6extern "C" {
7#include <stdlib.h>
8#include <assert.h>
9}
10
11#define DEFAULT_CAPACITY 20
12
13vector_int
14vector_int_allocate()
15{
16  return vector_int_allocate( DEFAULT_CAPACITY );
17}
18
19vector_int
20vector_int_allocate( int reserve )
21{
22  vector_int new_vector;
23  new_vector.last = -1;
24  new_vector.capacity = reserve;
25  new_vector.data = malloc( sizeof( int ) * reserve );
26  return new_vector;
27}
28
29void
30vector_int_deallocate( vector_int vec )
31{
32  free( vec.data );
33}
34
35void
36reserve( vector_int *vec, int reserve )
37{
38  if( reserve > vec->capacity ) {
39    vec->data = realloc( vec->data, sizeof( int ) * reserve );
40    vec->capacity = reserve;
41  }
42}
43
44void append( vector_int *vec, int element )
45{
46  vec->last++;
47  if( vec->last == vec->capacity ) {
48    vec->capacity *= 2;
49    vec->data = realloc( vec->data, sizeof( int ) * vec->capacity );
50  }
51  vec->data[ vec->last ] = element;
52}
53
54// implement bounded_array
55
56lvalue int
57?[?]( vector_int vec, int index )
58{
59  return vec.data[ index ];
60}
61
62int
63array_last( vector_int vec )
64{
65  return vec.last;
66}
67
Note: See TracBrowser for help on using the repository browser.