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 a0d9f94 was 51b73452, checked in by Peter A. Buhr <pabuhr@…>, 11 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
|
|---|
| 6 | extern "C" {
|
|---|
| 7 | #include <stdlib.h>
|
|---|
| 8 | #include <assert.h>
|
|---|
| 9 | }
|
|---|
| 10 |
|
|---|
| 11 | #define DEFAULT_CAPACITY 20
|
|---|
| 12 |
|
|---|
| 13 | vector_int
|
|---|
| 14 | vector_int_allocate()
|
|---|
| 15 | {
|
|---|
| 16 | return vector_int_allocate( DEFAULT_CAPACITY );
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | vector_int
|
|---|
| 20 | vector_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 |
|
|---|
| 29 | void
|
|---|
| 30 | vector_int_deallocate( vector_int vec )
|
|---|
| 31 | {
|
|---|
| 32 | free( vec.data );
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | void
|
|---|
| 36 | reserve( 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 |
|
|---|
| 44 | void 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 |
|
|---|
| 56 | lvalue int
|
|---|
| 57 | ?[?]( vector_int vec, int index )
|
|---|
| 58 | {
|
|---|
| 59 | return vec.data[ index ];
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | int
|
|---|
| 63 | array_last( vector_int vec )
|
|---|
| 64 | {
|
|---|
| 65 | return vec.last;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.