intmap.c

00001 /*
00002  * Copyright (c) 2010 Jiri Svoboda
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <assert.h>
00038 #include "list.h"
00039 #include "mytypes.h"
00040 
00041 #include "intmap.h"
00042 
00047 void intmap_init(intmap_t *intmap)
00048 {
00049         list_init(&intmap->elem);
00050 }
00051 
00058 void intmap_fini(intmap_t *intmap)
00059 {
00060         list_fini(&intmap->elem);
00061 }
00062 
00073 void intmap_set(intmap_t *intmap, int key, void *value)
00074 {
00075         list_node_t *node;
00076         map_elem_t *elem;
00077 
00078         node = list_first(&intmap->elem);
00079         while (node != NULL) {
00080                 elem = list_node_data(node, map_elem_t *);
00081                 if (elem->key == key) {
00082                         if (value != NULL) {
00083                                 /* Replace existing value. */
00084                                 elem->value = value;
00085                         } else {
00086                                 /* Remove map element. */
00087                                 list_remove(&intmap->elem, node);
00088                                 free(elem);
00089                         }
00090                         return;
00091                 }
00092                 node = list_next(&intmap->elem, node);
00093         }
00094 
00095         /* Allocate new map element and add it to the list. */
00096 
00097         elem = calloc(1, sizeof(map_elem_t));
00098         if (elem == NULL) {
00099                 printf("Memory allocation failed.\n");
00100                 exit(1);
00101         }
00102 
00103         elem->key = key;
00104         elem->value = value;
00105         list_append(&intmap->elem, elem);
00106 }
00107 
00116 void *intmap_get(intmap_t *intmap, int key)
00117 {
00118         list_node_t *node;
00119         map_elem_t *elem;
00120 
00121         node = list_first(&intmap->elem);
00122         while (node != NULL) {
00123                 elem = list_node_data(node, map_elem_t *);
00124                 if (elem->key == key) {
00125                         return elem->value;
00126                 }
00127                 node = list_next(&intmap->elem, node);
00128         }
00129 
00130         /* Not found */
00131         return NULL;
00132 }
00133 
00142 map_elem_t *intmap_first(intmap_t *intmap)
00143 {
00144         list_node_t *node;
00145 
00146         node = list_first(&intmap->elem);
00147         if (node == NULL)
00148                 return NULL;
00149 
00150         return list_node_data(node, map_elem_t *);
00151 }
00152 
00160 int intmap_elem_get_key(map_elem_t *elem)
00161 {
00162         return elem->key;
00163 }
00164 
00172 void *intmap_elem_get_value(map_elem_t *elem)
00173 {
00174         return elem->value;
00175 }

Generated on Thu Jun 2 07:45:42 2011 for HelenOS/USB by  doxygen 1.4.7