Some fixes + improvements (Vector) + code reformatting
[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 * vector->dataSize);
32 memcpy(vectorCopy->datas, vector->datas, vector->size * vector->dataSize);
33 return vectorCopy;
34 }
35
36 bool vector_empty(Vector* vector)
37 {
38 return (vector->size == 0);
39 }
40
41 UInt vector_size(Vector* vector)
42 {
43 return vector->size;
44 }
45
46 void _vector_realloc(Vector* vector, UInt newCapacity)
47 {
48 void* rellocatedDatas = (void*) safe_malloc(newCapacity * vector->dataSize);
49 memcpy(rellocatedDatas, vector->datas, vector->size * vector->dataSize);
50 safe_free(vector->datas);
51 vector->datas = rellocatedDatas;
52 vector->capacity = newCapacity;
53 }
54
55 void _vector_push(Vector* vector, void* data)
56 {
57 if (vector->size >= vector->capacity)
58 {
59 UInt increasedCapacity = vector->capacity > 0 ? 2 * vector->capacity : 1;
60 _vector_realloc(vector, increasedCapacity);
61 }
62 memcpy(
63 vector->datas + vector->size * vector->dataSize,
64 data,
65 vector->dataSize);
66 vector->size++;
67 }
68
69 void vector_pop(Vector* vector)
70 {
71 vector->size--;
72 if (vector_size(vector) <= (vector->capacity >> 1))
73 _vector_realloc(vector, vector->capacity >> 1);
74 }
75
76 void* _vector_get(Vector* vector, UInt index)
77 {
78 return vector->datas + index * vector->dataSize;
79 }
80
81 void _vector_set(Vector* vector, UInt index, void* data)
82 {
83 memcpy(vector->datas + index * vector->dataSize, data, vector->dataSize);
84 }
85
86 void vector_clear(Vector* vector)
87 {
88 safe_free(vector->datas);
89 _vector_init(vector, vector->dataSize);
90 }
91
92 void vector_destroy(Vector* vector)
93 {
94 vector_clear(vector);
95 safe_free(vector);
96 }
97
98 ////////////////////
99 // Iterator logic //
100 ////////////////////
101
102 VectorIterator* vector_get_iterator(Vector* vector)
103 {
104 VectorIterator* vectorI =
105 (VectorIterator*) safe_malloc(sizeof (VectorIterator));
106 vectorI->vector = vector;
107 vectorI_reset_begin(vectorI);
108 return vectorI;
109 }
110
111 void vectorI_reset_begin(VectorIterator* vectorI)
112 {
113 vectorI->current = vectorI->vector->datas;
114 }
115
116 void vectorI_reset_end(VectorIterator* vectorI)
117 {
118 vectorI->current = vectorI->vector->datas +
119 (vectorI->vector->size - 1) * vectorI->vector->dataSize;
120 }
121
122 bool vectorI_has_data(VectorIterator* vectorI)
123 {
124 return (
125 vectorI->current >= vectorI->vector->datas &&
126 vectorI->current <
127 vectorI->vector->datas +
128 vectorI->vector->size * vectorI->vector->dataSize
129 );
130 }
131
132 void* _vectorI_get(VectorIterator* vectorI)
133 {
134 return vectorI->current;
135 }
136
137 void _vectorI_set(VectorIterator* vectorI, void* data)
138 {
139 memcpy(vectorI->current, data, vectorI->vector->dataSize);
140 }
141
142 void vectorI_move_next(VectorIterator* vectorI)
143 {
144 vectorI->current += vectorI->vector->dataSize;
145 }
146
147 void vectorI_move_prev(VectorIterator* vectorI)
148 {
149 vectorI->current -= vectorI->vector->dataSize;
150 }
151
152 void vectorI_destroy(VectorIterator* vectorI)
153 {
154 safe_free(vectorI);
155 }