| [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 | 
|---|
| [ed0e67a] | 11 | // Last Modified By : Peter A. Buhr | 
|---|
|  | 12 | // Last Modified On : Tue Jul  5 21:00:56 2016 | 
|---|
|  | 13 | // Update Count     : 4 | 
|---|
| [86bd7c1f] | 14 | // | 
|---|
| [51b73452] | 15 |  | 
|---|
|  | 16 | #include "vector_int.h" | 
|---|
|  | 17 | #include <stdlib.h> | 
|---|
|  | 18 | #include <assert.h> | 
|---|
|  | 19 |  | 
|---|
|  | 20 | #define DEFAULT_CAPACITY 20 | 
|---|
|  | 21 |  | 
|---|
| [eab39cd] | 22 | void ?{}( vector_int * vec ) { | 
|---|
|  | 23 | vec { DEFAULT_CAPACITY }; | 
|---|
| [51b73452] | 24 | } | 
|---|
|  | 25 |  | 
|---|
| [eab39cd] | 26 | void ?{}( vector_int * vec, int reserve ) { | 
|---|
|  | 27 | vec->last = -1; | 
|---|
|  | 28 | vec->capacity = reserve; | 
|---|
|  | 29 | vec->data = malloc( sizeof( int ) * reserve ); | 
|---|
| [51b73452] | 30 | } | 
|---|
|  | 31 |  | 
|---|
| [1ad1c99e] | 32 | void ?{}( vector_int * vec, vector_int other ) { | 
|---|
|  | 33 | vec->last = other.last; | 
|---|
|  | 34 | vec->capacity = other.capacity; | 
|---|
|  | 35 | vec->data = malloc( sizeof( int ) * other.capacity ); | 
|---|
|  | 36 | for (int i = 0; i < vec->last; i++) { | 
|---|
|  | 37 | vec->data[i] = other.data[i]; | 
|---|
|  | 38 | } | 
|---|
|  | 39 | } | 
|---|
|  | 40 |  | 
|---|
| [eab39cd] | 41 | void ^?{}( vector_int * vec ) { | 
|---|
|  | 42 | free( vec->data ); | 
|---|
| [51b73452] | 43 | } | 
|---|
|  | 44 |  | 
|---|
| [134b86a] | 45 | void reserve( vector_int *vec, int reserve ) { | 
|---|
| [86bd7c1f] | 46 | if ( reserve > vec->capacity ) { | 
|---|
|  | 47 | vec->data = realloc( vec->data, sizeof( int ) * reserve ); | 
|---|
|  | 48 | vec->capacity = reserve; | 
|---|
|  | 49 | } | 
|---|
| [51b73452] | 50 | } | 
|---|
|  | 51 |  | 
|---|
| [134b86a] | 52 | void append( vector_int *vec, int element ) { | 
|---|
| [86bd7c1f] | 53 | vec->last++; | 
|---|
|  | 54 | if ( vec->last == vec->capacity ) { | 
|---|
|  | 55 | vec->capacity *= 2; | 
|---|
|  | 56 | vec->data = realloc( vec->data, sizeof( int ) * vec->capacity ); | 
|---|
|  | 57 | } | 
|---|
|  | 58 | vec->data[ vec->last ] = element; | 
|---|
| [51b73452] | 59 | } | 
|---|
|  | 60 |  | 
|---|
|  | 61 | // implement bounded_array | 
|---|
|  | 62 |  | 
|---|
| [1ad1c99e] | 63 | lvalue int ?[?]( vector_int * vec, int index ) { | 
|---|
|  | 64 | return vec->data[ index ]; | 
|---|
| [51b73452] | 65 | } | 
|---|
|  | 66 |  | 
|---|
| [1ad1c99e] | 67 | int last( vector_int * vec ) { | 
|---|
|  | 68 | return vec->last; | 
|---|
| [51b73452] | 69 | } | 
|---|
|  | 70 |  | 
|---|
| [86bd7c1f] | 71 |  | 
|---|
|  | 72 | // Local Variables: // | 
|---|
|  | 73 | // tab-width: 4 // | 
|---|
|  | 74 | // compile-command: "cfa vector_int.c" // | 
|---|
|  | 75 | // End: // | 
|---|