modify code for qlearn rabbit

This commit is contained in:
2024-05-06 00:15:44 +02:00
parent 77b9ead331
commit 76472e9492
7 changed files with 446 additions and 14 deletions
+2 -1
View File
@@ -3,7 +3,7 @@
#if 0
#define GEN_LIST_ALL(type)\
#define GEN_LIST_ALL_L(type)\
\
struct main_list_##type *create_var_list_##type(){\
struct main_list_##type *ret_var_list = malloc(sizeof(struct main_list_##type));\
@@ -135,6 +135,7 @@
#endif
GEN_LIST_ALL(TYPE_CHAR)
GEN_LIST_ALL(TYPE_U_CHAR)
GEN_LIST_ALL(TYPE_INT)
+28 -1
View File
@@ -22,9 +22,12 @@
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);\
size_t move_current_to_begin_list_##type(struct main_list_##type *var_list);\
size_t move_current_to_end_list_##type(struct main_list_##type *var_list);\
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 remove_all_list_in_##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);\
@@ -43,7 +46,11 @@ GENERATE_LIST_ALL(TYPE_STRING)
GENERATE_LIST_ALL(TYPE_PTR)
#define FOR_LIST_FORM_BEGIN(type, var_list)\
for(move_current_to_begin_list_##type(var_list); var_list->current_list; increment_list_##type(var_list))
#define FOR_LIST_FORM_END(type, var_list)\
for(move_current_to_end_list_##type(var_list); var_list->current_list; decrement_list_##type(var_list))
#define GEN_LIST_ALL(type)\
@@ -117,6 +124,18 @@ GENERATE_LIST_ALL(TYPE_PTR)
var_list->current_index = index;\
return index;\
}\
size_t move_current_to_begin_list_##type(struct main_list_##type *var_list){\
if(var_list->begin_list == NULL) return 0;\
var_list->current_list = var_list->begin_list;\
var_list->current_index = 0;\
return 0;\
}\
size_t move_current_to_end_list_##type(struct main_list_##type *var_list){\
if(var_list->end_list == NULL) return 0;\
var_list->current_list = var_list->end_list;\
var_list->current_index = var_list->size - 1;\
return var_list->current_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;\
@@ -158,13 +177,21 @@ GENERATE_LIST_ALL(TYPE_PTR)
\
}\
}\
void free_all_var_list_##type(struct main_list_##type *var_list){\
void remove_all_list_in_##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);\
}\
var_list->begin_list = NULL;\
var_list->current_list = NULL;\
var_list->end_list = NULL;\
var_list->size = 0;\
var_list->current_index = 0;\
}\
void free_all_var_list_##type(struct main_list_##type *var_list){\
remove_all_list_in_##type(var_list);\
free(var_list);\
}\
void increment_list_##type(struct main_list_##type * var_list){\
+34
View File
@@ -49,6 +49,8 @@ TEST(insert){
TEST(remove){
struct main_list_TYPE_INT * var_list_int = create_var_list_TYPE_INT();
remove_all_list_in_TYPE_INT(var_list_int);
for(int i=0; i<5; ++i)
push_back_list_TYPE_INT(var_list_int, i);
@@ -74,6 +76,35 @@ TEST(remove){
}
TEST(remove_All){
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);
remove_all_list_in_TYPE_INT(var_list_int);
for(int i=0; i<5; ++i)
push_back_list_TYPE_INT(var_list_int, 10*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);
}
/*
struct test_c {
int value;
@@ -115,6 +146,9 @@ TEST(list_TYPE_PTR){
free_all_var_list_TYPE_PTR(var_list_ptr);
free(t0);
free(t1);
free(t2);
}