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
* sizeof (void*));
32 for (UInt i
= 0; i
< vector
->size
; i
++)
34 vectorCopy
->datas
[i
] = safe_malloc(vector
->dataSize
);
35 memcpy(vectorCopy
->datas
[i
], vector
->datas
[i
], vector
->dataSize
);
40 Bool
vector_empty(Vector
* vector
)
42 return (vector
->size
== 0);
45 UInt
vector_size(Vector
* vector
)
50 void _vector_realloc(Vector
* vector
, UInt newCapacity
)
52 void** rellocatedDatas
= (void**) safe_malloc(newCapacity
* sizeof (void*));
53 for (UInt i
= 0; i
< vector
->size
; i
++)
55 rellocatedDatas
[i
] = (void*) safe_malloc(vector
->dataSize
);
56 memcpy(rellocatedDatas
[i
], vector
->datas
[i
], vector
->dataSize
);
57 safe_free(vector
->datas
[i
]);
59 safe_free(vector
->datas
);
60 vector
->datas
= rellocatedDatas
;
61 vector
->capacity
= newCapacity
;
64 void _vector_push(Vector
* vector
, void* data
)
66 void* pData
= safe_malloc(vector
->dataSize
);
67 memcpy(pData
, data
, vector
->dataSize
);
68 if (vector
->size
>= vector
->capacity
)
70 UInt increasedCapacity
= vector
->capacity
> 0 ? 2 * vector
->capacity
: 1;
71 _vector_realloc(vector
, increasedCapacity
);
73 vector
->datas
[vector
->size
] = pData
;
77 void vector_pop(Vector
* vector
)
79 safe_free(vector
->datas
[vector_size(vector
) - 1]);
81 if (vector_size(vector
) <= (vector
->capacity
>> 1))
82 _vector_realloc(vector
, vector
->capacity
>> 1);
85 void* _vector_get(Vector
* vector
, UInt index
)
87 return vector
->datas
[index
];
90 void _vector_set(Vector
* vector
, UInt index
, void* data
)
92 memcpy(vector
->datas
[index
], data
, vector
->dataSize
);
95 void vector_clear(Vector
* vector
)
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
);
103 void vector_destroy(Vector
* vector
)
105 vector_clear(vector
);
113 VectorIterator
* vector_get_iterator(Vector
* vector
)
115 VectorIterator
* vectorI
= (VectorIterator
*) safe_malloc(sizeof (VectorIterator
));
116 vectorI
->vector
= vector
;
117 vectorI_reset_begin(vectorI
);
121 void vectorI_reset_begin(VectorIterator
* vectorI
)
123 vectorI
->current
= vectorI
->vector
->datas
;
126 void vectorI_reset_end(VectorIterator
* vectorI
)
128 vectorI
->current
= vectorI
->vector
->datas
+ vectorI
->vector
->size
- 1;
131 Bool
vectorI_has_data(VectorIterator
* vectorI
)
133 return (vectorI
->current
>= vectorI
->vector
->datas
&&
134 vectorI
->current
< vectorI
->vector
->datas
+ vectorI
->vector
->size
);
137 void* _vectorI_get(VectorIterator
* vectorI
)
139 return *(vectorI
->current
);
142 void _vectorI_set(VectorIterator
* vectorI
, void* data
)
144 *(vectorI
->current
) = data
;
147 void vectorI_move_next(VectorIterator
* vectorI
)
152 void vectorI_move_prev(VectorIterator
* vectorI
)
157 void vectorI_destroy(VectorIterator
* vectorI
)