2 * Copyright (c) 2010 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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).
45 * @param intmap Map to initialize.
47 void intmap_init(intmap_t
*intmap
)
49 list_init(&intmap
->elem
);
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.
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
)
78 node
= list_first(&intmap
->elem
);
79 while (node
!= NULL
) {
80 elem
= list_node_data(node
, map_elem_t
*);
81 if (elem
->key
== key
) {
83 /* Replace existing value. */
86 /* Remove map element. */
87 list_remove(&intmap
->elem
, node
);
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
));
99 printf("Memory allocation failed.\n");
105 list_append(&intmap
->elem
, elem
);
108 /** Get value corresponding to a key.
111 * @param key Key for which to retrieve mapping
113 * @return Value correspoding to @a key or @c NULL if no mapping
116 void *intmap_get(intmap_t
*intmap
, int key
)
121 node
= list_first(&intmap
->elem
);
122 while (node
!= NULL
) {
123 elem
= list_node_data(node
, map_elem_t
*);
124 if (elem
->key
== key
) {
127 node
= list_next(&intmap
->elem
, node
);
134 /** Get first element in the map.
136 * For iterating over the map, this returns the first element (in no
140 * @return Map element or NULL if the map is empty
142 map_elem_t
*intmap_first(intmap_t
*intmap
)
146 node
= list_first(&intmap
->elem
);
150 return list_node_data(node
, map_elem_t
*);
155 * Giver a map element, return the key.
157 * @param elem Map element
160 int intmap_elem_get_key(map_elem_t
*elem
)
165 /** Get element value.
167 * Giver a map element, return the value.
169 * @param elem Map element
172 void *intmap_elem_get_value(map_elem_t
*elem
)