9511_project03

project 3 for algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README LICENSE
source/vector.c (3240B)
   1 #include "../include/vector.h"
   2 
   3 status_t ADT_Vector_create(ADT_Vector_t **v)
   4 {
   5     if(v == NULL) return ERROR_NULL_POINTER;
   6 
   7     if((*v = (ADT_Vector_t *)malloc(sizeof(ADT_Vector_t))) == NULL)
   8         return ERROR_MEMORY;
   9 
  10     if(((*v)->a = (void **)malloc(sizeof(void *) * VECTOR_INIT_SIZE)) == NULL)
  11         return ERROR_MEMORY;
  12 
  13     (*v)->size = 0;
  14     (*v)->alloc = VECTOR_INIT_SIZE;
  15     (*v)->printer = NULL;
  16     (*v)->comparator = NULL;
  17 
  18     return OK;
  19 }
  20 
  21 status_t ADT_Vector_add(ADT_Vector_t **v, void *e)
  22 {
  23     void *tmp;
  24 
  25     if(v == NULL || e == NULL) return ERROR_NULL_POINTER;
  26 
  27     /* Extends the array in case low memory left */
  28     if(((*v)->size + 1) == (*v)->alloc) {
  29         if((tmp = (void **)realloc((*v)->a, sizeof(void *) * (*v)->alloc * VECTOR_GROWTH_FACTOR)) == NULL)
  30             return ERROR_MEMORY;
  31         else {
  32             (*v)->a = tmp;
  33             (*v)->alloc *= VECTOR_GROWTH_FACTOR;
  34         }
  35     }
  36 
  37     (*v)->a[(*v)->size++] = e;
  38 
  39     return OK;
  40 }
  41 
  42 status_t ADT_Vector_destroy(ADT_Vector_t **v)
  43 {
  44     if(v == NULL) return ERROR_NULL_POINTER;
  45 
  46     for(size_t i = 0; i < (*v)->size; i++) {
  47         free((*v)->a[i]);
  48         (*v)->a[i] = NULL;
  49     }
  50 
  51     free((*v)->a);
  52     free(*v);
  53     *v = NULL;
  54 
  55     return OK;
  56 }
  57 
  58 /* Esta funcion no puede crear elementos nuevos, solo setear los que ya estan */
  59 status_t ADT_Vector_set_elem(ADT_Vector_t **v, void *e, size_t pos)
  60 {
  61     if(v == NULL || e == NULL) return ERROR_NULL_POINTER;
  62     else if(pos > (*v)->size) return ERROR_INVALID_POS;
  63 
  64     (*v)->a[pos] = e;
  65 
  66     return OK;
  67 }
  68 
  69 status_t ADT_Vector_get_elem(const ADT_Vector_t *v, void **e, size_t pos)
  70 {
  71     if(v == NULL || e == NULL) return ERROR_NULL_POINTER;
  72 
  73     *e = v->a[pos];
  74 
  75     return OK;
  76 }
  77 
  78 status_t ADT_Vector_get_elem_pos(const ADT_Vector_t *v, void *e, size_t *pos)
  79 {
  80     size_t i;
  81 
  82     if(v == NULL || e == NULL) return ERROR_NULL_POINTER;
  83 
  84     for(i = 0; i < v->size; i++) { 
  85         if(v->comparator(e, v->a[i])) {
  86             *pos = i;
  87             return OK;
  88         }
  89     }
  90 
  91     return ELEM_NOT_FOUND;
  92 }
  93 
  94 status_t ADT_Vector_print(const ADT_Vector_t *v, FILE *fp)
  95 {
  96     status_t st;
  97     size_t i;
  98 
  99     if(v == NULL) return ERROR_NULL_POINTER;
 100 
 101     for(i = 0; i < v->size; i++)
 102         if((st = v->printer(v->a[i], fp)) != OK)
 103             return st;
 104 
 105     return OK;
 106 }
 107 
 108 status_t ADT_Vector_sort(ADT_Vector_t *v, comparator_t pf)
 109 {
 110     if(v == NULL) return ERROR_NULL_POINTER;
 111 
 112     /* Quick sort */
 113     qsort(v->a, v->size, sizeof(void *), pf);
 114 
 115     return OK;
 116 }
 117 
 118 status_t ADT_Vector_set_printer(ADT_Vector_t *v, printer_t pf)
 119 {
 120     if(v == NULL) return ERROR_NULL_POINTER;
 121 
 122     v->printer = pf;
 123 
 124     return OK;
 125 }
 126 
 127 status_t ADT_Vector_set_comparator(ADT_Vector_t *v, comparator_t pf)
 128 {
 129     if(v == NULL) return ERROR_NULL_POINTER;
 130 
 131     v->comparator = pf;
 132 
 133     return OK;
 134 }
 135 
 136 status_t ADT_Vector_export_as_csv(const ADT_Vector_t *v, FILE *fp)
 137 {
 138     status_t st;
 139     size_t i;
 140 
 141     if(v == NULL) return ERROR_NULL_POINTER;
 142 
 143     for(i = 0; i < v->size; i++)
 144         if((st = v->printer(v->a[i], fp)) != OK)
 145             return st;
 146 
 147     return OK;
 148 }
 149 
 150 status_t ADT_Vector_export_as_xml(const ADT_Vector_t *v, FILE *fp)
 151 {
 152     status_t st;
 153     size_t i;
 154 
 155     if(v == NULL) return ERROR_NULL_POINTER;
 156 
 157     fprintf(fp, "%s\n", XML_STR_HEADER);
 158     fprintf(fp, "<%s>\n", XML_STR_ROOT);
 159 
 160     for(i = 0; i < v->size; i++){
 161         fprintf(fp, "\t<%s>\n", XML_STR_ROW);
 162         if((st = v->printer(v->a[i], fp)) != OK)
 163             return st;
 164 
 165         fprintf(fp, "\t</%s>\n", XML_STR_ROW);
 166     }
 167 
 168     fprintf(fp, "</%s>\n", XML_STR_ROOT);
 169     return OK;
 170 }