Now using 2 spaces instead of tabs. Fix copyright years. Improve documentation
[cgds.git] / src / Vector.c
1 /**
2 * @file Vector.c
3 */
4
5 #include "cgds/Vector.h"
6
7 //////////////////
8 // Vector logic //
9 //////////////////
10
11 void _vector_init(Vector* vector, size_t dataSize)
12 {
13 vector->datas = NULL;
14 vector->dataSize = dataSize;
15 vector->size = 0;
16 vector->capacity = 0;
17 }
18
19 Vector* _vector_new(size_t dataSize)
20 {
21 Vector* vector = (Vector*) safe_malloc(sizeof (Vector));
22 _vector_init(vector, dataSize);
23 return vector;
24 }
25
26 Vector* vector_copy(Vector* vector)
27 {
28 Vector* vectorCopy = _vector_new(vector->dataSize);
29 vectorCopy->size = vector->size;
30 vectorCopy->capacity = vector->capacity;
31 vectorCopy->datas = (void**) safe_malloc(vector->capacity * sizeof (void*));
32 for (UInt i = 0; i < vector->size; i++)
33 {
34 vectorCopy->datas[i] = safe_malloc(vector->dataSize);
35 memcpy(vectorCopy->datas[i], vector->datas[i], vector->dataSize);
36 }
37 return vectorCopy;
38 }
39
40 bool vector_empty(Vector* vector)
41 {
42 return (vector->size == 0);
43 }
44
45 UInt vector_size(Vector* vector)
46 {
47 return vector->size;
48 }
49
50 void _vector_realloc(Vector* vector, UInt newCapacity)
51 {
52 void** rellocatedDatas = (void**) safe_malloc(newCapacity * sizeof (void*));
53 for (UInt i = 0; i < vector->size; i++)
54 {
55 rellocatedDatas[i] = (void*) safe_malloc(vector->dataSize);
56 memcpy(rellocatedDatas[i], vector->datas[i], vector->dataSize);
57 safe_free(vector->datas[i]);
58 }
59 safe_free(vector->datas);
60 vector->datas = rellocatedDatas;
61 vector->capacity = newCapacity;
62 }
63
64 void _vector_push(Vector* vector, void* data)
65 {
66 void* pData = safe_malloc(vector->dataSize);
67 memcpy(pData, data, vector->dataSize);
68 if (vector->size >= vector->capacity)
69 {
70 UInt increasedCapacity = vector->capacity > 0 ? 2 * vector->capacity : 1;
71 _vector_realloc(vector, increasedCapacity);
72 }
73 vector->datas[vector->size] = pData;
74 vector->size++;
75 }
76
77 void vector_pop(Vector* vector)
78 {
79 safe_free(vector->datas[vector_size(vector) - 1]);
80 vector->size--;
81 if (vector_size(vector) <= (vector->capacity >> 1))
82 _vector_realloc(vector, vector->capacity >> 1);
83 }
84
85 void* _vector_get(Vector* vector, UInt index)
86 {
87 return vector->datas[index];
88 }
89
90 void _vector_set(Vector* vector, UInt index, void* data)
91 {
92 memcpy(vector->datas[index], data, vector->dataSize);
93 }
94
95 void vector_clear(Vector* vector)
96 {
97 for (UInt i = 0; i < vector->size; i++)
98 safe_free(vector->datas[i]);
99 safe_free(vector->datas);
100 _vector_init(vector, vector->dataSize);
101 }
102
103 void vector_destroy(Vector* vector)
104 {
105 vector_clear(vector);
106 safe_free(vector);
107 }
108
109 ////////////////////
110 // Iterator logic //
111 ////////////////////
112
113 VectorIterator* vector_get_iterator(Vector* vector)
114 {
115 VectorIterator* vectorI =
116 (VectorIterator*) safe_malloc(sizeof (VectorIterator));
117 vectorI->vector = vector;
118 vectorI_reset_begin(vectorI);
119 return vectorI;
120 }
121
122 void vectorI_reset_begin(VectorIterator* vectorI)
123 {
124 vectorI->current = vectorI->vector->datas;
125 }
126
127 void vectorI_reset_end(VectorIterator* vectorI)
128 {
129 vectorI->current = vectorI->vector->datas + vectorI->vector->size - 1;
130 }
131
132 bool vectorI_has_data(VectorIterator* vectorI)
133 {
134 return (vectorI->current >= vectorI->vector->datas &&
135 vectorI->current < vectorI->vector->datas + vectorI->vector->size);
136 }
137
138 void* _vectorI_get(VectorIterator* vectorI)
139 {
140 return *(vectorI->current);
141 }
142
143 void _vectorI_set(VectorIterator* vectorI, void* data)
144 {
145 *(vectorI->current) = data;
146 }
147
148 void vectorI_move_next(VectorIterator* vectorI)
149 {
150 vectorI->current++;
151 }
152
153 void vectorI_move_prev(VectorIterator* vectorI)
154 {
155 vectorI->current--;
156 }
157
158 void vectorI_destroy(VectorIterator* vectorI)
159 {
160 safe_free(vectorI);
161 }