Skip to content

Commit

Permalink
adding the array_to_list function, fixing indexes from int to long
Browse files Browse the repository at this point in the history
  • Loading branch information
Ximaz committed Mar 22, 2024
1 parent c7ab026 commit a748a29
Show file tree
Hide file tree
Showing 16 changed files with 113 additions and 57 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ a.out
*.log
unit_tests
liblist.so
.vscode
16 changes: 0 additions & 16 deletions .vscode/settings.json

This file was deleted.

32 changes: 22 additions & 10 deletions include/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ typedef struct s_list_elem {
} list_elem_t;

typedef struct s_list {
int count;
long count;
list_elem_destroy_t destroy;
list_elem_t *elems_head;
list_elem_t *elems_tail;
Expand All @@ -36,14 +36,25 @@ list_t *list_new(list_elem_destroy_t destroy);

void list_destroy(list_t *list);

/**
* @brief Transforms a generic array to a list
*
* @param[in] array
* @param[in] size The size of the array
* @param[in] destroy [OPT] The destructor for array's elem (may be NULL)
* @return The new list on success, NULL on error
*/
list_t *array_to_list(void *const *const array, long size,
list_elem_destroy_t destroy);

/* Getters */

/**
* @brief Returns the number of elements inside the list
*
* @param[in] list
*/
int list_count(list_t const *list);
long list_count(list_t const *list);

/**
* @brief This function type will be used to compare two elements
Expand All @@ -63,7 +74,7 @@ typedef int (*list_equal_cmp_t)(void const *expected, void const *elem);
* @param[in] elem
* @param[in] cmp the function to compare elements with
*/
int list_index_of(list_t const *list, void const *elem, list_equal_cmp_t cmp);
long list_index_of(list_t const *list, void const *elem, list_equal_cmp_t cmp);

/**
* @brief Returns the element for the given index
Expand All @@ -72,7 +83,7 @@ int list_index_of(list_t const *list, void const *elem, list_equal_cmp_t cmp);
* @param[in] index
* @return the element on success, NULL on error (Out-Of-Range)
*/
void *list_value_at(list_t const *list, int index);
void *list_value_at(list_t const *list, long index);

/* Operators */

Expand Down Expand Up @@ -109,9 +120,10 @@ int list_push_front(list_t *list, void *elem);
*
* @param[in] list
* @param[in] elem
* @param[in] index
* @return 0 on success, -1 on error (Out-Of-Range)
*/
int list_insert_at(list_t *list, void *elem, int index);
int list_insert_at(list_t *list, void *elem, long index);

/**
* @brief Pushes an element at the back of the list
Expand Down Expand Up @@ -146,7 +158,7 @@ void *list_pop_front(list_t *list);
* @param[in] elem
* @return the pop'd element if any, NULL otherwise
*/
void *list_remove_at(list_t *list, int index);
void *list_remove_at(list_t *list, long index);

/**
* @brief Pops an element from the back of the list
Expand Down Expand Up @@ -201,7 +213,7 @@ int list_reverse_into(list_t const *list, list_t *output);
* @param[in] from
* @param[in] to (excluded)
*/
list_t *list_slice(list_t const *list, int from, int to);
list_t *list_slice(list_t const *list, long from, long to);

/**
* @brief Slices the current list itself, no allocation of any kind. All
Expand All @@ -213,7 +225,7 @@ list_t *list_slice(list_t const *list, int from, int to);
* @param[in] to (excluded)
* @return 0 on success, -1 otherwise, the list is unchanged (Out-Of-Range)
*/
int list_slice_itself(list_t *list, int from, int to);
int list_slice_itself(list_t *list, long from, long to);

/**
* @brief Slices a list from 'from' to 'to' (excluded) into the output list
Expand All @@ -224,7 +236,7 @@ int list_slice_itself(list_t *list, int from, int to);
* @param[out] output
* @return 0 on success, -1 otherwise (Out-Of-Range)
*/
int list_slice_into(list_t const *list, int from, int to,
int list_slice_into(list_t const *list, long from, long to,
list_t *output);

/**
Expand Down Expand Up @@ -360,7 +372,7 @@ void list_filter_into(list_t const *list, list_filter_t filter,
* @param[in] list the list that holds the 'elem' at 'index'
* @return The new accumulator / last value to store
*/
typedef void *(*list_reduce_t)(void const *acc, void const *elem, int index,
typedef void *(*list_reduce_t)(void const *acc, void const *elem, long index,
list_t const *list);

/**
Expand Down
25 changes: 25 additions & 0 deletions src/array_to_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
** EPITECH PROJECT, 2024
** List in C
** File description:
** array_to_list.c
*/

#include "list.h"

list_t *array_to_list(void *const *const array, long size,
list_elem_destroy_t destroy)
{
list_t *list = NULL;

if (0 >= size)
return NULL;
list = list_new(destroy);
if (NULL == list)
return NULL;
do {
--size;
list_push_front(list, array[size]);
} while (0 < size);
return list;
}
2 changes: 1 addition & 1 deletion src/list_count.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "list.h"

int list_count(list_t const *list)
long list_count(list_t const *list)
{
return list->count;
}
4 changes: 2 additions & 2 deletions src/list_index_of.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

#include "list.h"

int list_index_of(list_t const *list, void const *elem, list_equal_cmp_t cmp)
long list_index_of(list_t const *list, void const *elem, list_equal_cmp_t cmp)
{
int i = 0;
long i = 0;
list_elem_t *e = list->elems_head;

while (NULL != e) {
Expand Down
4 changes: 2 additions & 2 deletions src/list_insert_at.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static list_elem_t *list_elem_insert(list_elem_t *e, void *elem)
return new_elem;
}

static int list_insert(list_t *list, void *elem, int index)
static int list_insert(list_t *list, void *elem, long index)
{
list_elem_t *new_e = NULL;
list_elem_t *e = list->elems_head;
Expand All @@ -38,7 +38,7 @@ static int list_insert(list_t *list, void *elem, int index)
return 0;
}

int list_insert_at(list_t *list, void *elem, int index)
int list_insert_at(list_t *list, void *elem, long index)
{
if ((0 > index && 0 == list->count) || index > list->count)
return -1;
Expand Down
2 changes: 1 addition & 1 deletion src/list_remove_at.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static void list_elem_remove(list_elem_t *e)
free(e);
}

void *list_remove_at(list_t *list, int index)
void *list_remove_at(list_t *list, long index)
{
void *data = NULL;
list_elem_t *e = list->elems_head;
Expand Down
14 changes: 7 additions & 7 deletions src/list_slice.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <stdlib.h>
#include <stdio.h>

static list_elem_t *move_to(list_elem_t *node, int range)
static list_elem_t *move_to(list_elem_t *node, long range)
{
while (NULL != node && 0 < range) {
node = node->next;
Expand All @@ -18,7 +18,7 @@ static list_elem_t *move_to(list_elem_t *node, int range)
return node;
}

static int convert_indexes(list_t const *list, int *from, int *to)
static int convert_indexes(list_t const *list, long *from, long *to)
{
if (0 > *from)
*from += list->count;
Expand All @@ -29,7 +29,7 @@ static int convert_indexes(list_t const *list, int *from, int *to)
return 0;
}

int list_slice_into(list_t const *list, int from, int to, list_t *output)
int list_slice_into(list_t const *list, long from, long to, list_t *output)
{
list_elem_t *head = list->elems_head;

Expand All @@ -44,7 +44,7 @@ int list_slice_into(list_t const *list, int from, int to, list_t *output)
return 0;
}

list_t *list_slice(list_t const *list, int from, int to)
list_t *list_slice(list_t const *list, long from, long to)
{
list_t *sliced = NULL;

Expand All @@ -60,7 +60,7 @@ list_t *list_slice(list_t const *list, int from, int to)
return sliced;
}

static void destroy_list_tail(list_t *list, int range)
static void destroy_list_tail(list_t *list, long range)
{
list_elem_t *tmp = NULL;

Expand All @@ -75,7 +75,7 @@ static void destroy_list_tail(list_t *list, int range)
list->elems_tail->next = NULL;
}

static void destroy_list_head(list_t *list, int range)
static void destroy_list_head(list_t *list, long range)
{
list_elem_t *tmp = NULL;

Expand All @@ -90,7 +90,7 @@ static void destroy_list_head(list_t *list, int range)
list->elems_head->prev = NULL;
}

int list_slice_itself(list_t *list, int from, int to)
int list_slice_itself(list_t *list, long from, long to)
{
if (-1 == convert_indexes(list, &from, &to))
return -1;
Expand Down
2 changes: 0 additions & 2 deletions src/list_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ static void quick_sort(list_t *list, int low, int high, list_sort_t sort)
if (low < high) {
p = partition(list, low, high, sort);
quick_sort(list, low, p - 1, sort);
list_print(list);
quick_sort(list, p + 1, high, sort);
list_print(list);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/list_value_at.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "list.h"

void *list_value_at(list_t const *list, int index)
void *list_value_at(list_t const *list, long index)
{
list_elem_t *elem = list->elems_head;

Expand Down
36 changes: 36 additions & 0 deletions tests/tests_array_to_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
** EPITECH PROJECT, 2024
** Tests of List in C
** File description:
** tests_array_to_list.c
*/

#include "list.h"
#include <criterion/criterion.h>
#include <criterion/new/assert.h>

Test(array_to_list, test_impl)
{
void *array[] = { (void *) 1, (void *) 2, (void *) 3, (void *) 4, (void *) 5 };
long size = 5;
list_t expected = { 0 };
list_t *list = array_to_list(array, size, NULL);

cr_assert(eq(int, 0, list_push_back(&expected, (void *) 1)));
cr_assert(eq(int, 0, list_push_back(&expected, (void *) 2)));
cr_assert(eq(int, 0, list_push_back(&expected, (void *) 3)));
cr_assert(eq(int, 0, list_push_back(&expected, (void *) 4)));
cr_assert(eq(int, 0, list_push_back(&expected, (void *) 5)));
cr_assert(ne(ptr, NULL, list));
cr_assert(eq(int, 1, list_equal(&expected, list, NULL)));
list_destroy(list);
list_clear(&expected);
}

Test(array_to_list, test_bad_inputs)
{
void *array[] = { (void *) 1, (void *) 2, (void *) 3, (void *) 4, (void *) 5 };

cr_assert(eq(ptr, NULL, array_to_list(array, 0, NULL)));
cr_assert(eq(ptr, NULL, array_to_list(array, -1, NULL)));
}
10 changes: 5 additions & 5 deletions tests/tests_list_index_of.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ Test(list_index_of, test_impl)
cr_assert(eq(int, 0, list_insert_at(list, "This is my tail 2-1 !", -1)));
cr_assert(eq(int, 0, list_insert_at(list, "This is my tail 2 !", 2)));
cr_assert(eq(int, 4, list_count(list)));
cr_assert(eq(int, 0, list_index_of(list, "This is my head !", my_strcmp)));
cr_assert(eq(int, 1, list_index_of(list, "This is my tail !", my_strcmp)));
cr_assert(eq(int, 2, list_index_of(list, "This is my tail 2 !", my_strcmp)));
cr_assert(eq(int, 3, list_index_of(list, "This is my tail 2-1 !", my_strcmp)));
cr_assert(eq(int, -1, list_index_of(list, "Invalid Element", my_strcmp)));
cr_assert(eq(long, 0, list_index_of(list, "This is my head !", my_strcmp)));
cr_assert(eq(long, 1, list_index_of(list, "This is my tail !", my_strcmp)));
cr_assert(eq(long, 2, list_index_of(list, "This is my tail 2 !", my_strcmp)));
cr_assert(eq(long, 3, list_index_of(list, "This is my tail 2-1 !", my_strcmp)));
cr_assert(eq(long, -1, list_index_of(list, "Invalid Element", my_strcmp)));
list_destroy(list);
}
4 changes: 2 additions & 2 deletions tests/tests_list_reduce.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "list.h"

static void *reduce(void const *acc, void const *current,
int index __attribute__((unused)),
long index __attribute__((unused)),
list_t const *list __attribute__((unused)))
{
unsigned long long a = (unsigned long long) acc;
Expand All @@ -22,7 +22,7 @@ static void *reduce(void const *acc, void const *current,
}

static void *reduce_str(void const *acc, void const *current,
int index __attribute__((unused)),
long index __attribute__((unused)),
list_t const *list __attribute__((unused)))
{
char const *a = acc;
Expand Down
12 changes: 6 additions & 6 deletions tests/tests_pop_and_remove.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ Test(list_pop_back, test_impl)

cr_assert(eq(int, 0, list_push_back(list, strdup("Hello, World !"))));
cr_assert(eq(int, 0, list_push_back(list, strdup("This is my tail !"))));
cr_assert(eq(int, 2, list_count(list)));
cr_assert(eq(long, 2, list_count(list)));
s2 = list_pop_back(list);
cr_assert(eq(int, 1, list_count(list)));
cr_assert(eq(long, 1, list_count(list)));
s1 = list_pop_back(list);
cr_assert(eq(int, 0, list_count(list)));
cr_assert(eq(long, 0, list_count(list)));
cr_assert(eq(str, "Hello, World !", s1));
cr_assert(eq(str, "This is my tail !", s2));
free(s2);
Expand All @@ -39,11 +39,11 @@ Test(list_pop_front, test_impl)

cr_assert(eq(int, 0, list_push_back(list, strdup("This is my head !"))));
cr_assert(eq(int, 0, list_push_back(list, strdup("Hello, World !"))));
cr_assert(eq(int, 2, list_count(list)));
cr_assert(eq(long, 2, list_count(list)));
s1 = list_pop_front(list);
cr_assert(eq(int, 1, list_count(list)));
cr_assert(eq(long, 1, list_count(list)));
s2 = list_pop_front(list);
cr_assert(eq(int, 0, list_count(list)));
cr_assert(eq(long, 0, list_count(list)));
cr_assert(eq(str, "This is my head !", s1));
cr_assert(eq(str, "Hello, World !", s2));
free(s2);
Expand Down
Loading

0 comments on commit a748a29

Please sign in to comment.