Fixed bugs in vector, passes the tests now.
This commit is contained in:
parent
913fa38663
commit
48dcdeeeb9
|
@ -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. */
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -25,42 +25,44 @@
|
|||
#define VECTOR_H
|
||||
/* Constants. */
|
||||
/* Growth constant, for the power. */
|
||||
#define GROWTH_CONSTANT_VECTOR_C 2
|
||||
#define GROWTH_CONSTANT_VECTOR_C 2
|
||||
/* Inital power. */
|
||||
#define INITIAL_POWER_VECTOR_C 3
|
||||
#define INITIAL_POWER_VECTOR_C 3
|
||||
/* Function errror/return code on success. */
|
||||
#define SUCCESS_CODE_VECTOR_C 1
|
||||
#define SUCCESS_CODE_VECTOR_C 1
|
||||
/* Function errror/return code on failure. */
|
||||
#define FAILURE_CODE_VECTOR_C 0
|
||||
#define FAILURE_CODE_VECTOR_C 0
|
||||
/* Function errror/return code on failure to find index. */
|
||||
#define INDEX_NOT_FOUND_CODE_VECTOR_C -1
|
||||
#define INDEX_NOT_FOUND_CODE_VECTOR_C -1
|
||||
/* Function errror/return code on invalid input. */
|
||||
#define INVALID_INPUT_CODE_VECTOR_C -2
|
||||
#define INVALID_INPUT_CODE_VECTOR_C -2
|
||||
/* Function errror/return code on invalid input. */
|
||||
#define INVALID_FREE_FUNCTION_POINTER -3
|
||||
#define INVALID_FREE_FUNCTION_POINTER -3
|
||||
/* Error code returned when input fails sanity check. */
|
||||
#define CODE_FREE_SUCCESS_MALFORMED_VECTOR_C -1
|
||||
#define CODE_FREE_SUCCESS_MALFORMED_VECTOR_C -1
|
||||
/* Check code for growing the vector. */
|
||||
#define CODE_GROW_VECTOR_C 1
|
||||
#define CODE_GROW_VECTOR_C 1
|
||||
/* Check code for keeping the vector as is. */
|
||||
#define CODE_KEEP_VECTOR_C 0
|
||||
#define CODE_KEEP_VECTOR_C 0
|
||||
/* Check code for shrinking the vector. */
|
||||
#define CODE_SHRINK_VECTOR_C -1
|
||||
#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
|
||||
#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
|
||||
#define THRESHOLD_WHOLE_VECTOR_C 1.0
|
||||
/* The threshold used to grow the vector. */
|
||||
#define THRESHOLD_GROW_VECTOR_C 0.75
|
||||
#define THRESHOLD_GROW_VECTOR_C 0.75
|
||||
/* The threshold used to shrink the vector. */
|
||||
#define THRESHOLD_SHRINK_VECTOR_C 0.25
|
||||
#define THRESHOLD_SHRINK_VECTOR_C 0.25
|
||||
/* Define the initial index for the last element. */
|
||||
#define LAST_ELEMENT_EMPTY_INDEX_VECTOR_C 0
|
||||
#define LAST_ELEMENT_EMPTY_INDEX_VECTOR_C 0
|
||||
/* Boolean Logic. */
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
|
||||
|
||||
/* Useful typedef for void pointers to make it simpler to deal with. */
|
||||
|
|
Loading…
Reference in New Issue