diff --git a/README.md b/README.md index c4094d3..1a3e440 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # list_t -Dynamically allocating list type for C with various functionality (tested on Linux and MinGW). +Dynamically allocating linked block list type for C with various functionality (tested on GNU/Linux and MinGW). diff --git a/build/liblist.a b/build/liblist.a index 8a1068f..ab5e1ff 100644 Binary files a/build/liblist.a and b/build/liblist.a differ diff --git a/build/test.exe b/build/test.exe index 592cb41..8c9fafc 100644 Binary files a/build/test.exe and b/build/test.exe differ diff --git a/include/list.h b/include/list.h index 3b69948..fb4c9bf 100644 --- a/include/list.h +++ b/include/list.h @@ -52,6 +52,7 @@ typedef int (*_l_comparator)(const list_t*, int,int); /** Takes (list), (i1), typedef void (*_l_action)(const list_t*, int); /** Takes (list), (index) **/ /* Functions */ + #ifdef __cplusplus extern "C" { #endif @@ -59,7 +60,7 @@ extern "C" { 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 **/ -void l_add(list_t *t, const void* o); /** Adds new element at end of array with copy of data at , then updates ->length pointer **/ +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 ) **/ @@ -74,7 +75,11 @@ void l_swap(list_t *t, int index1, int index2); /** Swap elements at 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 **/ + 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 **/ diff --git a/obj/list.o b/obj/list.o index 872fc3a..c566ac9 100644 Binary files a/obj/list.o and b/obj/list.o differ diff --git a/obj/sorting.o b/obj/sorting.o index 5ac579b..36eed60 100644 Binary files a/obj/sorting.o and b/obj/sorting.o differ diff --git a/src/list.c b/src/list.c index f8abeaf..a103292 100644 --- a/src/list.c +++ b/src/list.c @@ -2,6 +2,7 @@ #include #include #include +#include #include @@ -114,7 +115,7 @@ static l_block* _l_last_block(l_block* b0) return b0; }*/ /** currently unused **/ -void l_add(list_t *t,const void* o) +int l_add(list_t *t,const void* o) { int real_i = t->length%t->blocksize; int block_skip = t->length/t->blocksize; @@ -129,6 +130,7 @@ void l_add(list_t *t,const void* o) wbl = _l_block_skip(t->block0, block_skip); memcpy(_l_at(wbl, t->type, real_i), o, t->type); t->length+=1; + return t->length-1; } void l_nullify(list_t *t, int index) @@ -266,6 +268,54 @@ list_t* l_clone_if(const list_t *t , _l_predicate f) return n; } +int l_count(const list_t *t, _l_predicate f) +{ + register int i=0; + int r=0; + if(f==NULL) return t->length; + else + for(;ilength;i++) + r += !!f(t,i); + return r; +} + +void *l_push(list_t *t, const void* data) +{ + return l_get(t, l_add(t,data)); +} + +int l_pop(list_t *t, void* pop_into) +{ + if(t->length>0) { + int real_i = (t->length-1)%t->blocksize; + int block_skip = (t->length-1)/t->blocksize; + l_block *wbl= _l_block_skip(t->block0, block_skip); + + if(pop_into!=NULL) + memcpy(pop_into, _l_at(wbl, t->type, real_i), t->type); + + if(real_i==0 && block_skip>0) + { + l_block *last = _l_block_skip(t->block0, block_skip-1); + free(wbl); + last->next = NULL; + } + + t->length-=1; + + return t->length; + } else return -1; +} + +void* l_pop_n(list_t *t) +{ + if(t->length>0) { + void* new = malloc(t->type); + l_pop(t, new); + return new; + } else return NULL; +} + /* Iterator stuff */ int li_next(l_iterator *it) diff --git a/src/test/main.c b/src/test/main.c index ac19838..f2cedb8 100644 --- a/src/test/main.c +++ b/src/test/main.c @@ -9,7 +9,7 @@ void p_show_sorted(const list_t *list); int main() { list_t *list; - int size=100; + int size=10; int i; l_iterator iter; @@ -24,6 +24,7 @@ int main() p_show_sorted(list); + /* Create iterator */ iter = li_create(list);