Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix and imporvments #6

Merged
merged 12 commits into from
Oct 20, 2024
20 changes: 10 additions & 10 deletions code/logic/doublylist.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ void fossil_dlist_erase(fossil_dlist_t* dlist) {
}

int32_t fossil_dlist_insert(fossil_dlist_t* dlist, fossil_tofu_t data) {
if (!dlist) return -1;
if (!dlist) return FOSSIL_TOFU_FAILURE;

fossil_dlist_node_t* new_node = (fossil_dlist_node_t*)fossil_tofu_alloc(sizeof(fossil_dlist_node_t));
if (!new_node) {
return -1; // Allocation failed
return FOSSIL_TOFU_FAILURE; // Allocation failed
}

new_node->data = data; // Consider deep copying data if necessary
Expand All @@ -59,16 +59,16 @@ int32_t fossil_dlist_insert(fossil_dlist_t* dlist, fossil_tofu_t data) {
dlist->tail = new_node;
}

return 0; // Success
return FOSSIL_TOFU_SUCCESS; // Success
}

int32_t fossil_dlist_remove(fossil_dlist_t* dlist, fossil_tofu_t* data) {
if (fossil_dlist_is_empty(dlist)) {
return -1; // Empty list
return FOSSIL_TOFU_FAILURE; // Empty list
}

fossil_dlist_node_t* node_to_remove = dlist->tail;
if (!node_to_remove) return -1;
if (!node_to_remove) return FOSSIL_TOFU_FAILURE;

if (node_to_remove == dlist->head) {
dlist->head = NULL;
Expand All @@ -81,18 +81,18 @@ int32_t fossil_dlist_remove(fossil_dlist_t* dlist, fossil_tofu_t* data) {
*data = node_to_remove->data; // Consider deep copy if necessary
fossil_tofu_free(node_to_remove);

return 0; // Success
return FOSSIL_TOFU_SUCCESS; // Success
}

int32_t fossil_dlist_search(const fossil_dlist_t* dlist, fossil_tofu_t data) {
fossil_dlist_node_t* current = dlist->head;
while (current) {
if (fossil_tofu_equals(current->data, data)) {
return 0; // Found
return FOSSIL_TOFU_SUCCESS; // Found
}
current = current->next;
}
return -1; // Not found
return FOSSIL_TOFU_FAILURE; // Not found
}

void fossil_dlist_reverse_forward(fossil_dlist_t* dlist) {
Expand Down Expand Up @@ -168,11 +168,11 @@ int32_t fossil_dlist_setter(fossil_dlist_t* dlist, fossil_tofu_t data) {
if (fossil_tofu_equals(current->data, data)) {
// Ensure to free old data if it was dynamically allocated
current->data = data;
return 0; // Success
return FOSSIL_TOFU_SUCCESS; // Success
}
current = current->next;
}
return -1; // Not found
return FOSSIL_TOFU_FAILURE; // Not found
}

bool fossil_dlist_not_empty(const fossil_dlist_t* dlist) {
Expand Down
16 changes: 8 additions & 8 deletions code/logic/dqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void fossil_dqueue_erase(fossil_dqueue_t* dqueue) {
int32_t fossil_dqueue_insert(fossil_dqueue_t* dqueue, fossil_tofu_t data) {
fossil_dqueue_node_t* new_node = (fossil_dqueue_node_t*)fossil_tofu_alloc(sizeof(fossil_dqueue_node_t));
if (!new_node) {
return -1; // Allocation failed
return FOSSIL_TOFU_FAILURE; // Allocation failed
}

new_node->data = data;
Expand All @@ -62,12 +62,12 @@ int32_t fossil_dqueue_insert(fossil_dqueue_t* dqueue, fossil_tofu_t data) {
dqueue->rear = new_node;
}

return 0; // Success
return FOSSIL_TOFU_SUCCESS; // Success
}

int32_t fossil_dqueue_remove(fossil_dqueue_t* dqueue, fossil_tofu_t* data) {
if (fossil_dqueue_is_empty(dqueue)) {
return -1; // Empty queue
return FOSSIL_TOFU_FAILURE; // Empty queue
}

fossil_dqueue_node_t* node_to_remove = dqueue->front;
Expand All @@ -83,18 +83,18 @@ int32_t fossil_dqueue_remove(fossil_dqueue_t* dqueue, fossil_tofu_t* data) {
*data = node_to_remove->data;
fossil_tofu_free(node_to_remove);

return 0; // Success
return FOSSIL_TOFU_SUCCESS; // Success
}

int32_t fossil_dqueue_search(const fossil_dqueue_t* dqueue, fossil_tofu_t data) {
fossil_dqueue_node_t* current = dqueue->front;
while (current) {
if (fossil_tofu_equals(current->data, data)) {
return 0; // Found
return FOSSIL_TOFU_SUCCESS; // Found
}
current = current->next;
}
return -1; // Not found
return FOSSIL_TOFU_FAILURE; // Not found
}

size_t fossil_dqueue_size(const fossil_dqueue_t* dqueue) {
Expand Down Expand Up @@ -124,11 +124,11 @@ int32_t fossil_dqueue_setter(fossil_dqueue_t* dqueue, fossil_tofu_t data) {
if (fossil_tofu_equals(current->data, data)) {
// Assuming `data` should replace current->data
current->data = data; // Update data
return 0; // Success
return FOSSIL_TOFU_SUCCESS; // Success
}
current = current->next;
}
return -1; // Not found
return FOSSIL_TOFU_FAILURE; // Not found
}

bool fossil_dqueue_not_empty(const fossil_dqueue_t* dqueue) {
Expand Down
16 changes: 8 additions & 8 deletions code/logic/forwardlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,38 +41,38 @@ void fossil_flist_erase(fossil_flist_t* flist) {
int32_t fossil_flist_insert(fossil_flist_t* flist, fossil_tofu_t data) {
fossil_flist_node_t* new_node = (fossil_flist_node_t*)fossil_tofu_alloc(sizeof(fossil_flist_node_t));
if (!new_node) {
return -1; // Allocation failed
return FOSSIL_TOFU_FAILURE; // Allocation failed
}

new_node->data = data;
new_node->next = flist->head;
flist->head = new_node;

return 0; // Success
return FOSSIL_TOFU_SUCCESS; // Success
}

int32_t fossil_flist_remove(fossil_flist_t* flist, fossil_tofu_t* data) {
if (fossil_flist_is_cnullptr(flist)) {
return -1; // Empty list
return FOSSIL_TOFU_FAILURE; // Empty list
}

fossil_flist_node_t* node_to_remove = flist->head;
*data = node_to_remove->data;
flist->head = node_to_remove->next;
fossil_tofu_free(node_to_remove);

return 0; // Success
return FOSSIL_TOFU_SUCCESS; // Success
}

int32_t fossil_flist_search(const fossil_flist_t* flist, fossil_tofu_t data) {
fossil_flist_node_t* current = flist->head;
while (current) {
if (fossil_tofu_equals(current->data, data)) {
return 0; // Found
return FOSSIL_TOFU_SUCCESS; // Found
}
current = current->next;
}
return -1; // Not found
return FOSSIL_TOFU_FAILURE; // Not found
}

void fossil_flist_reverse_forward(fossil_flist_t* flist) {
Expand Down Expand Up @@ -120,11 +120,11 @@ int32_t fossil_flist_setter(fossil_flist_t* flist, fossil_tofu_t data) {
while (current) {
if (fossil_tofu_equals(current->data, data)) {
current->data = data; // Update data
return 0; // Success
return FOSSIL_TOFU_SUCCESS; // Success
}
current = current->next;
}
return -1; // Not found
return FOSSIL_TOFU_FAILURE; // Not found
}

bool fossil_flist_not_empty(const fossil_flist_t* flist) {
Expand Down
10 changes: 8 additions & 2 deletions code/logic/fossil/tofu/tofu.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ extern "C"
{
#endif

// Consistent return values for functions in the "tofu" data structure.
enum {
FOSSIL_TOFU_SUCCESS = 0,
FOSSIL_TOFU_FAILURE = -1
};

// Enumerated types for representing various data types in the "tofu" data structure.
typedef enum {
FOSSIL_TOFU_TYPE_GHOST, // Ghost type for unknown type.
Expand All @@ -76,8 +82,8 @@ typedef enum {
FOSSIL_TOFU_TYPE_BCHAR,
FOSSIL_TOFU_TYPE_CCHAR,
FOSSIL_TOFU_TYPE_WCHAR,
FOSSIL_TOFU_TYPE_SIZE,
FOSSIL_TOFU_TYPE_BOOL
FOSSIL_TOFU_TYPE_BOOL,
FOSSIL_TOFU_TYPE_SIZE
} fossil_tofu_type_t;

// Union for holding different types of values
Expand Down
18 changes: 9 additions & 9 deletions code/logic/pqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void fossil_pqueue_erase(fossil_pqueue_t* pqueue) {
int32_t fossil_pqueue_insert(fossil_pqueue_t* pqueue, fossil_tofu_t data, int32_t priority) {
fossil_pqueue_node_t* new_node = (fossil_pqueue_node_t*)fossil_tofu_alloc(sizeof(fossil_pqueue_node_t));
if (!new_node) {
return -1; // Allocation failed
return FOSSIL_TOFU_FAILURE; // Allocation failed
}

new_node->data = data;
Expand All @@ -60,12 +60,12 @@ int32_t fossil_pqueue_insert(fossil_pqueue_t* pqueue, fossil_tofu_t data, int32_
current->next = new_node;
}

return 0; // Success
return FOSSIL_TOFU_SUCCESS; // Success
}

int32_t fossil_pqueue_remove(fossil_pqueue_t* pqueue, fossil_tofu_t* data, int32_t priority) {
if (fossil_pqueue_is_empty(pqueue)) {
return -1; // Empty queue
return FOSSIL_TOFU_FAILURE; // Empty queue
}

fossil_pqueue_node_t* current = pqueue->front;
Expand All @@ -77,7 +77,7 @@ int32_t fossil_pqueue_remove(fossil_pqueue_t* pqueue, fossil_tofu_t* data, int32
}

if (!current) {
return -1; // Not found
return FOSSIL_TOFU_FAILURE; // Not found
}

if (prev) {
Expand All @@ -89,18 +89,18 @@ int32_t fossil_pqueue_remove(fossil_pqueue_t* pqueue, fossil_tofu_t* data, int32
*data = current->data;
fossil_tofu_free(current);

return 0; // Success
return FOSSIL_TOFU_SUCCESS; // Success
}

int32_t fossil_pqueue_search(const fossil_pqueue_t* pqueue, fossil_tofu_t data, int32_t priority) {
fossil_pqueue_node_t* current = pqueue->front;
while (current) {
if (current->priority == priority && fossil_tofu_equals(current->data, data)) {
return 0; // Found
return FOSSIL_TOFU_SUCCESS; // Found
}
current = current->next;
}
return -1; // Not found
return FOSSIL_TOFU_FAILURE; // Not found
}

size_t fossil_pqueue_size(const fossil_pqueue_t* pqueue) {
Expand Down Expand Up @@ -129,11 +129,11 @@ int32_t fossil_pqueue_setter(fossil_pqueue_t* pqueue, fossil_tofu_t data, int32_
while (current) {
if (current->priority == priority && fossil_tofu_equals(current->data, data)) {
current->data = data; // Update data
return 0; // Success
return FOSSIL_TOFU_SUCCESS; // Success
}
current = current->next;
}
return -1; // Not found
return FOSSIL_TOFU_FAILURE; // Not found
}

bool fossil_pqueue_not_empty(const fossil_pqueue_t* pqueue) {
Expand Down
16 changes: 8 additions & 8 deletions code/logic/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void fossil_queue_erase(fossil_queue_t* queue) {
int32_t fossil_queue_insert(fossil_queue_t* queue, fossil_tofu_t data) {
fossil_queue_node_t* new_node = (fossil_queue_node_t*)fossil_tofu_alloc(sizeof(fossil_queue_node_t));
if (!new_node) {
return -1; // Allocation failed
return FOSSIL_TOFU_FAILURE; // Allocation failed
}

new_node->data = data;
Expand All @@ -57,12 +57,12 @@ int32_t fossil_queue_insert(fossil_queue_t* queue, fossil_tofu_t data) {
queue->rear = new_node;
}

return 0; // Success
return FOSSIL_TOFU_SUCCESS; // Success
}

int32_t fossil_queue_remove(fossil_queue_t* queue, fossil_tofu_t* data) {
if (fossil_queue_is_empty(queue)) {
return -1; // Empty queue
return FOSSIL_TOFU_FAILURE; // Empty queue
}

fossil_queue_node_t* node_to_remove = queue->front;
Expand All @@ -74,18 +74,18 @@ int32_t fossil_queue_remove(fossil_queue_t* queue, fossil_tofu_t* data) {
queue->rear = NULL;
}

return 0; // Success
return FOSSIL_TOFU_SUCCESS; // Success
}

int32_t fossil_queue_search(const fossil_queue_t* queue, fossil_tofu_t data) {
fossil_queue_node_t* current = queue->front;
while (current) {
if (fossil_tofu_equals(current->data, data)) {
return 0; // Found
return FOSSIL_TOFU_SUCCESS; // Found
}
current = current->next;
}
return -1; // Not found
return FOSSIL_TOFU_FAILURE; // Not found
}

size_t fossil_queue_size(const fossil_queue_t* queue) {
Expand Down Expand Up @@ -114,11 +114,11 @@ int32_t fossil_queue_setter(fossil_queue_t* queue, fossil_tofu_t data) {
while (current) {
if (fossil_tofu_equals(current->data, data)) {
current->data = data; // Update data
return 0; // Success
return FOSSIL_TOFU_SUCCESS; // Success
}
current = current->next;
}
return -1; // Not found
return FOSSIL_TOFU_FAILURE; // Not found
}


Expand Down
Loading