improve test/makemain.sh, move queu_pop code to vector class
[cgds.git] / src / Vector.c
CommitLineData
a7868768
BA
1/**
2 * @file Vector.cpp
3 */
4
5#include "cgds/Vector.h"
6
7//////////////////
8// Vector logic //
9//////////////////
10
11void _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
19Vector* _vector_new(size_t dataSize)
20{
21 Vector* vector = (Vector*) safe_malloc(sizeof (Vector));
22 _vector_init(vector, dataSize);
23 return vector;
24}
25
26Vector* 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
40Bool vector_empty(Vector* vector)
41{
42 return (vector->size == 0);
43}
44
45UInt vector_size(Vector* vector)
46{
47 return vector->size;
48}
49
50void _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
64void _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
77void 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
8306616e
BA
85void vector_pop_first(Vector* vector)
86{
87 safe_free(vector->datas[0]);
88 vector->datas++;
89 vector->size--;
90 if (vector_size(vector) <= (vector->capacity >> 1))
91 _vector_realloc(vector, vector->capacity >> 1);
92}
93
a7868768
BA
94void* _vector_get(Vector* vector, UInt index)
95{
96 return vector->datas[index];
97}
98
99void _vector_set(Vector* vector, UInt index, void* data)
100{
101 memcpy(vector->datas[index], data, vector->dataSize);
102}
103
104void vector_clear(Vector* vector)
105{
106 for (UInt i = 0; i < vector->size; i++)
107 safe_free(vector->datas[i]);
108 safe_free(vector->datas);
109 _vector_init(vector, vector->dataSize);
110}
111
112void vector_destroy(Vector* vector)
113{
114 vector_clear(vector);
115 safe_free(vector);
116}
117
118////////////////////
119// Iterator logic //
120////////////////////
121
122VectorIterator* vector_get_iterator(Vector* vector)
123{
124 VectorIterator* vectorI = (VectorIterator*) safe_malloc(sizeof (VectorIterator));
125 vectorI->vector = vector;
126 vectorI_reset_begin(vectorI);
127 return vectorI;
128}
129
130void vectorI_reset_begin(VectorIterator* vectorI)
131{
132 vectorI->current = vectorI->vector->datas;
133}
134
135void vectorI_reset_end(VectorIterator* vectorI)
136{
137 vectorI->current = vectorI->vector->datas + vectorI->vector->size - 1;
138}
139
140Bool vectorI_has_data(VectorIterator* vectorI)
141{
142 return (vectorI->current >= vectorI->vector->datas &&
143 vectorI->current < vectorI->vector->datas + vectorI->vector->size);
144}
145
146void* _vectorI_get(VectorIterator* vectorI)
147{
148 return *(vectorI->current);
149}
150
151void _vectorI_set(VectorIterator* vectorI, void* data)
152{
153 *(vectorI->current) = data;
154}
155
156void vectorI_move_next(VectorIterator* vectorI)
157{
158 vectorI->current++;
159}
160
161void vectorI_move_prev(VectorIterator* vectorI)
162{
163 vectorI->current--;
164}
165
166void vectorI_destroy(VectorIterator* vectorI)
167{
168 safe_free(vectorI);
169}