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() { int test_vec_remove() {
vec test_vec; vec test_vec;
char* test_string = "test"; char* test_string = "test";
char* second_test_string = "test2";
int return_code; int return_code;
vec_init(&test_vec); 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, test_string);
vec_add(&test_vec, second_test_string);
vec_remove(&test_vec, 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); vec_free(&test_vec);
return return_code; return return_code;

View File

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