initial commit
[cgds.git] / src / Vector.c
... / ...
CommitLineData
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
85void* _vector_get(Vector* vector, UInt index)
86{
87 return vector->datas[index];
88}
89
90void _vector_set(Vector* vector, UInt index, void* data)
91{
92 memcpy(vector->datas[index], data, vector->dataSize);
93}
94
95void 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
103void vector_destroy(Vector* vector)
104{
105 vector_clear(vector);
106 safe_free(vector);
107}
108
109////////////////////
110// Iterator logic //
111////////////////////
112
113VectorIterator* vector_get_iterator(Vector* vector)
114{
115 VectorIterator* vectorI = (VectorIterator*) safe_malloc(sizeof (VectorIterator));
116 vectorI->vector = vector;
117 vectorI_reset_begin(vectorI);
118 return vectorI;
119}
120
121void vectorI_reset_begin(VectorIterator* vectorI)
122{
123 vectorI->current = vectorI->vector->datas;
124}
125
126void vectorI_reset_end(VectorIterator* vectorI)
127{
128 vectorI->current = vectorI->vector->datas + vectorI->vector->size - 1;
129}
130
131Bool vectorI_has_data(VectorIterator* vectorI)
132{
133 return (vectorI->current >= vectorI->vector->datas &&
134 vectorI->current < vectorI->vector->datas + vectorI->vector->size);
135}
136
137void* _vectorI_get(VectorIterator* vectorI)
138{
139 return *(vectorI->current);
140}
141
142void _vectorI_set(VectorIterator* vectorI, void* data)
143{
144 *(vectorI->current) = data;
145}
146
147void vectorI_move_next(VectorIterator* vectorI)
148{
149 vectorI->current++;
150}
151
152void vectorI_move_prev(VectorIterator* vectorI)
153{
154 vectorI->current--;
155}
156
157void vectorI_destroy(VectorIterator* vectorI)
158{
159 safe_free(vectorI);
160}