List manager
- typedef struct _list {
- int l;
- void **v;
- } *list;
A list
is simply an array of pointers. l
contains the number of elements in the list. v
points to an area in memory large enough to hold all the elements in the list.
- extern list list_alloc( void );
- extern list list_init( list this );
- extern list list_new( void );
- extern void list_free( list this );
- extern int list_length( list this );
- extern void *list_get( list this, int key );
- extern void *list_put( list this, int key, void *val );
- extern void *list_delete( list this, int key );
- extern void *list_insert( list this, int key, void *val );
list_new
returns a new empty list.
list_free
deallocates the memory space occupied by a list.
list_length
gives the number of elements contained in a list.
list_get
returns an element of a list.
list_put
, list_insert
and list_delete
modify the content of a list.
- list list_alloc( void ) {
- return (list)calloc( 1, sizeof ( struct _list ));
- }
- list list_init( list this ) {
- return this;
- }
- list list_new( void ) {
- return list_init( list_alloc() );
- }
- void list_free( list this ) {
- if ( this->v )
- free( this->v );
- free( this );
- }
list_new
returns a newly allocated and initialized instance of a list
.
list_alloc
allocates the memory necessary for a list and returns it.
list_init
returns the list this
after it's been initialized.
list_free
deallocates the memory occupied by the array of pointers and the structure of the list this
.
- int list_length( list this ) {
- return this->l;
- }
list_length
returns the number of elements of the list this
.
- void *list_get( list this, int key ) {
- if ( key < 0 )
- return this->l ? this->v[ this->l - 1 ] : 0;
- else if ( key < this->l )
- return this->v[ key ];
- else
- return 0;
- }
list_get
returns the element of the list this
at position key
.
If key
is negative, list_get
returns the last element of the list.
If the list is empty or if key
is larger than the length of the list, list_get
returns 0.
- void *list_put( list this, int key, void *val ) {
- if ( key < 0 )
- key = this->l;
- if ( key < this->l )
- this->v[ key ] = val;
- else {
- this->l = key + 1;
- this->v = (void **)realloc( this->v, (key + 1) * sizeof ( void * ));
- this->v[ key ] = val;
- }
- return val;
- }
list_put
puts the element val
at position key
in the list this
. val
is any pointer.
If key
is negative, val
is added at the end of the list.
If the list is empty or if key
is larger than the length of the list, list_put
allocates enough space to contain key
elements and puts val
at the end of the list.
NOTE: realloc
takes care of copying the data if a new block of memory is allocated.
list_put
returns val
.
- void *list_delete( list this, int key ) {
- void **p, *val;
- if ( key < 0 )
- key = this->l ? this->l - 1 : 0;
- if ( key < this->l ) {
- val = this->v[ key ];
- this->l--;
- for ( p = this->v + key; p < this->v + this->l; p++ )
- *p = *(p+1);
- return val;
- }
- else
- return 0;
- }
list_delete
removes the element in the list this
at position key
.
If key
is negative, list_delete
removes the last element of the list.
list_delete
decrements the length of the list and shifts all the elements of the list to the left from key
+ 1. All the elements after key
change of index.
list_delete
returns the removed element or 0 if the list is empty or if key
larger than the length of the list.
- void *list_insert( list this, int key, void *val ) {
- void **p;
- if ( key < 0 || key >= this->l )
- return list_put( this, key, val );
- this->l++;
- this->v = (void **)realloc( this->v, this->l * sizeof ( void * ));
- for ( p = this->v + (this->l - 1); p > this->v + key; p-- )
- *p = *(p-1);
- this->v[ key ] = val;
- return val;
- }
list_insert
inserts the element val
at position key
in the list this
. val
is any pointer.
If key
is negative or larger than the length of the list, val
is added at the end of the list.
list_insert
increments the length of the list, allocates enough space to hold one more element, shifts all the elements of the list to the right starting at position key
+ 1 and puts val
at position key
. All the elements after key
change of index. NOTE: realloc
takes care of copying the data if a new block of memory is allocated.
list_insert
returns the added element.
Comments