Advertisement
Guest User

Untitled

a guest
Sep 16th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.23 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include "vector.h"
  3.  
  4. /* Prof., desculpa por ter muitos envios no run.codes,
  5.    ainda não instalei a virtualbox aqui no meu pc, aí
  6.    eu estava compilando direto no run.codes mesmo
  7. */
  8.  
  9. struct vector
  10. {
  11.     int last, capacity;
  12.     ITEM **items;
  13. };
  14.  
  15. Vector *vector_new(int capacity) //OK
  16. {
  17.     if(capacity <= 0)
  18.     {
  19.         return NULL;
  20.     }
  21.    
  22.     Vector *vec = (Vector *) malloc(capacity * sizeof(Vector));
  23.    
  24.     if(vec == NULL)
  25.     {
  26.         return NULL;
  27.     }
  28.    
  29.     vec->last = -1;
  30.    
  31.     return vec;
  32. }
  33.  
  34. void vector_free(Vector **vec)
  35. {
  36.     if(vec != NULL || *vec != NULL)
  37.     {
  38.         int i;
  39.         if((*vec)->last >= 0)
  40.         {
  41.             for(i = 0; i <= (*vec)->last; i++)
  42.             {
  43.                 item_free(&((*vec)->items[i]));
  44.             }
  45.         }
  46.        
  47.         free(*vec);
  48.         *vec = NULL;
  49.     }
  50.    
  51. }
  52.  
  53. int vector_size(const Vector *vec) //OK
  54. {
  55.     if(vec == NULL)
  56.     {
  57.         return 0;
  58.     }
  59.    
  60.     return (vec->last+1);
  61. }
  62.  
  63. int vector_capacity(const Vector *vec) //OK
  64. {
  65.     if(vec == NULL)
  66.     {
  67.         return 0;
  68.     }
  69.    
  70.     return vec->capacity;
  71. }
  72.  
  73. int vector_empty(const Vector *vec) //OK
  74. {
  75.     if(vec == NULL || vec->items == NULL)
  76.     {
  77.         return 1;
  78.     }
  79.    
  80.     if(vec->last == -1)
  81.     {
  82.         return 0;
  83.     }
  84.    
  85.     return 1;
  86. }
  87.  
  88. int vector_add(Vector *vec, ITEM *item)
  89. {
  90.     int s, c;
  91.     if(vec == NULL || item == NULL)
  92.     {
  93.         return 0;
  94.     }
  95.    
  96.     s = vector_size(vec);
  97.     c = vector_capacity(vec);
  98.    
  99.     if(s == c)
  100.     {
  101.         vec = (Vector *) realloc(vec, (vec->capacity * 2) * sizeof(Vector));
  102.         vec->capacity = vec->capacity * 2;
  103.     }
  104.    
  105.     vec->items[++vec->last] = item;
  106.    
  107.     return 1;
  108. }
  109.  
  110. ITEM *vector_remove(Vector *vec, int key) //OK
  111. {
  112.     if(vec == NULL)
  113.     {
  114.         return NULL;
  115.     }
  116.    
  117.     int i, j;
  118.    
  119.     Vector *aux;
  120.    
  121.     for(i = 0; i <= vec->last; i++)
  122.     {
  123.         if(vec->items[i]->key == key)
  124.         {
  125.             aux->items[0] = vec->items[i];
  126.             for(j = i; j < vec->last; j++)
  127.             {
  128.                 item_free(&(vec->items[j]));
  129.                 vec->items[j] = vec->items[j + 1];
  130.             }
  131.             vec->last--;
  132.             return aux->items[0];
  133.         }
  134.     }
  135.    
  136.     return NULL;
  137. }
  138.  
  139. ITEM *vector_get(const Vector *vec, int key) //OK
  140. {
  141.     if(vec == NULL)
  142.     {
  143.         return NULL;
  144.     }
  145.    
  146.     int i;
  147.    
  148.     for(i = 0; i <= vec->last; i++)
  149.     {
  150.         if(vec->items[i]->key == key)
  151.         {
  152.             return vec->items[i];
  153.         }
  154.     }
  155.    
  156.     return NULL;
  157. }
  158.  
  159. ITEM *vector_at(const Vector *vec, int pos) //OK
  160. {
  161.     if(vec == NULL || pos >= 0 || pos < vec->capacity)
  162.     {
  163.         return NULL;
  164.     }
  165.    
  166.     return vec->items[pos];
  167. }
  168.  
  169. Vector *vector_copy(Vector *dst, const Vector *src)
  170. {
  171.     if(dst == NULL || src == NULL)
  172.     {
  173.         return NULL;
  174.     }
  175.    
  176.     int i, s, c;
  177.    
  178.     for(i = 0; i <= src->last; i++)
  179.     {
  180.         item_free(&(dst->items[i]));
  181.         dst->items[i] = src->items[i];
  182.     }
  183.    
  184.     s = vector_size(dst);
  185.     c = vector_capacity(dst);
  186.    
  187.     if(s == c)
  188.     {
  189.         dst = (Vector *) realloc(dst, (dst->capacity * 2) * sizeof(Vector));
  190.         dst->capacity = dst->capacity * 2;
  191.     }
  192.    
  193.     return dst;
  194. }
  195.  
  196. Vector *vector_rev(Vector *rev, const Vector *vec)
  197. {
  198.     if(rev == NULL || vec == NULL)
  199.     {
  200.         return NULL;
  201.     }
  202.    
  203.     int i, j = 0, s, c;
  204.    
  205.     for(i = rev->last; i >= 0; i--)
  206.     {
  207.         item_free(&(rev->items[i]));
  208.         rev->items[j++] = vec->items[i];
  209.     }
  210.    
  211.     s = vector_size(rev);
  212.     c = vector_capacity(rev);
  213.    
  214.     if(s == c)
  215.     {
  216.         rev = (Vector *) realloc(rev, (rev->capacity * 2) * sizeof(Vector));
  217.         rev->capacity = rev->capacity * 2;
  218.     }
  219.    
  220.     return rev;
  221. }
  222.  
  223. Vector *vector_cat(const Vector *vec1, const Vector *vec2, Vector *cat)
  224. {
  225.     if(vec1 == NULL || vec2 == NULL || cat == NULL)
  226.     {
  227.         return NULL;
  228.     }
  229.    
  230.     int i, j = 0, s, c;
  231.    
  232.     for(i = 0; i <= vec1->last; i++)
  233.     {
  234.         item_free(&(cat->items[i]));
  235.         cat->items[i] = vec1->items[i];
  236.     }
  237.    
  238.     s = vector_size(cat);
  239.     c = vector_capacity(cat);
  240.    
  241.     if(s == c)
  242.     {
  243.         cat = (Vector *) realloc(cat, (cat->capacity * 2) * sizeof(Vector));
  244.         cat->capacity = cat->capacity * 2;
  245.     }
  246.    
  247.     for(i = cat->last; i <= vec2->last; i++)
  248.     {
  249.         item_free(&(cat->items[i]));
  250.         cat->items[i] = vec2->items[j++];
  251.     }
  252.    
  253.     s = vector_size(cat);
  254.     c = vector_capacity(cat);
  255.    
  256.     if(s == c)
  257.     {
  258.         cat = (Vector *) realloc(cat, (cat->capacity * 2) * sizeof(Vector));
  259.         cat->capacity = cat->capacity * 2;
  260.     }
  261.    
  262.     return cat;
  263. }
  264.  
  265. void vector_print(const Vector *vec) {
  266.     if (vec != NULL) {
  267.         int i;
  268.         for (i = 0; i <= vec->last; i++)
  269.             item_print(vec->items[i]);
  270.     }
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement