[86bd7c1f] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
[eab39cd] | 7 | // vector_int.c -- |
---|
[86bd7c1f] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Wed May 27 17:56:53 2015 |
---|
[eab39cd] | 11 | // Last Modified By : Rob Schluntz |
---|
[1ad1c99e] | 12 | // Last Modified On : Wed Apr 27 17:27:12 2016 |
---|
[86bd7c1f] | 13 | // Update Count : 3 |
---|
| 14 | // |
---|
[51b7345] | 15 | |
---|
| 16 | #include "vector_int.h" |
---|
| 17 | extern "C" { |
---|
| 18 | #include <stdlib.h> |
---|
| 19 | #include <assert.h> |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | #define DEFAULT_CAPACITY 20 |
---|
| 23 | |
---|
[eab39cd] | 24 | void ?{}( vector_int * vec ) { |
---|
| 25 | vec { DEFAULT_CAPACITY }; |
---|
[51b7345] | 26 | } |
---|
| 27 | |
---|
[eab39cd] | 28 | void ?{}( vector_int * vec, int reserve ) { |
---|
| 29 | vec->last = -1; |
---|
| 30 | vec->capacity = reserve; |
---|
| 31 | vec->data = malloc( sizeof( int ) * reserve ); |
---|
[51b7345] | 32 | } |
---|
| 33 | |
---|
[1ad1c99e] | 34 | void ?{}( vector_int * vec, vector_int other ) { |
---|
| 35 | vec->last = other.last; |
---|
| 36 | vec->capacity = other.capacity; |
---|
| 37 | vec->data = malloc( sizeof( int ) * other.capacity ); |
---|
| 38 | for (int i = 0; i < vec->last; i++) { |
---|
| 39 | vec->data[i] = other.data[i]; |
---|
| 40 | } |
---|
| 41 | } |
---|
| 42 | |
---|
[eab39cd] | 43 | void ^?{}( vector_int * vec ) { |
---|
| 44 | free( vec->data ); |
---|
[51b7345] | 45 | } |
---|
| 46 | |
---|
[134b86a] | 47 | void reserve( vector_int *vec, int reserve ) { |
---|
[86bd7c1f] | 48 | if ( reserve > vec->capacity ) { |
---|
| 49 | vec->data = realloc( vec->data, sizeof( int ) * reserve ); |
---|
| 50 | vec->capacity = reserve; |
---|
| 51 | } |
---|
[51b7345] | 52 | } |
---|
| 53 | |
---|
[134b86a] | 54 | void append( vector_int *vec, int element ) { |
---|
[86bd7c1f] | 55 | vec->last++; |
---|
| 56 | if ( vec->last == vec->capacity ) { |
---|
| 57 | vec->capacity *= 2; |
---|
| 58 | vec->data = realloc( vec->data, sizeof( int ) * vec->capacity ); |
---|
| 59 | } |
---|
| 60 | vec->data[ vec->last ] = element; |
---|
[51b7345] | 61 | } |
---|
| 62 | |
---|
| 63 | // implement bounded_array |
---|
| 64 | |
---|
[1ad1c99e] | 65 | lvalue int ?[?]( vector_int * vec, int index ) { |
---|
| 66 | return vec->data[ index ]; |
---|
[51b7345] | 67 | } |
---|
| 68 | |
---|
[1ad1c99e] | 69 | int last( vector_int * vec ) { |
---|
| 70 | return vec->last; |
---|
[51b7345] | 71 | } |
---|
| 72 | |
---|
[86bd7c1f] | 73 | |
---|
| 74 | // Local Variables: // |
---|
| 75 | // tab-width: 4 // |
---|
| 76 | // compile-command: "cfa vector_int.c" // |
---|
| 77 | // End: // |
---|