more functions

added push(const void*) and pop(void*), changed add(const void*) to return index of new element
master
jaob99 7 years ago
parent 237ff9d1ee
commit f0bb14df47

@ -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).

Binary file not shown.

Binary file not shown.

@ -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 <type> (e.g. "sizeof(int)"), and first block with <blocksize> (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 <from> into it **/
void l_destroy(list_t *t); /** Frees all memory alocated to <t> and then frees <t> **/
void l_add(list_t *t, const void* o); /** Adds new element at end of array with copy of data at <o>, 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 <o>, then updates ->length pointer and returns index of new element **/
void* l_get(const list_t *t, int index); /** Returns pointer to element at <index> **/
void l_nullify(list_t *t, int index); /** Writes over element at <index> with 0s (bytes) **/
void l_copy(const list_t* src,int so,list_t* de, int o,int len); /** Copies <len> elements from <src> (offset by <so>) to <de> (offset by <o>) **/
@ -74,6 +75,10 @@ void l_swap(list_t *t, int index1, int index2); /** Swap elements at <index
void l_sort(list_t *t, _l_sort_alg alg, _l_comparator comp); /** Sorts <t> with algorithm <alg> using comparator <comp> **/
list_t* l_clone(const list_t *t); /** Creates new list from <t> **/
list_t* l_clone_if(const list_t *t, _l_predicate f); /** Creates new list from <t> with all elements that satisfy <f> **/
int l_count(const list_t *t, _l_predicate f); /** Count all elements that satisfy <f>, or all if <f> is NULL **/
void* l_push(list_t *t, const void* o); /** Adds new element at end of <t>, updates ->length, and returns pointer to element **/
int l_pop(list_t *t, void* pop_into); /** Removes last element after copying it into <pop_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 <list> and initialises at index 0 **/
int li_next(l_iterator *it); /** Updates current element and increments pointer in <it> and returns non-zero if index is in bounds **/

Binary file not shown.

Binary file not shown.

@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdarg.h>
#include <list.h>
@ -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(;i<t->length;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)

@ -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);

Loading…
Cancel
Save