/Users/maurits/Documents/studie/afstuderen/biosphere/common/dict.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 dictionary implementation with
00007  * almost constant time get operations.
00008  * 
00009  * License information:
00010  * 
00011  * Copyright (c) 2006 Maurits Hartman
00012  *
00013  * Permission is hereby granted, free of charge, to any person
00014  * obtaining a copy of this software and associated documentation
00015  * files (the "Software"), to deal in the Software without
00016  * restriction, including without limitation the rights to use,
00017  * copy, modify, merge, publish, distribute, sublicense, and/or sell
00018  * copies of the Software, and to permit persons to whom the
00019  * Software is furnished to do so, subject to the following
00020  * conditions:
00021  * 
00022  * The above copyright notice and this permission notice shall be
00023  * included in all copies or substantial portions of the Software.
00024  * 
00025  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00026  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
00027  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00028  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
00029  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
00030  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00031  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00032  * OTHER DEALINGS IN THE SOFTWARE.
00033  * 
00034  */
00035 
00036 #include "dict.h"
00037 #include "str.h"
00038 
00039 #include <apr_strings.h>
00040 #include <assert.h>
00041 
00042 
00043 bs_dict *new_dict(apr_pool_t *mp)
00044 {
00045         apr_status_t rv;
00046         apr_pool_t *pool;
00047         bs_dict *dict = NULL;
00048         
00049         /* Create a subpool for this dict and its data: */
00050         rv = apr_pool_create(&pool, mp);
00051         if (rv != APR_SUCCESS) return NULL;
00052         
00053         /* Initialize data members (note that bucket does not have
00054          * to be malloced: */
00055     dict                        = apr_pcalloc(pool, sizeof(bs_dict));
00056     dict->size      = 0;
00057     dict->pool          = pool;
00058     
00059     return dict;        
00060 }
00061 
00062 
00063 void delete_dict(bs_dict *dict)
00064 {
00065     if (dict != NULL) {
00066         apr_pool_destroy(dict->pool);
00067     }   
00068 }
00069 
00070 
00071 void *dict_get(const bs_dict *dict, const char *key)
00072 {
00073         unsigned char c;
00074         assert(dict && key);
00075         
00076         c = key[0];
00077         if (dict->bucket[c]) {
00078                 unsigned i;
00079                 bs_list *tmp = dict->bucket[c];
00080                 for (i = 0; i < list_size(tmp); i++) {
00081                         bs_dict_item *item = (bs_dict_item *) list_index(tmp, i);
00082                         if (streq(key, item->key)) return item->data;
00083                 }
00084         }
00085         return NULL;
00086 }
00087 
00088 
00089 bs_bool dict_add(bs_dict *dict, const char *key, void *data)
00090 {
00091         unsigned char c;
00092         unsigned i;
00093         bs_dict_item *item;
00094         assert(dict && key);
00095         
00096         /* See if the key is not already taken: */
00097         c = key[0];
00098         if (!dict->bucket[c]) dict->bucket[c] = new_list(dict->pool);
00099         for (i = 0; i < list_size(dict->bucket[c]); i++) {
00100                 item = (bs_dict_item *) list_index(dict->bucket[c], i);
00101                 if (streq(key, item->key)) return FALSE;
00102         }
00103         
00104         /* Insert: */
00105         item = apr_palloc(dict->pool, sizeof(bs_dict_item));
00106         item->key = apr_pstrdup(dict->pool, key);
00107         item->data = data;
00108         dict->size++;
00109         return TRUE;
00110 }
00111 
00112 
00113 void dict_remove(bs_dict *dict, const char *key)
00114 {
00115         unsigned char c;
00116         assert(dict);
00117         
00118         c = key[0];
00119         if (dict->bucket[c]) {
00120                 unsigned i;
00121                 for (i = 0; i < list_size(dict->bucket[c]); i++) {
00122                         bs_dict_item *item;
00123                         item = (bs_dict_item *) list_index(dict->bucket[c], i);
00124                         if (streq(key, item->key)) {
00125                                 list_remove(dict->bucket[c], i);
00126                                 dict->size--;
00127                                 return; 
00128                         }
00129                 }
00130         }
00131 }
00132 
00133 
00134 inline unsigned dict_size(const bs_dict *dict)
00135 {
00136         assert(dict);
00137         return dict->size;      
00138 }

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