From 9594210f02572681ed581c5197ace4c207db0917 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Mon, 8 Nov 2021 16:37:57 +0800 Subject: initial commit Now the rough framework is established and the grammar class is sort of ready. It remains to write a general input reading mechanism. --- src/list.h | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/list.h (limited to 'src/list.h') diff --git a/src/list.h b/src/list.h new file mode 100644 index 0000000..d774f96 --- /dev/null +++ b/src/list.h @@ -0,0 +1,74 @@ +#ifndef LIST_H +#define LIST_H +#include +#include "util.h" + +/* Use an enumeration instead of a MACRO */ +enum { LIST_INIT_AMOUNT = 32 }; + +/* The elements of the list are all void pointers, so the users of the + list should determine the types of the elements manually. */ + +typedef struct List_s List; + +/* allocate a new list */ +/* upon failure return NULL */ +List *new_list(); + +/* add an element to the end of the list */ +/* upon failure return non-zero */ +unsigned char add_to_list(List *ls, void *element); + +/* pop an element from the end of the list, and return that element */ +/* upon failure return NULL */ +void *pop_from_list(List *ls); + +typedef void (*printer)(void *); + +typedef printer acter; /* a type that can act on list + elements */ + +void map_list(List *ls, acter f); + +void print_list(List *ls, printer prt); + +/* COPYER is expected to return NULL when it fails to copy. */ +typedef void *(*copyer)(void *); + +/* upon failure return NULL */ +void *copy_num(void *); + +/* upon failure return 1 */ +unsigned char copy_list(List *dest, List *source, copyer copyf); + +void *list_nth(List *ls, NUM n); + +NUM list_length(List *ls); + +/* Make sure the list has at least SIZE slots to use. This should + only be used to create fixed capacity arrays, otherwise we risk + frequently re-allocating and hence losing performance. */ +/* Upon failure return non-zero. */ +unsigned char list_assure_size(List *ls, NUM size); + +/* This is mainly used to set the length of a sparse list, since only + when dealing with sparse lists do we not need to care about the + elements. */ +/* Upon failure return non-zero. */ +unsigned char list_set_length(List *ls, NUM len); + +/* Convert a list to an array. + + ELEMENT_BYTES means the size of the type of elements. This is used + to calculate the number of elements in the array. + + The number of elements of the array will be stored in *NUM. */ +void *list_to_array(List *ls, NUM element_bytes, NUM *num); + +/* destroy the list: If ALL_FREE_P is 1, this frees every void + pointers contained in the list; if it is 2, this frees the first + pointer. In any case, the list is de-allocated. */ +void destroy_list(List *ls, unsigned char all_free_p); + + +#endif -- cgit v1.2.3-18-g5258