Skip to content

Commit

Permalink
Merge pull request #11 from dreamer-coding/main
Browse files Browse the repository at this point in the history
Template classes and features
  • Loading branch information
dreamer-coding authored Nov 20, 2024
2 parents 1b9c679 + c7fbcee commit c40c1d9
Show file tree
Hide file tree
Showing 38 changed files with 5,379 additions and 385 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/meson_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ on:
paths:
- "**.c"
- "**.h"
- "**.cpp"
- "**.hpp"
- "**.py"
- "**.build"
- "**.options"
pull_request:
paths:
- "**.c"
- "**.h"
- "**.cpp"
- "**.hpp"
- "**.py"
- "**.build"
- "**.options"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Before using ToFu, ensure you have the following installed:
# ======================
[wrap-git]
url = https://github.com/fossillogic/fossil-tofu.git
revision = v0.1.5
revision = v0.1.6

[provide]
fossil-tofu = fossil_tofu_dep
Expand Down
33 changes: 33 additions & 0 deletions code/logic/doublylist.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,39 @@ fossil_dlist_t* fossil_dlist_create_container(char* type) {
return dlist;
}

fossil_dlist_t* fossil_dlist_create_default(void) {
return fossil_dlist_create_container("any");
}

fossil_dlist_t* fossil_dlist_create_copy(const fossil_dlist_t* other) {
fossil_dlist_t* dlist = (fossil_dlist_t*)fossil_tofu_alloc(sizeof(fossil_dlist_t));
if (dlist == NULL) {
return NULL;
}
dlist->type = fossil_tofu_strdup(other->type);
dlist->head = NULL;
dlist->tail = NULL;
fossil_dlist_node_t* current = other->head;
while (current != NULL) {
fossil_dlist_insert(dlist, fossil_tofu_get_value(&current->data));
current = current->next;
}
return dlist;
}

fossil_dlist_t* fossil_dlist_create_move(fossil_dlist_t* other) {
fossil_dlist_t* dlist = (fossil_dlist_t*)fossil_tofu_alloc(sizeof(fossil_dlist_t));
if (dlist == NULL) {
return NULL;
}
dlist->type = other->type;
dlist->head = other->head;
dlist->tail = other->tail;
other->head = NULL;
other->tail = NULL;
return dlist;
}

void fossil_dlist_destroy(fossil_dlist_t* dlist) {
fossil_dlist_node_t* current = dlist->head;
while (current != NULL) {
Expand Down
79 changes: 79 additions & 0 deletions code/logic/dqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,39 @@ fossil_dqueue_t* fossil_dqueue_create_container(char* type) {
return dqueue;
}

fossil_dqueue_t* fossil_dqueue_create_default(void) {
return fossil_dqueue_create_container("any");
}

fossil_dqueue_t* fossil_dqueue_create_copy(const fossil_dqueue_t* other) {
fossil_dqueue_t* dqueue = (fossil_dqueue_t*)fossil_tofu_alloc(sizeof(fossil_dqueue_t));
if (dqueue == NULL) {
return NULL;
}
dqueue->type = fossil_tofu_strdup(other->type);
dqueue->front = NULL;
dqueue->rear = NULL;
fossil_dqueue_node_t* current = other->front;
while (current != NULL) {
fossil_dqueue_insert(dqueue, fossil_tofu_get_value(&current->data));
current = current->next;
}
return dqueue;
}

fossil_dqueue_t* fossil_dqueue_create_move(fossil_dqueue_t* other) {
fossil_dqueue_t* dqueue = (fossil_dqueue_t*)fossil_tofu_alloc(sizeof(fossil_dqueue_t));
if (dqueue == NULL) {
return NULL;
}
dqueue->type = other->type;
dqueue->front = other->front;
dqueue->rear = other->rear;
other->front = NULL;
other->rear = NULL;
return dqueue;
}

void fossil_dqueue_destroy(fossil_dqueue_t* dqueue) {
fossil_dqueue_node_t* current = dqueue->front;
while (current != NULL) {
Expand Down Expand Up @@ -111,3 +144,49 @@ bool fossil_dqueue_is_empty(const fossil_dqueue_t* dqueue) {
bool fossil_dqueue_is_cnullptr(const fossil_dqueue_t* dqueue) {
return dqueue == NULL;
}

// *****************************************************************************
// Getter and setter functions
// *****************************************************************************

char *fossil_dqueue_get(const fossil_dqueue_t* dqueue, size_t index) {
size_t i = 0;
fossil_dqueue_node_t* current = dqueue->front;
while (current != NULL) {
if (i == index) {
return fossil_tofu_get_value(&current->data);
}
i++;
current = current->next;
}
return NULL;
}

char *fossil_dqueue_get_front(const fossil_dqueue_t* dqueue) {
return fossil_tofu_get_value(&dqueue->front->data);
}

char *fossil_dqueue_get_back(const fossil_dqueue_t* dqueue) {
return fossil_tofu_get_value(&dqueue->rear->data);
}

void fossil_dqueue_set(fossil_dqueue_t* dqueue, size_t index, char *element) {
size_t i = 0;
fossil_dqueue_node_t* current = dqueue->front;
while (current != NULL) {
if (i == index) {
fossil_tofu_set_value(&current->data, element);
return;
}
i++;
current = current->next;
}
}

void fossil_dqueue_set_front(fossil_dqueue_t* dqueue, char *element) {
fossil_tofu_set_value(&dqueue->front->data, element);
}

void fossil_dqueue_set_back(fossil_dqueue_t* dqueue, char *element) {
fossil_tofu_set_value(&dqueue->rear->data, element);
}
30 changes: 30 additions & 0 deletions code/logic/forwardlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,36 @@ fossil_flist_t* fossil_flist_create_container(char* type) {
return flist;
}

fossil_flist_t* fossil_flist_create_default(void) {
return fossil_flist_create_container("any");
}

fossil_flist_t* fossil_flist_create_copy(const fossil_flist_t* other) {
fossil_flist_t* flist = (fossil_flist_t*)fossil_tofu_alloc(sizeof(fossil_flist_t));
if (flist == NULL) {
return NULL;
}
flist->type = fossil_tofu_strdup(other->type);
flist->head = NULL;
fossil_flist_node_t* current = other->head;
while (current != NULL) {
fossil_flist_insert(flist, fossil_tofu_get_value(&current->data));
current = current->next;
}
return flist;
}

fossil_flist_t* fossil_flist_create_move(fossil_flist_t* other) {
fossil_flist_t* flist = (fossil_flist_t*)fossil_tofu_alloc(sizeof(fossil_flist_t));
if (flist == NULL) {
return NULL;
}
flist->type = other->type;
flist->head = other->head;
other->head = NULL;
return flist;
}

void fossil_flist_destroy(fossil_flist_t* flist) {
fossil_flist_node_t* current = flist->head;
while (current != NULL) {
Expand Down
177 changes: 177 additions & 0 deletions code/logic/fossil/tofu/doublylist.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,35 @@ typedef struct fossil_dlist_t {
*/
fossil_dlist_t* fossil_dlist_create_container(char* type);

/**
* Create a new doubly linked list with default values.
*
* Time complexity: O(1)
*
* @return The created doubly linked list.
*/
fossil_dlist_t* fossil_dlist_create_default(void);

/**
* Create a new doubly linked list by copying an existing list.
*
* Time complexity: O(n)
*
* @param other The doubly linked list to copy.
* @return The created doubly linked list.
*/
fossil_dlist_t* fossil_dlist_create_copy(const fossil_dlist_t* other);

/**
* Create a new doubly linked list by moving an existing list.
*
* Time complexity: O(1)
*
* @param other The doubly linked list to move.
* @return The created doubly linked list.
*/
fossil_dlist_t* fossil_dlist_create_move(fossil_dlist_t* other);

/**
* Erase the contents of the doubly linked list and fossil_tofu_free allocated memory.
*
Expand Down Expand Up @@ -212,6 +241,154 @@ void fossil_dlist_set_back(fossil_dlist_t* dlist, char *element);

#ifdef __cplusplus
}
#include <stdexcept>

namespace fossil {

/**
* @brief A doubly linked list.
*
* @tparam T The type of data the doubly linked list will store.
*/
template <typename T>
class DList {
public:
/**
* @brief Default constructor.
*/
DList() : head(nullptr), tail(nullptr) {}

/**
* @brief Destructor.
*/
~DList() {
clear();
}

/**
* @brief Insert data into the doubly linked list.
*
* @param data The data to insert.
* @note Time complexity: O(1)
*/
void insert(const T& data) {
Node* newNode = new Node(data);
if (!head) {
head = tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}

/**
* @brief Remove data from the doubly linked list.
* @note Time complexity: O(1)
*/
void remove() {
if (!tail) {
throw std::underflow_error("List is empty");
}
Node* toDelete = tail;
tail = tail->prev;
if (tail) {
tail->next = nullptr;
} else {
head = nullptr;
}
delete toDelete;
}

/**
* @brief Reverse the doubly linked list in the forward direction.
* @note Time complexity: O(n)
*/
T get(size_t index) const {
Node* current = head;
for (size_t i = 0; i < index; ++i) {
if (!current) {
throw std::out_of_range("Index out of range");
}
current = current->next;
}
if (!current) {
throw std::out_of_range("Index out of range");
}
return current->data;
}

/**
* @brief Get the first element in the doubly linked list.
* @note Time complexity: O(1)
*/
T get_front() const {
if (!head) {
throw std::underflow_error("List is empty");
}
return head->data;
}

/**
* @brief Get the last element in the doubly linked list.
* @note Time complexity: O(1)
*/
T get_back() const {
if (!tail) {
throw std::underflow_error("List is empty");
}
return tail->data;
}

/**
* @brief Set the element at the specified index in the doubly linked list.
* @note Time complexity: O(n)
*/
void clear() {
while (head) {
Node* toDelete = head;
head = head->next;
delete toDelete;
}
tail = nullptr;
}

/**
* @brief Get the size of the doubly linked list.
* @note Time complexity: O(n)
*/
size_t size() const {
size_t count = 0;
Node* current = head;
while (current) {
++count;
current = current->next;
}
return count;
}

/**
* @brief Check if the doubly linked list is not empty.
* @note Time complexity: O(1)
*/
bool is_empty() const {
return head == nullptr;
}

private:
struct Node {
T data;
Node* prev;
Node* next;
Node(const T& data) : data(data), prev(nullptr), next(nullptr) {}
};

Node* head;
Node* tail;
};

} // namespace fossil

#endif

#endif /* FOSSIL_TOFU_FRAMEWORK_H */
Loading

0 comments on commit c40c1d9

Please sign in to comment.