Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include "vector.h"
- /* Prof., desculpa por ter muitos envios no run.codes,
- ainda não instalei a virtualbox aqui no meu pc, aí
- eu estava compilando direto no run.codes mesmo
- */
- struct vector
- {
- int last, capacity;
- ITEM **items;
- };
- Vector *vector_new(int capacity) //OK
- {
- if(capacity <= 0)
- {
- return NULL;
- }
- Vector *vec = (Vector *) malloc(capacity * sizeof(Vector));
- if(vec == NULL)
- {
- return NULL;
- }
- vec->last = -1;
- return vec;
- }
- void vector_free(Vector **vec)
- {
- if(vec != NULL || *vec != NULL)
- {
- int i;
- if((*vec)->last >= 0)
- {
- for(i = 0; i <= (*vec)->last; i++)
- {
- item_free(&((*vec)->items[i]));
- }
- }
- free(*vec);
- *vec = NULL;
- }
- }
- int vector_size(const Vector *vec) //OK
- {
- if(vec == NULL)
- {
- return 0;
- }
- return (vec->last+1);
- }
- int vector_capacity(const Vector *vec) //OK
- {
- if(vec == NULL)
- {
- return 0;
- }
- return vec->capacity;
- }
- int vector_empty(const Vector *vec) //OK
- {
- if(vec == NULL || vec->items == NULL)
- {
- return 1;
- }
- if(vec->last == -1)
- {
- return 0;
- }
- return 1;
- }
- int vector_add(Vector *vec, ITEM *item)
- {
- int s, c;
- if(vec == NULL || item == NULL)
- {
- return 0;
- }
- s = vector_size(vec);
- c = vector_capacity(vec);
- if(s == c)
- {
- vec = (Vector *) realloc(vec, (vec->capacity * 2) * sizeof(Vector));
- vec->capacity = vec->capacity * 2;
- }
- vec->items[++vec->last] = item;
- return 1;
- }
- ITEM *vector_remove(Vector *vec, int key) //OK
- {
- if(vec == NULL)
- {
- return NULL;
- }
- int i, j;
- Vector *aux;
- for(i = 0; i <= vec->last; i++)
- {
- if(vec->items[i]->key == key)
- {
- aux->items[0] = vec->items[i];
- for(j = i; j < vec->last; j++)
- {
- item_free(&(vec->items[j]));
- vec->items[j] = vec->items[j + 1];
- }
- vec->last--;
- return aux->items[0];
- }
- }
- return NULL;
- }
- ITEM *vector_get(const Vector *vec, int key) //OK
- {
- if(vec == NULL)
- {
- return NULL;
- }
- int i;
- for(i = 0; i <= vec->last; i++)
- {
- if(vec->items[i]->key == key)
- {
- return vec->items[i];
- }
- }
- return NULL;
- }
- ITEM *vector_at(const Vector *vec, int pos) //OK
- {
- if(vec == NULL || pos >= 0 || pos < vec->capacity)
- {
- return NULL;
- }
- return vec->items[pos];
- }
- Vector *vector_copy(Vector *dst, const Vector *src)
- {
- if(dst == NULL || src == NULL)
- {
- return NULL;
- }
- int i, s, c;
- for(i = 0; i <= src->last; i++)
- {
- item_free(&(dst->items[i]));
- dst->items[i] = src->items[i];
- }
- s = vector_size(dst);
- c = vector_capacity(dst);
- if(s == c)
- {
- dst = (Vector *) realloc(dst, (dst->capacity * 2) * sizeof(Vector));
- dst->capacity = dst->capacity * 2;
- }
- return dst;
- }
- Vector *vector_rev(Vector *rev, const Vector *vec)
- {
- if(rev == NULL || vec == NULL)
- {
- return NULL;
- }
- int i, j = 0, s, c;
- for(i = rev->last; i >= 0; i--)
- {
- item_free(&(rev->items[i]));
- rev->items[j++] = vec->items[i];
- }
- s = vector_size(rev);
- c = vector_capacity(rev);
- if(s == c)
- {
- rev = (Vector *) realloc(rev, (rev->capacity * 2) * sizeof(Vector));
- rev->capacity = rev->capacity * 2;
- }
- return rev;
- }
- Vector *vector_cat(const Vector *vec1, const Vector *vec2, Vector *cat)
- {
- if(vec1 == NULL || vec2 == NULL || cat == NULL)
- {
- return NULL;
- }
- int i, j = 0, s, c;
- for(i = 0; i <= vec1->last; i++)
- {
- item_free(&(cat->items[i]));
- cat->items[i] = vec1->items[i];
- }
- s = vector_size(cat);
- c = vector_capacity(cat);
- if(s == c)
- {
- cat = (Vector *) realloc(cat, (cat->capacity * 2) * sizeof(Vector));
- cat->capacity = cat->capacity * 2;
- }
- for(i = cat->last; i <= vec2->last; i++)
- {
- item_free(&(cat->items[i]));
- cat->items[i] = vec2->items[j++];
- }
- s = vector_size(cat);
- c = vector_capacity(cat);
- if(s == c)
- {
- cat = (Vector *) realloc(cat, (cat->capacity * 2) * sizeof(Vector));
- cat->capacity = cat->capacity * 2;
- }
- return cat;
- }
- void vector_print(const Vector *vec) {
- if (vec != NULL) {
- int i;
- for (i = 0; i <= vec->last; i++)
- item_print(vec->items[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement