22

List manager

  1. typedef struct _list {
  2.     int l;
  3.     void **v;
  4. } *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.

  1. extern list list_alloc( void );
  2. extern list list_init( list this );
  3. extern list list_new( void );
  4. extern void list_free( list this );
  5. extern int list_length( list this );
  6. extern void *list_get( list this, int key );
  7. extern void *list_put( list this, int key, void *val );
  8. extern void *list_delete( list this, int key );
  9. 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.

  1. list list_alloc( void ) {
  2.     return (list)calloc( 1, sizeof ( struct _list ));
  3. }
  4.  
  5. list list_init( list this ) {
  6.     return this;
  7. }
  8.  
  9. list list_new( void ) {
  10.     return list_init( list_alloc() );
  11. }
  12.  
  13. void list_free( list this ) {
  14.     if ( this->v )
  15.         free( this->v );
  16.     free( this );
  17. }

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.

  1. int list_length( list this ) {
  2.     return this->l;
  3. }

list_length returns the number of elements of the list this.

  1. void *list_get( list this, int key ) {
  2.     if ( key < 0 )
  3.         return this->l ? this->v[ this->l - 1 ] : 0;
  4.     else if ( key < this->l )
  5.         return this->v[ key ];
  6.     else
  7.         return 0;
  8. }

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.

  1. void *list_put( list this, int key, void *val ) {
  2.     if ( key < 0 )
  3.         key = this->l;
  4.     if ( key < this->l )
  5.         this->v[ key ] = val;
  6.     else {
  7.         this->l = key + 1;
  8.         this->v = (void **)realloc( this->v, (key + 1) * sizeof ( void * ));
  9.         this->v[ key ] = val;
  10.     }
  11.  
  12.     return val;
  13. }

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.

  1. void *list_delete( list this, int key ) {
  2.     void **p, *val;
  3.  
  4.     if ( key < 0 )
  5.         key = this->l ? this->l - 1 : 0;
  6.     if ( key < this->l ) {
  7.         val = this->v[ key ];
  8.         this->l--;
  9.         for ( p = this->v + key; p < this->v + this->l; p++ )
  10.             *p = *(p+1);
  11.         return val;
  12.     }
  13.     else
  14.         return 0;
  15. }

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.

  1. void *list_insert( list this, int key, void *val ) {
  2.     void **p;
  3.  
  4.     if ( key < 0 || key >= this->l )
  5.         return list_put( this, key, val );
  6.  
  7.     this->l++;
  8.     this->v = (void **)realloc( this->v, this->l * sizeof ( void * ));
  9.  
  10.     for ( p = this->v + (this->l - 1); p > this->v + key; p-- )
  11.         *p = *(p-1);
  12.  
  13.     this->v[ key ] = val;
  14.  
  15.     return val;
  16. }

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

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].