#include <stdlib.h>
#include "vector.h"


Vector* _Vector_Constructor(int capacity){
    Vector* vector = (Vector*)malloc(sizeof(Vector));
    if (vector == NULL)
        return NULL;
    vector->capacity=capacity;
    vector->lenght=0;
    vector->elems=(TElem*)malloc(capacity*(sizeof(TElem)));
    if (vector->elems==NULL)
        return NULL;
    return vector;
}

void _Vector_Destructor(Vector* vector){
    if (vector==NULL)
        return;
    free(vector->elems);
    vector->elems=NULL;
    free(vector);
    
}

int _Vector_add(Vector* vector, TElem t){
    if (vector == NULL)
        return 1;
    if (vector->elems==NULL)
        return 1;
    
    if (vector->lenght==vector->capacity)
        _Vector_resize(vector);
    vector->elems[vector->lenght]=t;
    vector->lenght++;
    return 0;
}

int _Vector_delete(Vector* vector,int index){
    if (vector == NULL)
        return 1;
    if (vector->elems==NULL)
        return 1;
    if (vector->lenght<=index)
        return 1;
    
    TElem return_value=vector->elems[index];
    for(int i=index;i<vector->lenght-1;i++)
        vector->elems[i]=vector->elems[i+1];
    vector->lenght--;
    return 0;
}

void _Vector_resize(Vector* vector){
    if (vector == NULL)
        return;
    vector->capacity*=2;
    TElem* aux = (TElem*)realloc(vector->elems,vector->capacity*sizeof(TElem));
    if (aux==NULL)
        return;
    vector->elems=aux;
}

TElem _Vector_getItem(Vector* vector,int index){
    if (vector==NULL)
        return NULL;
    if (index<0 || index >= vector->lenght)
        return NULL;
    return vector->elems[index];
}



int _Vector_getLength(Vector* vector){
    if (vector==NULL)
        return 0;
    return vector->lenght;
}

int _Vector_getCapacity(Vector* vector){
    if (vector==NULL)
        return 0;
    return vector->capacity;
}