Last active
June 23, 2022 14:17
-
-
Save AmirHosein-Gharaati/4f569a1caa93196da1035b69479eaf53 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#define COEFFIECIENT 2 | |
typedef struct { | |
int capacity, used_size; | |
int *data; | |
} Vector; | |
void vec_init(Vector *v, int capacity) { | |
if (capacity <= 0) { | |
return; | |
} | |
v->data = malloc(capacity * sizeof(int)); | |
v->capacity = capacity; | |
v->used_size = 0; | |
} | |
void vec_push(Vector *V, int element) { | |
if (V->used_size == V->capacity) { | |
V->data = realloc(V->data, V->capacity * COEFFIECIENT * sizeof(int)); | |
V->capacity *= COEFFIECIENT; | |
} | |
V->data[V->used_size] = element; | |
V->used_size++; | |
} | |
void vec_pop(Vector *V) { | |
if (V->used_size == 0 || V->capacity == 0) | |
return; | |
V->used_size--; | |
if (V->used_size <= V->capacity / COEFFIECIENT) { | |
V->data = realloc(V->data, (V->capacity / COEFFIECIENT) * sizeof(int)); | |
V->capacity /= COEFFIECIENT; | |
} | |
} | |
void vec_delete(Vector *V) { free(V->data); } | |
int vec_get(Vector *V, int index) { return (V->data)[index]; } | |
// ------------------------------- | |
typedef struct { | |
int capacity, used_size; | |
Vector *vectors; | |
} Container; | |
void container_init(Container *C, int capacity) { | |
C->vectors = malloc(sizeof(Vector) * capacity); | |
C->capacity = capacity; | |
C->used_size = 0; | |
} | |
// ASSUMES V's ownership!!! | |
void container_push(Container *C, const Vector *V) { | |
if (C->used_size == C->capacity) { | |
C->vectors = | |
realloc(C->vectors, C->capacity * COEFFIECIENT * sizeof(Vector)); | |
C->capacity *= COEFFIECIENT; | |
} | |
*(C->vectors + C->used_size++) = *V; | |
} | |
void container_pop(Container *C) { | |
if (C->used_size == 0 || C->capacity == 0) | |
return; | |
vec_delete(C->vectors + --(C->used_size)); | |
if (C->used_size <= C->capacity / COEFFIECIENT) { | |
C->vectors = | |
realloc(C->vectors, (C->capacity / COEFFIECIENT) * sizeof(Vector)); | |
C->capacity /= COEFFIECIENT; | |
} | |
} | |
Vector *container_get(Container *C, int index) { return C->vectors + index; } | |
void container_delete(Container *C) { | |
for (int i = 0; i < C->used_size; i++) | |
vec_delete(container_get(C, i)); | |
free(C->vectors); | |
} | |
int main() { | |
Container C; | |
container_init(&C, 1); | |
for (int i = 0; i < 5; i++) { | |
Vector V; | |
vec_init(&V, 10); | |
for (int j = 0; j < 99; j++) { | |
vec_push(&V, j); | |
} | |
container_push(&C, &V); | |
} | |
container_pop(&C); | |
for (int i = 0; i < 4; i++) { | |
for (int j = 0; j < 9; j++) { | |
printf("%d ", vec_get(container_get(&C, i), j)); | |
} | |
printf("\n"); | |
} | |
vec_pop(container_get(&C, 2)); | |
container_delete(&C); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment