Skip to content

Instantly share code, notes, and snippets.

@volgar1x
Last active May 20, 2018 21:12
Show Gist options
  • Save volgar1x/4f4319552062d6bbd6b210f36d6e5e71 to your computer and use it in GitHub Desktop.
Save volgar1x/4f4319552062d6bbd6b210f36d6e5e71 to your computer and use it in GitHub Desktop.
typedef struct {
Element* start;
size_t len;
size_t cap;
} vector;
vector* vector_new () {
vector* vec = (vector*) malloc(sizeof(vec));
vec->cap = 10;
vec->start = (Element*) malloc(sizeof(Element) * vec->cap);
return vec;
}
void vector_push (vector* vec, Element* elem) {
if (vec->len >= vec->cap) {
vector_grow(vec);
}
vec->start[vec->len++] = elem;
}
void vector_grow (vector* vec) {
int newCap = vec->cap * 2;
Element* newStart = (Element*) malloc(sizeof(newStart) * newCap);
memcpy(newStart, vec->start);
vec->start = newStart;
vec->cap = newCap;
}
void vector_free (vector* vec) {
free(vec->start);
free(vec);
}
vector matrice[10];
matrice[0] = vector_new();
int i;
for (i = 0; i < random(); i++) {
vector_push(matrice[0], Element{});
}
matrice[1] = vector_new();
matrice[2] = vector_new();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment