1
0
Fork 0

Fixed bugs in vector, passes the tests now.

This commit is contained in:
wael 2022-01-02 13:08:23 +02:00
parent 913fa38663
commit 48dcdeeeb9
No known key found for this signature in database
GPG Key ID: C0A5FBF4558963D4
3 changed files with 24 additions and 22 deletions

View File

@ -55,7 +55,7 @@ int test_vector() {
}
assert(vector->length == INITIAL_VECTOR_SIZE);
assert(vector->array != NULL);
assert(vector->last_element != VECTOR_LAST_ELEMENT_EMPTY_INDEX);
assert(vector->last_element == VECTOR_LAST_ELEMENT_EMPTY_INDEX);
assert(!vector->empty);
/* Attempt to free. */

View File

@ -187,7 +187,7 @@ static inline int delete_from_vector(vector_t* const vector, const size_t index,
/* Check if the vector should be shrunk. */
if (check_vector_shrink(vector, THRESHOLD_SHRINK_VECTOR_C))
/* TODO: implement. */
return (shrink_vector(vector)) ? SUCCESS_CODE_VECTOR_C : DELETE_SUCCESS_VECTOR_SHRINK_FAIL_VECTOR_C;
/* Done. */
return SUCCESS_CODE_VECTOR_C;
@ -339,7 +339,7 @@ static inline void compact_vector(vector_t* const vector) {
void_ptr *pointer = vector->array;
size_t i, j;
/* TODO: Check this logic and pointer arithmetic! */
/* This loop handles compacting the vector in O(n^2), sort of like bubble sort for NULL values. */
for (i = 0; i < vector->length; i++)
for (j = 0; j < vector->length - 1; j++)
if (*(pointer + (j * vector->element_size)) == (void_ptr) NULL) {

View File

@ -48,8 +48,10 @@
#define CODE_SHRINK_VECTOR_C -1
/* Error code for when write succeeds and vector grow fails. */
#define WRITE_SUCCESS_VECTOR_GROW_FAIL_VECTOR_C 3
/* Error code for when delete succeeds and vector shrink fails. */
#define DELETE_SUCCESS_VECTOR_SHRINK_FAIL_VECTOR_C 4
/* Error code to indicate allocation failure. */
#define ALLOC_FAILURE_VECTOR_C 4
#define ALLOC_FAILURE_VECTOR_C 5
/* Used to calculate the whole when checking size-changes. */
#define THRESHOLD_WHOLE_VECTOR_C 1.0
/* The threshold used to grow the vector. */