Switch vector to use size_t for its array sizes and indices.
This commit is contained in:
parent
c97538265d
commit
1b57ebb027
|
@ -18,11 +18,11 @@ struct vec_s {
|
||||||
/**
|
/**
|
||||||
* The maximum size of the vector.
|
* The maximum size of the vector.
|
||||||
*/
|
*/
|
||||||
int capacity;
|
size_t capacity;
|
||||||
/**
|
/**
|
||||||
* The current number of items in the vector.
|
* The current number of items in the vector.
|
||||||
*/
|
*/
|
||||||
int size;
|
size_t size;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct vec_s vec;
|
typedef struct vec_s vec;
|
||||||
|
@ -87,7 +87,7 @@ int vec_foreach(vec* vec, void* data, compare_func compare, foreach_func foreach
|
||||||
* @param index the index to retreive a value from
|
* @param index the index to retreive a value from
|
||||||
* @return pointer to the value, or, if the index is out of bounds (or there is nothing there) NULL.
|
* @return pointer to the value, or, if the index is out of bounds (or there is nothing there) NULL.
|
||||||
*/
|
*/
|
||||||
void* vec_index(vec* vec, int index);
|
void* vec_index(vec* vec, size_t index);
|
||||||
/**
|
/**
|
||||||
* Clears the vector, removing all elements from it
|
* Clears the vector, removing all elements from it
|
||||||
* but not resizing it down.
|
* but not resizing it down.
|
||||||
|
|
12
src/vec.c
12
src/vec.c
|
@ -44,16 +44,16 @@ libds_result vec_add(vec* v, void* data) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
void vec_remove(vec* v, void* data) {
|
void vec_remove(vec* v, void* data) {
|
||||||
int index = 0;
|
size_t index = 0;
|
||||||
void** data_array = v->data;
|
void** data_array = v->data;
|
||||||
while (index < v->size && data_array[index]) {
|
while (index < v->size && data_array[index]) {
|
||||||
int search_ahead = 0;
|
size_t search_ahead = 0;
|
||||||
while(data_array[index + search_ahead] == data){
|
while(data_array[index + search_ahead] == data){
|
||||||
search_ahead++;
|
search_ahead++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(search_ahead){
|
if(search_ahead){
|
||||||
int remaining_elements = v->size - (index + search_ahead);
|
size_t remaining_elements = v->size - (index + search_ahead);
|
||||||
memmove(data_array + index, data_array + index + search_ahead, remaining_elements);
|
memmove(data_array + index, data_array + index + search_ahead, remaining_elements);
|
||||||
v->size -= search_ahead;
|
v->size -= search_ahead;
|
||||||
}
|
}
|
||||||
|
@ -62,7 +62,7 @@ void vec_remove(vec* v, void* data) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void* vec_find(vec* v, void* data, compare_func compare) {
|
void* vec_find(vec* v, void* data, compare_func compare) {
|
||||||
int index = 0;
|
size_t index = 0;
|
||||||
void* found = NULL;
|
void* found = NULL;
|
||||||
void** data_array = v->data;
|
void** data_array = v->data;
|
||||||
while (index < v->size && found == NULL) {
|
while (index < v->size && found == NULL) {
|
||||||
|
@ -75,7 +75,7 @@ void* vec_find(vec* v, void* data, compare_func compare) {
|
||||||
}
|
}
|
||||||
int vec_foreach(vec* v, void* data, compare_func compare, foreach_func foreach,
|
int vec_foreach(vec* v, void* data, compare_func compare, foreach_func foreach,
|
||||||
...) {
|
...) {
|
||||||
int index = 0;
|
size_t index = 0;
|
||||||
int return_code = 0;
|
int return_code = 0;
|
||||||
void** data_array = v->data;
|
void** data_array = v->data;
|
||||||
va_list args;
|
va_list args;
|
||||||
|
@ -90,7 +90,7 @@ int vec_foreach(vec* v, void* data, compare_func compare, foreach_func foreach,
|
||||||
return return_code;
|
return return_code;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* vec_index(vec* v, int index) {
|
void* vec_index(vec* v, size_t index) {
|
||||||
void* to_return = NULL;
|
void* to_return = NULL;
|
||||||
if (index < v->capacity && index >= 0) {
|
if (index < v->capacity && index >= 0) {
|
||||||
to_return = ((void**) v->data)[index];
|
to_return = ((void**) v->data)[index];
|
||||||
|
|
Loading…
Reference in New Issue
Block a user