Update GCC and binutils to latest versions
[helenos.git] / uspace / app / sbi / src / intmap.c
blobf7d1a5ccaad9d44b709dce00024aa51e0a052d9c
1 /*
2 * Copyright (c) 2010 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /** @file Integer map.
31 * Maps integers to pointers (void *). Current implementation is trivial
32 * (linked list of key-value pairs).
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <assert.h>
38 #include "list.h"
39 #include "mytypes.h"
41 #include "intmap.h"
43 /** Initialize map.
45 * @param intmap Map to initialize.
47 void intmap_init(intmap_t *intmap)
49 list_init(&intmap->elem);
52 /** Deinitialize map.
54 * The map must be already empty.
56 * @param intmap Map to initialize.
58 void intmap_fini(intmap_t *intmap)
60 list_fini(&intmap->elem);
63 /** Set value corresponding to a key.
65 * If there already exists a mapping for @a key in the map, it is
66 * silently replaced. If @a value is @c NULL, the mapping for @a key
67 * is removed from the map.
69 * @param intmap Map
70 * @param key Key (integer)
71 * @param value Value (must be a pointer) or @c NULL
73 void intmap_set(intmap_t *intmap, int key, void *value)
75 list_node_t *node;
76 map_elem_t *elem;
78 node = list_first(&intmap->elem);
79 while (node != NULL) {
80 elem = list_node_data(node, map_elem_t *);
81 if (elem->key == key) {
82 if (value != NULL) {
83 /* Replace existing value. */
84 elem->value = value;
85 } else {
86 /* Remove map element. */
87 list_remove(&intmap->elem, node);
88 free(elem);
90 return;
92 node = list_next(&intmap->elem, node);
95 /* Allocate new map element and add it to the list. */
97 elem = calloc(1, sizeof(map_elem_t));
98 if (elem == NULL) {
99 printf("Memory allocation failed.\n");
100 exit(1);
103 elem->key = key;
104 elem->value = value;
105 list_append(&intmap->elem, elem);
108 /** Get value corresponding to a key.
110 * @param intmap Map
111 * @param key Key for which to retrieve mapping
113 * @return Value correspoding to @a key or @c NULL if no mapping
114 * exists.
116 void *intmap_get(intmap_t *intmap, int key)
118 list_node_t *node;
119 map_elem_t *elem;
121 node = list_first(&intmap->elem);
122 while (node != NULL) {
123 elem = list_node_data(node, map_elem_t *);
124 if (elem->key == key) {
125 return elem->value;
127 node = list_next(&intmap->elem, node);
130 /* Not found */
131 return NULL;
134 /** Get first element in the map.
136 * For iterating over the map, this returns the first element (in no
137 * particular order).
139 * @param intmap Map
140 * @return Map element or NULL if the map is empty
142 map_elem_t *intmap_first(intmap_t *intmap)
144 list_node_t *node;
146 node = list_first(&intmap->elem);
147 if (node == NULL)
148 return NULL;
150 return list_node_data(node, map_elem_t *);
153 /** Get element key.
155 * Giver a map element, return the key.
157 * @param elem Map element
158 * @return Key
160 int intmap_elem_get_key(map_elem_t *elem)
162 return elem->key;
165 /** Get element value.
167 * Giver a map element, return the value.
169 * @param elem Map element
170 * @return Value
172 void *intmap_elem_get_value(map_elem_t *elem)
174 return elem->value;