Started fixing the serialization functions in lists, still not done.
This commit is contained in:
parent
e1e301fa14
commit
d660572fe2
|
@ -191,7 +191,7 @@ int clear_queue_data(queue_t **queue, void (*free_func)()) {
|
||||||
|
|
||||||
/* This function serializes the queue. */
|
/* This function serializes the queue. */
|
||||||
/* Note that it'll return NULL on allocation failure or invalid input - in the former case it'll set the correct length in the specified pointer, in the second it will set -1. */
|
/* Note that it'll return NULL on allocation failure or invalid input - in the former case it'll set the correct length in the specified pointer, in the second it will set -1. */
|
||||||
void **serialize_queue(queue_t *queue, int *length) {
|
void *serialize_queue(queue_t *queue, int *length) {
|
||||||
/* Sanity check. */
|
/* Sanity check. */
|
||||||
if (!queue) {
|
if (!queue) {
|
||||||
*length = -1;
|
*length = -1;
|
||||||
|
|
|
@ -55,6 +55,6 @@ int clear_queue(queue_t **queue);
|
||||||
/* This function deletes all the nodes of the given queue. */
|
/* This function deletes all the nodes of the given queue. */
|
||||||
int clear_queue_data(queue_t **queue, void (*free_function)());
|
int clear_queue_data(queue_t **queue, void (*free_function)());
|
||||||
/* This function serializes the queue. */
|
/* This function serializes the queue. */
|
||||||
void **serialize_queue(queue_t *queue, int *length);
|
void *serialize_queue(queue_t *queue, int *length);
|
||||||
|
|
||||||
#endif /* QUEUE_H */
|
#endif /* QUEUE_H */
|
||||||
|
|
|
@ -14,6 +14,8 @@
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/* TODO: fixing serialization: change the code to take a user-supplied indexing function and array, and just send the current void pointer to it with the index in each iteration of the loop. */
|
||||||
|
|
||||||
/* This file contains the function implementations for single linked lists. */
|
/* This file contains the function implementations for single linked lists. */
|
||||||
/* Include guard + includes. */
|
/* Include guard + includes. */
|
||||||
#ifndef SINGLE_LINKED_LIST_C
|
#ifndef SINGLE_LINKED_LIST_C
|
||||||
|
@ -21,8 +23,8 @@
|
||||||
#include "single_linked_list.h"
|
#include "single_linked_list.h"
|
||||||
#endif /* SINGLE_LINKED_LIST_C */
|
#endif /* SINGLE_LINKED_LIST_C */
|
||||||
|
|
||||||
/* This function initializes a new linked list, returns NULL on failure. */
|
/* This function initializes a new linked list, returns NULL on failure - data_size is used for storing stored object size. */
|
||||||
linked_list_t* initialize_single_linked_list() {
|
linked_list_t* initialize_single_linked_list(size_t data_size) {
|
||||||
/* Variables. */
|
/* Variables. */
|
||||||
linked_list_t *result;
|
linked_list_t *result;
|
||||||
|
|
||||||
|
@ -36,6 +38,7 @@ linked_list_t* initialize_single_linked_list() {
|
||||||
/* Initialize the internal fields. */
|
/* Initialize the internal fields. */
|
||||||
result->length = 0;
|
result->length = 0;
|
||||||
result->head = NULL;
|
result->head = NULL;
|
||||||
|
result->data_size = data_size;
|
||||||
|
|
||||||
/* Return the result. */
|
/* Return the result. */
|
||||||
return result;
|
return result;
|
||||||
|
@ -448,10 +451,11 @@ int clear_list_data_single_linked_list(linked_list_t **list, void (*free_functio
|
||||||
|
|
||||||
/* This function attetmps to serialize a linked list into an array. */
|
/* This function attetmps to serialize a linked list into an array. */
|
||||||
/* The function will allocate and return a pointer to said array on success, NULL on failure. */
|
/* The function will allocate and return a pointer to said array on success, NULL on failure. */
|
||||||
/* The length of the array is sent through the length pointer, and set to -1 on failure. */
|
/* The length of the array is sent through the length pointer, and set to 0 on failure. */
|
||||||
void **serialize_linked_list(linked_list_t *list, int *length) {
|
static inline void *serialize_linked_list(linked_list_t *list, size_t *length) {
|
||||||
/* Local variables. */
|
/* Local variables. */
|
||||||
void **array;
|
void *array;
|
||||||
|
char *buffer;
|
||||||
node_t *current;
|
node_t *current;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
@ -462,17 +466,27 @@ void **serialize_linked_list(linked_list_t *list, int *length) {
|
||||||
/* Set the length. */
|
/* Set the length. */
|
||||||
*length = list->length;
|
*length = list->length;
|
||||||
/* Attempt to allocate the array. */
|
/* Attempt to allocate the array. */
|
||||||
array = (void**) malloc((*length) * sizeof(void*));
|
array = (void*) malloc((*length) * (list->data_size));
|
||||||
/* Check for allocation failure. */
|
/* Check for allocation failure. */
|
||||||
if (array == NULL) {
|
if (array == NULL) {
|
||||||
*length = -1;
|
*length = 0;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
/* Attempt to allocate the buffer. */
|
||||||
|
buffer = (char*) malloc(list->data_size);
|
||||||
|
if (buffer == NULL) {
|
||||||
|
*length = 0;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Fill the array. */
|
/* Fill the array. */
|
||||||
current = list->head;
|
current = list->head;
|
||||||
for (i = 0; i < *length; i++) {
|
for (i = 0; i < *length; i++) {
|
||||||
array[i] = current->data;
|
/* Attempt to copy to the current buffer. */
|
||||||
|
memcpy(buffer, (char*)current->data, list->data_size);
|
||||||
|
/* Copy the data for the current item. */
|
||||||
|
/*memcpy(array + (i * list->data_size), current->data, list->data_size);*/
|
||||||
|
/* Advance the pointer. */
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
/* This header defines a single-linked-list and related functions. */
|
/* This header defines a single-linked-list and related functions. */
|
||||||
/* Use the single nodes. */
|
/* Use the single nodes. */
|
||||||
#include <stdlib.h> /* Used in allocations and deallocations. */
|
#include <stdlib.h> /* Used in allocations and deallocations. */
|
||||||
|
#include <string.h> /* For using memcpy. */
|
||||||
#ifndef SINGLE_LINKED_LIST_H
|
#ifndef SINGLE_LINKED_LIST_H
|
||||||
#define SINGLE_LINKED_LIST_H
|
#define SINGLE_LINKED_LIST_H
|
||||||
#include "../nodes/single_node.h"
|
#include "../nodes/single_node.h"
|
||||||
|
@ -41,10 +42,12 @@ typedef struct linked_list {
|
||||||
int length;
|
int length;
|
||||||
/* Stores the head node pointer. */
|
/* Stores the head node pointer. */
|
||||||
node_t *head;
|
node_t *head;
|
||||||
|
/* The size of each of the data values. */
|
||||||
|
size_t data_size;
|
||||||
} linked_list_t;
|
} linked_list_t;
|
||||||
|
|
||||||
/* This function initializes a new linked list. */
|
/* This function initializes a new linked list. */
|
||||||
linked_list_t* initialize_single_linked_list();
|
linked_list_t* initialize_single_linked_list(size_t data_size);
|
||||||
/* This function appends a node to the given linked list. */
|
/* This function appends a node to the given linked list. */
|
||||||
int append_node_single_linked_list(linked_list_t *list, node_t *node);
|
int append_node_single_linked_list(linked_list_t *list, node_t *node);
|
||||||
/* This function prepends a node to the given linked list. */
|
/* This function prepends a node to the given linked list. */
|
||||||
|
@ -90,5 +93,5 @@ int clear_list_single_linked_list(linked_list_t **list);
|
||||||
/* This function deletes all the nodes and data stored within of the given list and the list itself. */
|
/* This function deletes all the nodes and data stored within of the given list and the list itself. */
|
||||||
int clear_list_data_single_linked_list(linked_list_t **list, void (*free_function)(void*));
|
int clear_list_data_single_linked_list(linked_list_t **list, void (*free_function)(void*));
|
||||||
/* This function attempts to serialize a linked list into an array. */
|
/* This function attempts to serialize a linked list into an array. */
|
||||||
void **serialize_linked_list(linked_list_t *list, int *length);
|
static inline void *serialize_linked_list(linked_list_t *list, size_t *length);
|
||||||
#endif /* SINGLE_LINKED_LIST_H */
|
#endif /* SINGLE_LINKED_LIST_H */
|
||||||
|
|
|
@ -16,14 +16,20 @@ bubble_sort_tests.o: bubble_sort_tests.c
|
||||||
double_linked_list_tests.out: double_linked_list_tests.c
|
double_linked_list_tests.out: double_linked_list_tests.c
|
||||||
gcc -g -fsanitize=address -fsanitize=leak -fsanitize=undefined double_linked_list_tests.c -o double_linked_list_tests.out
|
gcc -g -fsanitize=address -fsanitize=leak -fsanitize=undefined double_linked_list_tests.c -o double_linked_list_tests.out
|
||||||
|
|
||||||
queue_tests.out: queue_tests.c
|
single_linked_list.o: ../linked_lists/single_linked_list.h ../linked_lists/single_linked_list.c
|
||||||
gcc -g -I ../linked_lists/ -fsanitize=address -fsanitize=leak -fsanitize=undefined queue_tests.c -o queue_tests.out
|
gcc -g -c ../linked_lists/single_linked_list.c -o single_linked_list.o
|
||||||
|
|
||||||
|
queue_tests.o: queue_tests.c
|
||||||
|
gcc -g -c -fanalyzer -fsanitize=address -fsanitize=leak -fsanitize=undefined queue_tests.c -o queue_tests.o
|
||||||
|
|
||||||
|
queue_tests.out: queue_tests.o single_linked_list.o
|
||||||
|
gcc -g -fsanitize=address -fsanitize=leak -fsanitize=undefined single_linked_list.o queue_tests.o -o queue_tests.out
|
||||||
|
|
||||||
quick_sort_tests.out: array_print.o quick_sort_tests.o
|
quick_sort_tests.out: array_print.o quick_sort_tests.o
|
||||||
gcc -g -fanalyzer -Wanalyzer-too-complex -fanalyzer-call-summaries -fsanitize=address -fsanitize=leak -fsanitize=undefined array_print.o quick_sort_tests.o -o quick_sort_tests.out
|
gcc -g -fanalyzer -Wanalyzer-too-complex -fanalyzer-call-summaries -fsanitize=address -fsanitize=leak -fsanitize=undefined array_print.o quick_sort_tests.o -o quick_sort_tests.out
|
||||||
|
|
||||||
quick_sort_tests.o: quick_sort_tests.c
|
quick_sort_tests.o: quick_sort_tests.c
|
||||||
gcc -g -D DEBUG -c -fanalyzer -Wanalyzer-too-complex -fanalyzer-call-summaries quick_sort_tests.c
|
gcc -g -D DEBUG -fanalyzer -Wanalyzer-too-complex -fanalyzer-call-summaries -c quick_sort_tests.c
|
||||||
|
|
||||||
array_print.o: ../utils/array_print.c
|
array_print.o: ../utils/array_print.c
|
||||||
gcc -g -c ../utils/array_print.c
|
gcc -g -c ../utils/array_print.c
|
||||||
|
|
|
@ -66,7 +66,7 @@ linked_list_t* generate_long_random_list(int length) {
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* Allocate the list. */
|
/* Allocate the list. */
|
||||||
list = initialize_single_linked_list();
|
list = initialize_single_linked_list(sizeof(int));
|
||||||
if (list == NULL)
|
if (list == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
@ -119,15 +119,16 @@ int list_count_length(linked_list_t *list) {
|
||||||
/* This function attempts to allocate a list, fill it, then serialize it. */
|
/* This function attempts to allocate a list, fill it, then serialize it. */
|
||||||
int test_serialization() {
|
int test_serialization() {
|
||||||
/* Local variables. */
|
/* Local variables. */
|
||||||
int i, length;
|
int i;
|
||||||
|
size_t length;
|
||||||
/* Used to allocate and store a value. */
|
/* Used to allocate and store a value. */
|
||||||
int *value;
|
int *value;
|
||||||
/* Used to store the array. */
|
/* Used to store the array. */
|
||||||
void **array;
|
void *array;
|
||||||
|
|
||||||
/* Allocations. */
|
/* Allocations. */
|
||||||
/* Initialize the list. */
|
/* Initialize the list. */
|
||||||
linked_list_t *list = initialize_single_linked_list();
|
linked_list_t *list = initialize_single_linked_list(sizeof(int));
|
||||||
if (list == NULL) {
|
if (list == NULL) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -164,7 +165,9 @@ int test_serialization() {
|
||||||
|
|
||||||
/* Check the values. */
|
/* Check the values. */
|
||||||
for (i = 0; i < SERIALIZATION_TEST_LIST_LENGTH; i++) {
|
for (i = 0; i < SERIALIZATION_TEST_LIST_LENGTH; i++) {
|
||||||
if (*((int *)(array[i])) != i) {
|
/* Get the value. */
|
||||||
|
*value = *((int *)(array + (i * sizeof(int))));
|
||||||
|
if (*value != i) {
|
||||||
/* Clear the list. */
|
/* Clear the list. */
|
||||||
clear_list_data_single_linked_list(&list, NULL);
|
clear_list_data_single_linked_list(&list, NULL);
|
||||||
/* Free the array. */
|
/* Free the array. */
|
||||||
|
@ -191,7 +194,7 @@ int test_linked_lists() {
|
||||||
int error;
|
int error;
|
||||||
|
|
||||||
/* Initialize an empty list. */
|
/* Initialize an empty list. */
|
||||||
list = initialize_single_linked_list();
|
list = initialize_single_linked_list(sizeof(int));
|
||||||
/* Check that it was correctly created. */
|
/* Check that it was correctly created. */
|
||||||
assert(list != NULL);
|
assert(list != NULL);
|
||||||
assert(list->length == INITIAL_LIST_LENGTH);
|
assert(list->length == INITIAL_LIST_LENGTH);
|
||||||
|
|
Loading…
Reference in New Issue