This repository has been archived on 2021-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
sys_prog/array/array.c

61 lines
953 B
C
Raw Normal View History

2019-11-04 10:47:14 +00:00
#include "array.h"
struct var_int_array {
int* data;
size_t size;
size_t capacity;
};
varray* var_int_array_new() {
varray* a = malloc(sizeof(struct var_int_array));
if (a) {
a->size = 0;
a->capacity = 10;
a->data = malloc(sizeof(int) * a->capacity);
if (!a->data) {
free(a);
}
}
return a;
}
void var_int_array_destroy(varray* v) {
free(v->data);
free(v);
}
size_t var_int_array_size(const varray* v) {
return v->size;
}
int var_int_array_append(varray* v, int value) {
if (v->size == v->capacity) {
size_t new_cap = v->capacity * 2;
int* new = realloc(v->data, new_cap);
if (!new) {
return 0;
}
v->data = new;
v->capacity = new_cap;
}
v->data[v->size++] = value;
return 1;
}
int var_int_array_pop(varray* v) {
return v->data[--v->size];
}
int var_int_array_get(const varray* v, size_t idx) {
return v->data[idx];
}
void var_int_array_put(varray* v, size_t idx, int val) {
v->data[idx] = val;
}