/Users/maurits/Documents/studie/afstuderen/biosphere/common/list.c

Go to the documentation of this file.
00001 /*
00002  * Author: MA Hartman
00003  * Date: feb 13, 2007
00004  * 
00005  * Function:
00006  * A general purpose list implementation.
00007  * 
00008  * License information:
00009  * 
00010  * Copyright (c) 2006 Maurits Hartman
00011  *
00012  * Permission is hereby granted, free of charge, to any person
00013  * obtaining a copy of this software and associated documentation
00014  * files (the "Software"), to deal in the Software without
00015  * restriction, including without limitation the rights to use,
00016  * copy, modify, merge, publish, distribute, sublicense, and/or sell
00017  * copies of the Software, and to permit persons to whom the
00018  * Software is furnished to do so, subject to the following
00019  * conditions:
00020  * 
00021  * The above copyright notice and this permission notice shall be
00022  * included in all copies or substantial portions of the Software.
00023  * 
00024  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
00026  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
00028  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
00029  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00030  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00031  * OTHER DEALINGS IN THE SOFTWARE.
00032  * 
00033  */
00034 
00035 #include "list.h"
00036 #include "alloc.h"
00037 
00038 #include <assert.h>
00039 #include <apr_pools.h>
00040 
00041 #define INITIAL_LIST_SIZE       16
00042 
00043 
00044 bs_list *new_list(apr_pool_t *mp)
00045 {
00046         apr_status_t rv;
00047         apr_pool_t *pool;
00048         bs_list *list = NULL;
00049         
00050         /* Create a subpool for this list and its data: */
00051         rv = apr_pool_create(&pool, mp);
00052         if (rv != APR_SUCCESS) return NULL;
00053         
00054         /* Initialize data members: */
00055     list                        = apr_pcalloc(pool, sizeof(bs_list));
00056     list->size      = 0;
00057     list->allocated = INITIAL_LIST_SIZE;
00058     list->pool          = pool;
00059     list->data      = apr_pcalloc(pool, sizeof(void * [INITIAL_LIST_SIZE]));
00060     
00061     return list;
00062 }
00063 
00064 
00065 void delete_list(bs_list *list)
00066 {
00067     if (list != NULL) {
00068         apr_pool_destroy(list->pool);
00069     }
00070 }
00071 
00072 
00073 void list_remove(bs_list *l, unsigned index)
00074 {
00075         unsigned i;
00076         assert(l != NULL && index < l->size);
00077         
00078         for (i = index + 1; i < l->size; i++) {
00079                 l->data[i - 1] = l->data[i];
00080         }
00081         l->size--;
00082 }
00083 
00084 
00085 void list_append(bs_list *list, void *data)
00086 {
00087         assert(list != NULL);
00088         
00089     if (list->size == list->allocated) {
00090         list->allocated *= 2;
00091         list->data = pool_realloc(list->data,
00092                         list->size,
00093                         sizeof(void *) * list->allocated,
00094                         list->pool);
00095     }
00096 
00097     list->data[list->size++] = data;
00098 }
00099 
00100 
00101 void list_insert(bs_list *l, unsigned index, void *data)
00102 {
00103         assert(l != NULL);
00104         if (index >= l->size) list_append(l, data);
00105         else {
00106                 unsigned i;
00107                 
00108                 /* Reserve some space if necessary: */
00109                 if (l->size == l->allocated) {
00110                 l->allocated *= 2;
00111                 l->data = pool_realloc(l->data, l->size,
00112                                 sizeof(void *) * l->allocated, l->pool);
00113         }
00114         
00115         /* Insert: */
00116         for (i = l->size; i > index; i--) l->data[i] = l->data[i - 1];
00117         l->data[index] = data;
00118         }
00119 }
00120 
00121 
00122 void list_merge(bs_list *list_1, bs_list *list_2)
00123 {
00124     int i;
00125     assert(list_1 != NULL && list_2 != NULL);
00126 
00127     for (i = 0; i < list_2->size; i++)
00128         list_append(list_1, list_2->data [i]);
00129 
00130     delete_list(list_2);
00131 }
00132 
00133 
00134 inline unsigned list_size(const bs_list *list)
00135 {
00136         assert(list != NULL);
00137     return list->size;
00138 }
00139 
00140 
00141 inline void *list_index(const bs_list *list, unsigned index)
00142 {
00143     assert(list != NULL && index < list->size);
00144     return list->data[index];
00145 }

Generated on Tue Jul 17 09:50:52 2007 for Bio-SPHERE by  doxygen 1.5.1