source: src/tests/vector/vector_int.c @ 2463d0e

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 2463d0e was ed0e67a, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

add stdhdr directory to includes, containing extern "C" include wrappers for standard C include files

  • Property mode set to 100644
File size: 1.6 KB
Line 
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
22void ?{}( vector_int * vec ) {
23        vec { DEFAULT_CAPACITY };
24}
25
26void ?{}( vector_int * vec, int reserve ) {
27        vec->last = -1;
28        vec->capacity = reserve;
29        vec->data = malloc( sizeof( int ) * reserve );
30}
31
32void ?{}( 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
41void ^?{}( vector_int * vec ) {
42        free( vec->data );
43}
44
45void reserve( vector_int *vec, int reserve ) {
46        if ( reserve > vec->capacity ) {
47                vec->data = realloc( vec->data, sizeof( int ) * reserve );
48                vec->capacity = reserve;
49        }
50}
51
52void append( vector_int *vec, int element ) {
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;
59}
60
61// implement bounded_array
62
63lvalue int ?[?]( vector_int * vec, int index ) {
64        return vec->data[ index ];
65}
66
67int 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: //
Note: See TracBrowser for help on using the repository browser.