#include #include "vararray.h" struct var_int_array { int * data; size_t size; size_t allocated_size; }; typedef struct var_int_array varray; varray * var_int_array_new() { /* O(1) */ varray * this = malloc(sizeof(varray)); if (this) { this->data = malloc (sizeof(int)); if (!this->data) { free (this); return 0; } this->size = 0; this->allocated_size = 1; } return this; } int var_int_array_get(const varray * this, size_t idx) { /* O(1) */ return this->data[idx]; } void var_int_array_destroy(varray * this) { /* O(1) */ free (this->data); free (this); } int var_int_array_append(varray * this, int value) { /* O(1) amortized */ if (this->size == this->allocated_size) { int * p = realloc(this->data, this->allocated_size * 2); if (!p) return 0; this->data = p; this->allocated_size *= 2; } this->data[this->size] = value; this->size += 1; return 1; }