#ifndef _LIST_H #define _LIST_H #include #include #include #include #define L_DEFAULT_BLOCKSIZE 16 /* Functional Macros */ #define l_create_d(type) l_create(type, L_DEFAULT_BLOCKSIZE) #define l_create_t(type, bs) l_create(sizeof(type), bs) #define l_create_td(type) l_create_d(sizeof(type)) #define l_create_from_d(from, length, type) l_create_from(from,length, type, L_DEFAULT_BLOCKSIZE) #define l_get_v(list,index, type) *(type*)l_get(list,index) /** Returns 1 if is a valid index for **/ #define l_bcheck(t, index) (index>=0&&indexlength) /* Enums */ typedef enum { QUICKSORT } _l_sort_alg; /* Structures */ struct l_block { struct l_block* next; unsigned char data[]; }; typedef struct { int length; size_t type; int blocksize; struct l_block* block0; } list_t; typedef struct { list_t *list; void* element; int index; } l_iterator; /* Function Pointers */ #define L_P_FUNCTION(name) int lp_ ## name(const list_t *list, int index) #define L_A_FUNCTION(name) void la_ ## name(const list_t *list, int index) #define L_C_FUNCTION(name) int lc_ ## name(const list_t *list, int i1, int i2) typedef int (*_l_predicate)(const list_t *, int); /** Takes (list), (index). Returns non-zero if true **/ typedef int (*_l_comparator)(const list_t*, int,int); /** Takes (list), (i1), (i2). Returns 0 if equal, -1 if goes before , +1 if goes after **/ typedef void (*_l_action)(const list_t*, int); /** Takes (list), (index) **/ /* Functions */ #ifdef __cplusplus extern "C" { #endif list_t* l_create(size_t type,int blocksize); /** Allocates new list_t aligned to size (e.g. "sizeof(int)"), and first block with (use l_create_d(size_t) to use default blocksize) **/ list_t* l_create_from(void* from, int items, size_t type, int bs); /** Allocates new list_t and copies elements from into it **/ void l_destroy(list_t *t); /** Frees all memory alocated to and then frees **/ int l_add(list_t *t, const void* o); /** Adds new element at end of array with copy of data at , then updates ->length pointer and returns index of new element **/ void* l_get(const list_t *t, int index); /** Returns pointer to element at **/ void l_nullify(list_t *t, int index); /** Writes over element at with 0s (bytes) **/ void l_copy(const list_t* src,int so,list_t* de, int o,int len); /** Copies elements from (offset by ) to (offset by ) **/ void l_copy_ar(const list_t* src, int so, void* dest, int len); /** Copies elements from (offset by ) to **/ void* l_toarray_n(const list_t *t, int* size, _l_predicate p); /** Creates a new array and copies each element that satisfies

, or if

is NULL, all of them. Size of the new array is returned in unless is NULL **/ void l_toarray(const list_t *t, void* dest, size_t len); /** Copies elements from to **/ int l_isnull(const list_t *t, int index); /** Returns 1 if all bytes of element at are 0 **/ void l_nullify_if(list_t *t, _l_predicate f); /** Nullifies elements that satisfy **/ void l_do(list_t *t, _l_action act); /** Calls on all elements of **/ void l_do_if(list_t *t, _l_action act, _l_predicate f); /** Calls on elements that satisfy **/ void l_swap(list_t *t, int index1, int index2); /** Swap elements at and **/ void l_swap_e(list_t *t, void* temp, int index1, int index2); /** Swap elements, using specified memory location as temp stoarge (or NULL to alloc) **/ void l_sort(list_t *t, _l_sort_alg alg, _l_comparator comp); /** Sorts with algorithm using comparator **/ list_t* l_clone(const list_t *t); /** Creates new list from **/ list_t* l_clone_if(const list_t *t, _l_predicate f); /** Creates new list from with all elements that satisfy **/ int l_count(const list_t *t, _l_predicate f); /** Count all elements that satisfy , or all if is NULL **/ void* l_push(list_t *t, const void* o); /** Adds new element at end of , updates ->length, and returns pointer to element **/ int l_pop(list_t *t, void* pop_into); /** Removes last element after copying it into (if not NULL), then updates ->length and returns new length. returns -1 if ->length is 0 **/ void* l_pop_n(list_t *t); /** Copies last element into newly malloc'd memory and calls l_pop, returns pointer to new memory. returns NULL if ->length is 0 **/ void l_reverse(list_t *t); /** Reverse all elements in array **/ l_iterator li_create(list_t *list); /** Creates iterator from and initialises at index 0 **/ int li_next(l_iterator *it); /** Updates current element and increments pointer in and returns non-zero if index is in bounds **/ void* li_get(l_iterator *i); /** Updates and returns current element of iterator, or NULL if not in bounds **/ void* li_iterate(l_iterator *i); /** Updates and returns current element of iterator and increments pointer, or returns NULL if not in bounds **/ void* li_set(l_iterator *i,int index); /** Sets current index to then updates and returns current element, or NULL if index is not in bounds **/ /* Predefined Predicates */ L_P_FUNCTION(not_null); /** Allows all non-null elements **/ /* Predefined Actions */ L_A_FUNCTION(assert_zero); /** Call assert(0) **/ /* Predefined Comparators */ L_C_FUNCTION(int_a); /** Ascending sort for (int) type **/ #ifdef __cplusplus } #endif #endif /* _LIST_H */