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