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