c031b2c2f556380f129b67e044f30983040dcc5c
5 #include "cgds/Vector.h"
11 void _vector_init(Vector
* vector
, size_t dataSize
)
14 vector
->dataSize
= dataSize
;
19 Vector
* _vector_new(size_t dataSize
)
21 Vector
* vector
= (Vector
*) safe_malloc(sizeof (Vector
));
22 _vector_init(vector
, dataSize
);
26 Vector
* vector_copy(Vector
* vector
)
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
);
36 bool vector_empty(Vector
* vector
)
38 return (vector
->size
== 0);
41 UInt
vector_size(Vector
* vector
)
46 void _vector_realloc(Vector
* vector
, UInt newCapacity
)
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
;
55 void _vector_push(Vector
* vector
, void* data
)
57 if (vector
->size
>= vector
->capacity
)
59 UInt increasedCapacity
= vector
->capacity
> 0 ? 2 * vector
->capacity
: 1;
60 _vector_realloc(vector
, increasedCapacity
);
63 vector
->datas
+ vector
->size
* vector
->dataSize
,
69 void vector_pop(Vector
* vector
)
72 if (vector_size(vector
) <= (vector
->capacity
>> 1))
73 _vector_realloc(vector
, vector
->capacity
>> 1);
76 void* _vector_get(Vector
* vector
, UInt index
)
78 return vector
->datas
+ index
* vector
->dataSize
;
81 void _vector_set(Vector
* vector
, UInt index
, void* data
)
83 memcpy(vector
->datas
+ index
* vector
->dataSize
, data
, vector
->dataSize
);
86 void vector_clear(Vector
* vector
)
88 safe_free(vector
->datas
);
89 _vector_init(vector
, vector
->dataSize
);
92 void vector_destroy(Vector
* vector
)
102 VectorIterator
* vector_get_iterator(Vector
* vector
)
104 VectorIterator
* vectorI
=
105 (VectorIterator
*) safe_malloc(sizeof (VectorIterator
));
106 vectorI
->vector
= vector
;
107 vectorI_reset_begin(vectorI
);
111 void vectorI_reset_begin(VectorIterator
* vectorI
)
113 vectorI
->current
= vectorI
->vector
->datas
;
116 void vectorI_reset_end(VectorIterator
* vectorI
)
118 vectorI
->current
= vectorI
->vector
->datas
+
119 (vectorI
->vector
->size
- 1) * vectorI
->vector
->dataSize
;
122 bool vectorI_has_data(VectorIterator
* vectorI
)
125 vectorI
->current
>= vectorI
->vector
->datas
&&
127 vectorI
->vector
->datas
+
128 vectorI
->vector
->size
* vectorI
->vector
->dataSize
132 void* _vectorI_get(VectorIterator
* vectorI
)
134 return vectorI
->current
;
137 void _vectorI_set(VectorIterator
* vectorI
, void* data
)
139 memcpy(vectorI
->current
, data
, vectorI
->vector
->dataSize
);
142 void vectorI_move_next(VectorIterator
* vectorI
)
144 vectorI
->current
+= vectorI
->vector
->dataSize
;
147 void vectorI_move_prev(VectorIterator
* vectorI
)
149 vectorI
->current
-= vectorI
->vector
->dataSize
;
152 void vectorI_destroy(VectorIterator
* vectorI
)