vector.h
Go to the documentation of this file.
1 
9 #ifndef _VECTOR_H_
10 #define _VECTOR_H_
11 
12 #include <stdlib.h>
13 
20 #define Vector(t) struct {int size, capacity; t *data; }
21 
26 #define vectInit(vect) ((vect).size = (vect).capacity = 0, (vect).data = 0)
27 
32 #define vectFree(vect) free((vect).data)
33 
40 #define vectAt(vect, index) ((vect).data[(index)])
41 
47 #define vectSize(vect) ((vect).size)
48 
55 #define vectPush(type, vect, value) do { \
56  if((vect).size == (vect).capacity) { \
57  (vect).capacity = ((vect).capacity ? (vect).capacity * 2 : 10); \
58  (vect).data = (type*)realloc((vect).data, sizeof(type) * (vect).capacity); \
59  } \
60  (vect).data[(vect).size++] = value; \
61  } while(0)
62 
69 #define vectIndexOf(vect, value, out) do { \
70  out = -1; \
71  for(int _i = 0; _i < vectSize(vect); ++_i) { \
72  if(vectAt(vect, _i) == value) { \
73  out = _i; \
74  break; \
75  } \
76  } \
77  } while(0)
78 
83 #define vectRemoveLast(vect) do { \
84  if((vect).size > 0) { \
85  --(vect).size; \
86  } \
87  } while(0)
88 
95 #define vectResize(type, vect, sizeToIncrease) do { \
96  if(((sizeToIncrease)+(vect).size) < (vect).capacity) { \
97  (vect).size += sizeToIncrease; \
98  } else { \
99  (vect).data = (type*)realloc((vect).data, sizeof(type) * (sizeToIncrease)); \
100  (vect).size += (sizeToIncrease); \
101  (vect).capacity += (sizeToIncrease); \
102  } \
103  } while(0) \
104 
105 
111 #define vectFill(type, vect, sizeToInit) do { \
112  (vect).size = (vect).capacity = (sizeToInit); \
113  (vect).data = (type*)malloc(sizeof(type) * (sizeToInit)); \
114  } while(0) \
115 
116 #endif // _VECTOR_H_