Rewrite vectors to move elements on removal.

This commit is contained in:
Danila Fedorin 2017-06-09 22:58:26 -07:00
parent a04490cbdd
commit 20507f73bb
2 changed files with 27 additions and 39 deletions

View File

@ -38,12 +38,16 @@ int test_vec_add() {
int test_vec_remove() {
vec test_vec;
char* test_string = "test";
char* second_test_string = "test2";
int return_code;
vec_init(&test_vec);
vec_add(&test_vec, test_string);
vec_add(&test_vec, test_string);
vec_add(&test_vec, test_string);
vec_add(&test_vec, second_test_string);
vec_remove(&test_vec, test_string);
return_code = *((void**)test_vec.data) == NULL;
return_code = ((void**)test_vec.data)[0] == second_test_string;
vec_free(&test_vec);
return return_code;

View File

@ -8,7 +8,7 @@ libds_result vec_init(vec* v) {
v->size = 0;
v->capacity = LIBDS_VEC_CAPACITY;
v->data = calloc(LIBDS_VEC_CAPACITY, sizeof(void*));
v->data = malloc(LIBDS_VEC_CAPACITY * sizeof(void*));
if (v->data == NULL) {
result = LIBDS_MALLOC;
}
@ -27,7 +27,7 @@ void vec_free(vec* v) {
libds_result vec_add(vec* v, void* data) {
libds_result result = LIBDS_SUCCESS;
if (v->size >= v->capacity) {
void* new_mem = calloc(v->capacity *= 2, sizeof(void*));
void* new_mem = malloc((v->capacity *= 2) * sizeof(void*));
if (new_mem) {
memcpy(new_mem, v->data, sizeof(void*) * v->capacity / 2);
free(v->data);
@ -38,47 +38,36 @@ libds_result vec_add(vec* v, void* data) {
}
if (result == LIBDS_SUCCESS) {
int index = 0;
while (index < v->capacity) {
void** data_array = v->data;
if (data_array[index] == NULL) {
data_array[index] = data;
v->size++;
break;
}
index++;
}
((void**) v->data)[v->size++] = data;
}
return result;
}
void vec_remove(vec* v, void* data) {
int elements = v->size;
int index = 0;
void** data_array = v->data;
while (elements > 0) {
if (data_array[index]) {
elements--;
if (data_array[index] == data) {
data_array[index] = NULL;
v->size--;
}
while (index < v->size && data_array[index]) {
int search_ahead = 0;
while(data_array[index + search_ahead] == data){
search_ahead++;
}
if(search_ahead){
int remaining_elements = v->size - (index + search_ahead);
memmove(data_array + index, data_array + index + search_ahead, remaining_elements);
v->size -= search_ahead;
}
index++;
}
}
void* vec_find(vec* v, void* data, compare_func compare) {
int elements = v->size;
int index = 0;
void* found = NULL;
void** data_array = v->data;
while (elements > 0 && found == NULL) {
if (data_array[index]) {
if (compare(data, data_array[index])) {
found = data_array[index];
}
elements--;
while (index < v->size && found == NULL) {
if (data_array[index] && compare(data, data_array[index])) {
found = data_array[index];
}
index++;
}
@ -86,19 +75,15 @@ void* vec_find(vec* v, void* data, compare_func compare) {
}
int vec_foreach(vec* v, void* data, compare_func compare, foreach_func foreach,
...) {
int elements = v->size;
int index = 0;
int return_code = 0;
void** data_array = v->data;
va_list args;
while (elements > 0 && return_code == 0) {
if (data_array[index]) {
if (compare(data, data_array[index])) {
va_start(args, foreach);
return_code = foreach (data_array[index], args);
va_end(args);
}
elements--;
while (index < v->size && return_code == 0) {
if (data_array[index] && compare(data, data_array[index])) {
va_start(args, foreach);
return_code = foreach (data_array[index], args);
va_end(args);
}
index++;
}
@ -108,8 +93,7 @@ int vec_foreach(vec* v, void* data, compare_func compare, foreach_func foreach,
void* vec_index(vec* v, int index) {
void* to_return = NULL;
if (index < v->capacity && index >= 0) {
void** data_array = v->data;
to_return = data_array[index];
to_return = ((void**) v->data)[index];
}
return to_return;
}