Skip to content

Commit

Permalink
[feature] minor linkedlist updates
Browse files Browse the repository at this point in the history
  • Loading branch information
lvntky committed Aug 9, 2024
1 parent 325df4d commit 4b10140
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 19 deletions.
64 changes: 60 additions & 4 deletions include/ccontainer/cc_linkedlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extern "C" {

#include <stddef.h> // for size_t
#include <stdlib.h> // for malloc
#include "ccontainer_utils.h" // for printing stdout etc.
#include <string.h>

// =====================================================================
// Configuration Macros
Expand All @@ -43,23 +43,79 @@ extern "C" {
// Utility Macros
// =====================================================================

#define CC_LINKEDLIST_LOG(format, ...) \
do { \
char buffer[256]; \
snprintf(buffer, sizeof(buffer), format, ##__VA_ARGS__); \
fprintf(stderr, "[CC_LINKEDLIST_H ERROR] - %s\n", buffer); \
} while (0)

// =====================================================================
// Struct Definitions
// =====================================================================

typedef struct cc_linkedlist_node {
void *data;
cc_linkedlist_node_t *next;
} cc_linkedlist_node_t;

// =====================================================================
// Library Function Declarations
// =====================================================================

// =====================================================================
// Struct Function Declarations
// =====================================================================
/**
* @brief create single node for linked list
*
* @param data to push to the linked list
* @param data_size size of the data for allocating memory for it
* @return cc_linkedlist_node_t*
*/
cc_linkedlist_node_t *cc_linkedlist_create_node(void *data, size_t data_size);

/**
* @brief gets the current size of linked list
*
* @param head to point specified linked list
* @return size_t number of nodes
*/
size_t cc_linkedlist_size(cc_linkedlist_node_t *head);

// =====================================================================
// Function Definitions
// =====================================================================

#ifdef CC_LINKEDLIST_IMPLEMENTATION
cc_linkedlist_node_t *cc_linkedlist_create_node(void *data, size_t data_size)

{
cc_linkedlist_node_t *new_node =
(cc_linkedlist_node_t *)malloc(sizeof(cc_linkedlist_node_t));

new_node->data = malloc(data_size);
new_node->next = NULL;

memcpy(new_node->data, data, data_size);

if (new_node == NULL || new_node->data == NULL) {
CC_LINKEDLIST_LOG(
"An error occured while creating node. Terminating program with failing exit status.");
exit(EXIT_FAILURE);
}

return new_node;
}

//TODO: Can we implement more faste solution then O(n) ?
size_t cc_linkedlist_size(cc_linkedlist_node_t *head)
{
size_t counter = 0;

while (head != NULL) {
counter++;
}

return counter;
}

#endif // CC_LINKEDLIST_IMPLEMENTATION

Expand Down
30 changes: 15 additions & 15 deletions include/ccontainer/cc_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ extern "C" {
// Utility Macros
// =====================================================================

#define CCONTAINER_LOG(format, ...) \
do { \
char buffer[256]; \
snprintf(buffer, sizeof(buffer), format, ##__VA_ARGS__); \
fprintf(stderr, "[CCONTAINER_H ERROR] - %s\n", buffer); \
} while (0)
#define CC_VECTOR_LOG(format, ...) \
do { \
char buffer[256]; \
snprintf(buffer, sizeof(buffer), format, ##__VA_ARGS__); \
fprintf(stderr, "[CC_VECTOR_H ERROR] - %s\n", buffer); \
} while (0)

// =====================================================================
// Struct Definitions
Expand Down Expand Up @@ -199,7 +199,7 @@ void cc_vector_free(cc_vector_t *vector)
void *cc_vector_at(cc_vector_t *vector, size_t index)
{
if (index > vector->size) {
CCONTAINER_LOG(
CC_VECTOR_LOG(
"Index out of bounds. Terminating program with failing exit status.");
exit(EXIT_FAILURE);
}
Expand All @@ -209,7 +209,7 @@ void *cc_vector_at(cc_vector_t *vector, size_t index)
void *cc_vector_front(cc_vector_t *vector)
{
if (vector == NULL) {
CCONTAINER_LOG("The vector is uninitialized.");
CC_VECTOR_LOG("The vector is uninitialized.");
exit(EXIT_FAILURE);
}
return vector->data[0];
Expand All @@ -218,7 +218,7 @@ void *cc_vector_front(cc_vector_t *vector)
void *cc_vector_back(cc_vector_t *vector)
{
if (vector == NULL) {
CCONTAINER_LOG("The vector is uninitialized.");
CC_VECTOR_LOG("The vector is uninitialized.");
exit(EXIT_FAILURE);
}
return vector->data[vector->size - 1];
Expand All @@ -233,7 +233,7 @@ void cc_vector_push_back(cc_vector_t *vector, void *data)
void **new_data = (void **)realloc(
vector->data, sizeof(void *) * vector->capacity);
if (!new_data) {
CCONTAINER_LOG(
CC_VECTOR_LOG(
"Memory allocation has failed for cc_vector_push_back(). Terminating program with failing exit status.");
exit(EXIT_FAILURE);
}
Expand Down Expand Up @@ -271,7 +271,7 @@ void cc_vector_clear(cc_vector_t *vector)
void cc_vector_erase(cc_vector_t *vector, size_t pos)
{
if (vector == NULL || pos >= vector->size) {
CCONTAINER_LOG("Invalid position for cc_vector_erase.");
CC_VECTOR_LOG("Invalid position for cc_vector_erase.");
exit(EXIT_FAILURE);
}

Expand All @@ -285,15 +285,15 @@ void cc_vector_erase(cc_vector_t *vector, size_t pos)
void cc_vector_shrint_to_fix(cc_vector_t *vector)
{
if (vector == NULL) {
CCONTAINER_LOG("The vector is uninitialized.");
CC_VECTOR_LOG("The vector is uninitialized.");
exit(EXIT_FAILURE);
}

if (vector->size < vector->capacity) {
void **new_data = (void **)realloc(
vector->data, sizeof(void *) * vector->size);
if (!new_data) {
CCONTAINER_LOG(
CC_VECTOR_LOG(
"Memory allocation has failed for cc_vector_shrink_to_fit(). Terminating program with failing exit status.");
exit(EXIT_FAILURE);
}
Expand All @@ -305,15 +305,15 @@ void cc_vector_shrint_to_fix(cc_vector_t *vector)
void cc_vector_shrink_to_fit(cc_vector_t *vector)
{
if (vector == NULL) {
CCONTAINER_LOG("The vector is uninitialized.");
CC_VECTOR_LOG("The vector is uninitialized.");
exit(EXIT_FAILURE);
}

if (vector->size < vector->capacity) {
void **new_data = (void **)realloc(
vector->data, sizeof(void *) * vector->size);
if (!new_data) {
CCONTAINER_LOG(
CC_VECTOR_LOG(
"Memory allocation has failed for cc_vector_shrink_to_fit(). Terminating program with failing exit status.");
exit(EXIT_FAILURE);
}
Expand Down

0 comments on commit 4b10140

Please sign in to comment.