source: src/examples/vector_int.c@ b617e4b

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 with_gc
Last change on this file since b617e4b was eab39cd, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

update vector_test to use constructors and destructors

  • Property mode set to 100644
File size: 1.4 KB
RevLine 
[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
12// Last Modified On : Wed Apr 06 17:18:31 2016
[86bd7c1f]13// Update Count : 3
14//
[51b73452]15
16#include "vector_int.h"
17extern "C" {
18#include <stdlib.h>
19#include <assert.h>
20}
21
22#define DEFAULT_CAPACITY 20
23
[eab39cd]24void ?{}( vector_int * vec ) {
25 vec { DEFAULT_CAPACITY };
[51b73452]26}
27
[eab39cd]28void ?{}( vector_int * vec, int reserve ) {
29 vec->last = -1;
30 vec->capacity = reserve;
31 vec->data = malloc( sizeof( int ) * reserve );
[51b73452]32}
33
[eab39cd]34void ^?{}( vector_int * vec ) {
35 free( vec->data );
[51b73452]36}
37
[134b86a]38void reserve( vector_int *vec, int reserve ) {
[86bd7c1f]39 if ( reserve > vec->capacity ) {
40 vec->data = realloc( vec->data, sizeof( int ) * reserve );
41 vec->capacity = reserve;
42 }
[51b73452]43}
44
[134b86a]45void append( vector_int *vec, int element ) {
[86bd7c1f]46 vec->last++;
47 if ( vec->last == vec->capacity ) {
48 vec->capacity *= 2;
49 vec->data = realloc( vec->data, sizeof( int ) * vec->capacity );
50 }
51 vec->data[ vec->last ] = element;
[51b73452]52}
53
54// implement bounded_array
55
[134b86a]56lvalue int ?[?]( vector_int vec, int index ) {
[86bd7c1f]57 return vec.data[ index ];
[51b73452]58}
59
[134b86a]60int last( vector_int vec ) {
[86bd7c1f]61 return vec.last;
[51b73452]62}
63
[86bd7c1f]64
65// Local Variables: //
66// tab-width: 4 //
67// compile-command: "cfa vector_int.c" //
68// End: //
Note: See TracBrowser for help on using the repository browser.