create linked list generic
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
|
||||
CC=gcc
|
||||
TOOLDIR=$(PWD)/../ytools_t
|
||||
|
||||
INCLUDE_DIR=$(TOOLDIR)/include
|
||||
CFLAGS=-I$(INCLUDE_DIR) -I./src
|
||||
|
||||
#SRC_DIR=$(ROOT_DIR)/src
|
||||
#SRC=$(wildcard */*/*.c)
|
||||
#HEADS=$(OBJS:.o=.h)
|
||||
|
||||
LISTSRC=src/list_t/list_t.c
|
||||
LISTSRC_O=$(LISTSRC:.c=.o)
|
||||
|
||||
SETTSRC=src/set_theoric_t/set_theoric_t.c
|
||||
SETTSRC_O=$(SETTSRC:.c=.o)
|
||||
|
||||
|
||||
TOOLSRC_O=$(TOOLDIR)/src/tools_t/tools_t.o
|
||||
#TOOLSRC=$(TOOLDIR)/src/tools_t/tools_t.c
|
||||
#TOOLSRC_O=$(TOOLSRC:.c=.o)
|
||||
|
||||
SRC=$(wildcard **/**/*.c)
|
||||
OBJ=$(SRC:.c=.o) #$(TOOLSRC_O)
|
||||
|
||||
TOPTARGETS := all clean
|
||||
DEP=$(TOOLDIR)
|
||||
|
||||
$(TOPTARGETS): $(DEP)
|
||||
|
||||
all: $(LISTSRC_O)
|
||||
|
||||
|
||||
$(LISTSRC_O): $(LISTSRC) $(TOOLSRC_O)
|
||||
$(CC) -o $@ -c $< $(CFLAGS)
|
||||
|
||||
#$(SETTSRC_O) : $(SETTSRC) $(TOOLSRC_O)
|
||||
# $(CC) -o $@ -c $< $(CFLAGS)
|
||||
|
||||
$(DEP):
|
||||
$(MAKE) -C $@ $(MAKECMDGOALS)
|
||||
|
||||
#$(TOOLSRC_O): $(TOOLSRC)
|
||||
# $(CC) -o $@ -c $< $(CFLAGS)
|
||||
|
||||
.PHONY: clean mrproper
|
||||
|
||||
clean:
|
||||
rm -f $(OBJ)
|
||||
|
||||
mrproper: clean
|
||||
rm -f $(EXEC)
|
||||
|
||||
run: $(EXEC)
|
||||
$(EXEC) -h
|
||||
@@ -0,0 +1,161 @@
|
||||
|
||||
#include "list_t/list_t.h"
|
||||
|
||||
#define GEN_LIST_ALL(type)\
|
||||
/*struct list_##type {\
|
||||
type value;\
|
||||
struct list_##type *preview;\
|
||||
struct list_##type *next;\
|
||||
};\
|
||||
struct main_list_##type {\
|
||||
struct list_##type *begin_list;\
|
||||
struct list_##type *current_list;\
|
||||
size_t current_index;\
|
||||
struct list_##type *end_list;\
|
||||
size_t size;\
|
||||
};*/\
|
||||
struct main_list_##type *create_var_list_##type(){\
|
||||
struct main_list_##type *ret_var_list = malloc(sizeof(struct main_list_##type));\
|
||||
ret_var_list->begin_list = NULL;\
|
||||
ret_var_list->end_list = NULL;\
|
||||
ret_var_list->current_list = NULL;\
|
||||
ret_var_list->current_index = 0;\
|
||||
ret_var_list->size = 0;\
|
||||
return ret_var_list;\
|
||||
}\
|
||||
void push_back_list_##type(struct main_list_##type *var_list, type value){\
|
||||
struct list_##type * list_to_add = malloc(sizeof(struct list_##type));\
|
||||
list_to_add->value = value;\
|
||||
list_to_add->preview = var_list->end_list;\
|
||||
list_to_add->next = NULL;\
|
||||
if(var_list->end_list){\
|
||||
(var_list->end_list)->next = list_to_add;\
|
||||
}else {\
|
||||
var_list->begin_list = list_to_add;\
|
||||
var_list->current_list= list_to_add;\
|
||||
}\
|
||||
var_list->end_list = list_to_add;\
|
||||
++(var_list->size);\
|
||||
}\
|
||||
void push_front_list_##type(struct main_list_##type *var_list, type value){\
|
||||
struct list_##type * list_to_add = malloc(sizeof(struct list_##type));\
|
||||
list_to_add->value = value;\
|
||||
list_to_add->preview = NULL;\
|
||||
list_to_add->next = var_list->begin_list;\
|
||||
if(var_list->begin_list){\
|
||||
(var_list->begin_list)->preview = list_to_add;\
|
||||
++(var_list->current_index);\
|
||||
}else {\
|
||||
var_list->end_list = list_to_add;\
|
||||
var_list->current_list= list_to_add;\
|
||||
}\
|
||||
var_list->begin_list = list_to_add;\
|
||||
++(var_list->size);\
|
||||
}\
|
||||
size_t move_current_to_index_list_##type(struct main_list_##type *var_list, size_t index){\
|
||||
if(index == var_list->current_index) return index;\
|
||||
if(var_list->begin_list == NULL) return 0;\
|
||||
if(index >= var_list->size){\
|
||||
var_list->current_list = var_list->end_list;\
|
||||
var_list->current_index = var_list->size - 1;\
|
||||
return var_list->size - 1;\
|
||||
}\
|
||||
if((var_list->current_index >= var_list->size) || !(var_list->current_list)){\
|
||||
var_list->current_list = var_list->end_list;\
|
||||
var_list->current_index = var_list->size - 1;\
|
||||
}\
|
||||
long from_current_index = var_list->current_index - index; \
|
||||
size_t abs_cur_diff = abs(from_current_index); \
|
||||
size_t array_diff_index[3] = {index, abs_cur_diff , var_list->size - 1 - index}; \
|
||||
size_t index_nearest = ARG_MIN_ARRAY_TYPE_SIZE_T(array_diff_index, 3);\
|
||||
if(index_nearest == 0){\
|
||||
var_list->current_list = var_list->begin_list;\
|
||||
for(size_t i=0; i<array_diff_index[0]; ++i) var_list->current_list = (var_list->current_list)->next; \
|
||||
}\
|
||||
else if(index_nearest == 2){\
|
||||
var_list->current_list = var_list->end_list;\
|
||||
for(size_t i=0; i < array_diff_index[0]; ++i) var_list->current_list = (var_list->current_list)->preview; \
|
||||
}else if(from_current_index >= 0) \
|
||||
for(size_t i=0; i < array_diff_index[1]; ++i) var_list->current_list = (var_list->current_list)->preview; \
|
||||
else \
|
||||
for(size_t i=0; i < array_diff_index[1]; ++i) var_list->current_list = (var_list->current_list)->next; \
|
||||
\
|
||||
var_list->current_index = index;\
|
||||
return index;\
|
||||
}\
|
||||
void insert_into_list_##type(struct main_list_##type *var_list, size_t index, type value ){\
|
||||
struct list_##type * list_to_add = malloc(sizeof(struct list_##type));\
|
||||
list_to_add->value = value;\
|
||||
if(var_list->begin_list == NULL){\
|
||||
list_to_add->preview = NULL;\
|
||||
list_to_add->next = NULL;\
|
||||
var_list->begin_list = list_to_add;\
|
||||
var_list->end_list = list_to_add;\
|
||||
var_list->current_list = list_to_add;\
|
||||
var_list->current_index = 0;\
|
||||
\
|
||||
}else {\
|
||||
size_t ii = move_current_to_index_list_##type(var_list, index);\
|
||||
if(index > ii)\
|
||||
printf("%ld out of range, we put the value at %ld index of the list \n",index, ii);\
|
||||
if(var_list->current_list){\
|
||||
list_to_add->preview = (var_list->current_list)->preview;\
|
||||
list_to_add->next = var_list->current_list;\
|
||||
if(list_to_add->preview)\
|
||||
(list_to_add->preview)->next = list_to_add;\
|
||||
else var_list->begin_list = list_to_add;\
|
||||
(var_list->current_list)->preview = list_to_add;\
|
||||
var_list->current_list = list_to_add;\
|
||||
}\
|
||||
}\
|
||||
++(var_list->size);\
|
||||
}\
|
||||
void remove_index_from_list_##type(struct main_list_##type *var_list, size_t index ){\
|
||||
if( index == move_current_to_index_list_##type(var_list, index)) {\
|
||||
struct list_##type * list_tmp_prev = (var_list->current_list)->preview;\
|
||||
struct list_##type * list_tmp_next = (var_list->current_list)->next;\
|
||||
if(list_tmp_prev){\
|
||||
list_tmp_prev->next = list_tmp_next;\
|
||||
if(list_tmp_next) list_tmp_next->preview = list_tmp_prev;\
|
||||
}\
|
||||
free(var_list->current_list);\
|
||||
var_list->current_list = list_tmp_next;\
|
||||
--(var_list->size);\
|
||||
\
|
||||
}\
|
||||
}\
|
||||
void free_all_var_list_##type(struct main_list_##type *var_list){\
|
||||
struct list_##type *tmp = var_list->begin_list;\
|
||||
while(tmp){\
|
||||
var_list->current_list = tmp;\
|
||||
tmp = tmp->next;\
|
||||
free(var_list->current_list);\
|
||||
}\
|
||||
free(var_list);\
|
||||
}\
|
||||
void increment_list_##type(struct main_list_##type * var_list){\
|
||||
var_list->current_list = (var_list->current_list)->next;\
|
||||
++(var_list->current_index);\
|
||||
}\
|
||||
void decrement_list_##type(struct main_list_##type * var_list){\
|
||||
var_list->current_list = (var_list->current_list)->preview;\
|
||||
--(var_list->current_index);\
|
||||
}\
|
||||
|
||||
|
||||
|
||||
GEN_LIST_ALL(TYPE_CHAR)
|
||||
GEN_LIST_ALL(TYPE_U_CHAR)
|
||||
GEN_LIST_ALL(TYPE_INT)
|
||||
GEN_LIST_ALL(TYPE_U_INT)
|
||||
GEN_LIST_ALL(TYPE_L_INT)
|
||||
GEN_LIST_ALL(TYPE_U_L_INT)
|
||||
GEN_LIST_ALL(TYPE_SIZE_T)
|
||||
GEN_LIST_ALL(TYPE_FLOAT)
|
||||
GEN_LIST_ALL(TYPE_DOUBLE)
|
||||
GEN_LIST_ALL(TYPE_L_DOUBLE)
|
||||
GEN_LIST_ALL(TYPE_STRING)
|
||||
|
||||
GEN_LIST_ALL(TYPE_PTR)
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
#ifndef __LIST_T_C__H
|
||||
#define __LIST_T_C__H
|
||||
|
||||
#include "tools_t/tools_t.h"
|
||||
|
||||
#define TYPE_PTR void*
|
||||
|
||||
#define GENERATE_LIST_ALL(type)\
|
||||
struct list_##type {\
|
||||
type value;\
|
||||
struct list_##type *preview;\
|
||||
struct list_##type *next;\
|
||||
};\
|
||||
struct main_list_##type {\
|
||||
struct list_##type *begin_list;\
|
||||
struct list_##type *current_list;\
|
||||
size_t current_index;\
|
||||
struct list_##type *end_list;\
|
||||
size_t size;\
|
||||
};\
|
||||
struct main_list_##type *create_var_list_##type();\
|
||||
void push_back_list_##type(struct main_list_##type *var_list, type value);\
|
||||
void push_front_list_##type(struct main_list_##type *var_list, type value);\
|
||||
size_t move_current_to_index_list_##type(struct main_list_##type *var_list, size_t index);\
|
||||
void insert_into_list_##type(struct main_list_##type *var_list, size_t index, type value );\
|
||||
void remove_index_from_list_##type(struct main_list_##type *var_list, size_t index );\
|
||||
void free_all_var_list_##type(struct main_list_##type *var_list);\
|
||||
void increment_list_##type(struct main_list_##type * var_list);\
|
||||
void decrement_list_##type(struct main_list_##type * var_list);\
|
||||
|
||||
|
||||
GENERATE_LIST_ALL(TYPE_CHAR)
|
||||
GENERATE_LIST_ALL(TYPE_U_CHAR)
|
||||
GENERATE_LIST_ALL(TYPE_INT)
|
||||
GENERATE_LIST_ALL(TYPE_U_INT)
|
||||
GENERATE_LIST_ALL(TYPE_L_INT)
|
||||
GENERATE_LIST_ALL(TYPE_U_L_INT)
|
||||
GENERATE_LIST_ALL(TYPE_SIZE_T)
|
||||
GENERATE_LIST_ALL(TYPE_FLOAT)
|
||||
GENERATE_LIST_ALL(TYPE_DOUBLE)
|
||||
GENERATE_LIST_ALL(TYPE_L_DOUBLE)
|
||||
GENERATE_LIST_ALL(TYPE_STRING)
|
||||
|
||||
GENERATE_LIST_ALL(TYPE_PTR)
|
||||
|
||||
|
||||
#endif /* __LIST_T_C__H */
|
||||
@@ -0,0 +1,60 @@
|
||||
|
||||
|
||||
|
||||
|
||||
NAME_TEST=is_good
|
||||
CC=gcc
|
||||
ROOT_DIR=$(PWD)
|
||||
YTESTDIR=$(PWD)/../../ytest_t
|
||||
|
||||
INCLUDE_DIR=$(PWD)/../src
|
||||
CFLAGS=-I$(INCLUDE_DIR) -I$(YTESTDIR)/include_ytest/include
|
||||
LDFLAGS=-L$(YTESTDIR) -lytest
|
||||
|
||||
#SRC_DIR=$(ROOT_DIR)/src
|
||||
#SRC=$(wildcard */*/*.c)
|
||||
SRC=$(wildcard **/**/*.c)
|
||||
#HEADS=$(OBJS:.o=.h)
|
||||
TEST_DIR=$(PWD)
|
||||
EXECSRC=$(NAME_TEST).c
|
||||
EXEC=launch_$(NAME_TEST)_m
|
||||
|
||||
LISTDIR=$(PWD)/..
|
||||
|
||||
LISTSRC_O=$(LISTDIR)/src/list_t/list_t.o
|
||||
|
||||
TOPTARGETS := all clean
|
||||
|
||||
DEPS=$(LISTDIR) $(YTESTDIR)
|
||||
|
||||
$(TOPTARGETS): $(DEPS)
|
||||
|
||||
$(DEPS):
|
||||
$(MAKE) -C $@ $(MAKECMDGOALS)
|
||||
|
||||
|
||||
#LISTSRC_O=$(LISTSRC:.c=.o)
|
||||
#SETTSRC_O=$(PWD)/../src/set_theoric_t/set_theoric_t.o
|
||||
#SETTSRC_O=$(SETTSRC:.c=.o)
|
||||
#TOOLSRC=$(TOOLDIR)/src/tools_t/tools_t.c
|
||||
#TOOLSRC_O=$(TOOLSRC:.c=.o)
|
||||
|
||||
OBJ=$(SRC:.c=.o) $(LISTSRC_O)
|
||||
|
||||
LIB_YTEST=$(YTESTDIR)/libytest.so
|
||||
|
||||
all: $(EXEC) $(LIB_YTEST)
|
||||
|
||||
$(EXEC): $(EXECSRC) $(OBJ)
|
||||
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
|
||||
|
||||
.PHONY: clean mrproper
|
||||
|
||||
clean:
|
||||
rm -f $(OBJ)
|
||||
|
||||
mrproper: clean
|
||||
rm -f $(EXEC)
|
||||
|
||||
run: $(EXEC)
|
||||
$(EXEC) -h
|
||||
@@ -0,0 +1,23 @@
|
||||
#!/bin/bash
|
||||
|
||||
if [ "$#" -le 0 ] ; then
|
||||
echo "Usage: $0 is_good.c" >&2
|
||||
echo "for example to compile: is_good.c" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [ "$#" -le 1 ] ; then
|
||||
echo "Usage: $0 $1" >&2
|
||||
echo " we can add more option for example '-D DEBUG=1' to have debug print, '-D HK' to have gtest like prompt, od '-g' to gbd" >&2
|
||||
echo "for example: $0 $1 \"-D DEBUG=1 -D HK -g\""
|
||||
fi
|
||||
|
||||
DIR_YTEST=$PWD/../../ytest_t
|
||||
SRC=../src
|
||||
|
||||
gcc -o launch_is_good_c $1 -L$DIR_YTEST $2 -lytest -I$DIR_YTEST/include_ytest/include $SRC/list_t/list_t.c -I$SRC
|
||||
#gcc -o launch_is_good_c $1 $2 -lytest -I../include_ytest src/list_t/list_t.o src/set_theoric_t/set_theoric_t.o -I./src
|
||||
|
||||
export LD_LIBRARY_PATH=$DIR_YTEST/:LD_LIBRARY_PATH
|
||||
|
||||
|
||||
#gcc $1 src/ftest/ftest.c src/fmock/fmock.c src/tools_t/tools_t.c src/bar_progress/bar_progress.c src/list_t/list_t.c src/set_theoric_t/set_theoric_t.c -I./include $2 -o launch_is_good_c -lpthread
|
||||
@@ -0,0 +1,84 @@
|
||||
#include "ftest/ftest.h"
|
||||
#include "fmock/fmock.h"
|
||||
|
||||
|
||||
#include "list_t/list_t.h"
|
||||
|
||||
|
||||
TEST(list_create){
|
||||
struct main_list_TYPE_INT * var_list_int = create_var_list_TYPE_INT();
|
||||
|
||||
push_back_list_TYPE_INT(var_list_int, 9);
|
||||
push_back_list_TYPE_INT(var_list_int, -9);
|
||||
push_back_list_TYPE_INT(var_list_int, 19);
|
||||
|
||||
move_current_to_index_list_TYPE_INT(var_list_int, 0);
|
||||
while(var_list_int->current_list){
|
||||
LOG("cur %ld : %d : size :%ld \n", var_list_int->current_index, (var_list_int->current_list)->value, var_list_int->size);
|
||||
increment_list_TYPE_INT(var_list_int);
|
||||
}
|
||||
|
||||
LOG("%s"," =============== \n");
|
||||
for(move_current_to_index_list_TYPE_INT(var_list_int, (var_list_int->size) - 1); var_list_int->current_list; decrement_list_TYPE_INT(var_list_int))
|
||||
LOG("cur %ld : %d : size :%ld \n", var_list_int->current_index, (var_list_int->current_list)->value, var_list_int->size);
|
||||
|
||||
free_all_var_list_TYPE_INT(var_list_int);
|
||||
|
||||
}
|
||||
|
||||
TEST(insert){
|
||||
struct main_list_TYPE_INT * var_list_int = create_var_list_TYPE_INT();
|
||||
|
||||
for(int i=0; i<5; ++i)
|
||||
push_back_list_TYPE_INT(var_list_int, i);
|
||||
|
||||
for(int i=0; i<10; ++i)
|
||||
insert_into_list_TYPE_INT(var_list_int, i, -2*i+1);
|
||||
|
||||
for(int i=var_list_int->size; i< 25; ++i)
|
||||
insert_into_list_TYPE_INT(var_list_int, i, 3*i+1);
|
||||
|
||||
for(move_current_to_index_list_TYPE_INT(var_list_int, 0); var_list_int->current_list; increment_list_TYPE_INT(var_list_int))
|
||||
LOG("cur %ld : %d : size :%ld \n", var_list_int->current_index, (var_list_int->current_list)->value, var_list_int->size);
|
||||
|
||||
free_all_var_list_TYPE_INT(var_list_int);
|
||||
|
||||
}
|
||||
|
||||
|
||||
TEST(remove){
|
||||
struct main_list_TYPE_INT * var_list_int = create_var_list_TYPE_INT();
|
||||
|
||||
for(int i=0; i<5; ++i)
|
||||
push_back_list_TYPE_INT(var_list_int, i);
|
||||
|
||||
for(int i=0; i<10; ++i)
|
||||
insert_into_list_TYPE_INT(var_list_int, i, -2*i+1);
|
||||
|
||||
for(int i=var_list_int->size; i< 25; ++i)
|
||||
insert_into_list_TYPE_INT(var_list_int, i, 3*i+1);
|
||||
|
||||
for(move_current_to_index_list_TYPE_INT(var_list_int, 0); var_list_int->current_list; increment_list_TYPE_INT(var_list_int))
|
||||
LOG("cur %ld : %d : size :%ld \n", var_list_int->current_index, (var_list_int->current_list)->value, var_list_int->size);
|
||||
|
||||
|
||||
for(int i=1; i<var_list_int->size; i+= 3)
|
||||
remove_index_from_list_TYPE_INT(var_list_int, i);
|
||||
|
||||
LOG("%s"," =============== \n");
|
||||
|
||||
for(move_current_to_index_list_TYPE_INT(var_list_int, 0); var_list_int->current_list; increment_list_TYPE_INT(var_list_int))
|
||||
LOG("cur %ld : %d : size :%ld \n", var_list_int->current_index, (var_list_int->current_list)->value, var_list_int->size);
|
||||
|
||||
free_all_var_list_TYPE_INT(var_list_int);
|
||||
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv){
|
||||
|
||||
|
||||
run_all_tests_args(argc, argv);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user