| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Jiri Svoboda
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 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.
|
|---|
| 16 | *
|
|---|
| 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.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @file Integer map.
|
|---|
| 30 | *
|
|---|
| 31 | * Maps integers to pointers (void *). Current implementation is trivial
|
|---|
| 32 | * (linked list of key-value pairs).
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <stdio.h>
|
|---|
| 36 | #include <stdlib.h>
|
|---|
| 37 | #include <assert.h>
|
|---|
| 38 | #include "list.h"
|
|---|
| 39 | #include "mytypes.h"
|
|---|
| 40 |
|
|---|
| 41 | #include "intmap.h"
|
|---|
| 42 |
|
|---|
| 43 | /** Initialize map.
|
|---|
| 44 | *
|
|---|
| 45 | * @param intmap Map to initialize.
|
|---|
| 46 | */
|
|---|
| 47 | void intmap_init(intmap_t *intmap)
|
|---|
| 48 | {
|
|---|
| 49 | list_init(&intmap->elem);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | /** Deinitialize map.
|
|---|
| 53 | *
|
|---|
| 54 | * The map must be already empty.
|
|---|
| 55 | *
|
|---|
| 56 | * @param intmap Map to initialize.
|
|---|
| 57 | */
|
|---|
| 58 | void intmap_fini(intmap_t *intmap)
|
|---|
| 59 | {
|
|---|
| 60 | list_fini(&intmap->elem);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | /** Set value corresponding to a key.
|
|---|
| 64 | *
|
|---|
| 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.
|
|---|
| 68 | *
|
|---|
| 69 | * @param intmap Map
|
|---|
| 70 | * @param key Key (integer)
|
|---|
| 71 | * @param value Value (must be a pointer) or @c NULL
|
|---|
| 72 | */
|
|---|
| 73 | void intmap_set(intmap_t *intmap, int key, void *value)
|
|---|
| 74 | {
|
|---|
| 75 | list_node_t *node;
|
|---|
| 76 | map_elem_t *elem;
|
|---|
| 77 |
|
|---|
| 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);
|
|---|
| 89 | }
|
|---|
| 90 | return;
|
|---|
| 91 | }
|
|---|
| 92 | node = list_next(&intmap->elem, node);
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /* Allocate new map element and add it to the list. */
|
|---|
| 96 |
|
|---|
| 97 | elem = calloc(1, sizeof(map_elem_t));
|
|---|
| 98 | if (elem == NULL) {
|
|---|
| 99 | printf("Memory allocation failed.\n");
|
|---|
| 100 | exit(1);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | elem->key = key;
|
|---|
| 104 | elem->value = value;
|
|---|
| 105 | list_append(&intmap->elem, elem);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /** Get value corresponding to a key.
|
|---|
| 109 | *
|
|---|
| 110 | * @param intmap Map
|
|---|
| 111 | * @param key Key for which to retrieve mapping
|
|---|
| 112 | *
|
|---|
| 113 | * @return Value correspoding to @a key or @c NULL if no mapping
|
|---|
| 114 | * exists.
|
|---|
| 115 | */
|
|---|
| 116 | void *intmap_get(intmap_t *intmap, int key)
|
|---|
| 117 | {
|
|---|
| 118 | list_node_t *node;
|
|---|
| 119 | map_elem_t *elem;
|
|---|
| 120 |
|
|---|
| 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;
|
|---|
| 126 | }
|
|---|
| 127 | node = list_next(&intmap->elem, node);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | /* Not found */
|
|---|
| 131 | return NULL;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /** Get first element in the map.
|
|---|
| 135 | *
|
|---|
| 136 | * For iterating over the map, this returns the first element (in no
|
|---|
| 137 | * particular order).
|
|---|
| 138 | *
|
|---|
| 139 | * @param intmap Map
|
|---|
| 140 | * @return Map element or NULL if the map is empty
|
|---|
| 141 | */
|
|---|
| 142 | map_elem_t *intmap_first(intmap_t *intmap)
|
|---|
| 143 | {
|
|---|
| 144 | list_node_t *node;
|
|---|
| 145 |
|
|---|
| 146 | node = list_first(&intmap->elem);
|
|---|
| 147 | if (node == NULL)
|
|---|
| 148 | return NULL;
|
|---|
| 149 |
|
|---|
| 150 | return list_node_data(node, map_elem_t *);
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | /** Get element key.
|
|---|
| 154 | *
|
|---|
| 155 | * Giver a map element, return the key.
|
|---|
| 156 | *
|
|---|
| 157 | * @param elem Map element
|
|---|
| 158 | * @return Key
|
|---|
| 159 | */
|
|---|
| 160 | int intmap_elem_get_key(map_elem_t *elem)
|
|---|
| 161 | {
|
|---|
| 162 | return elem->key;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /** Get element value.
|
|---|
| 166 | *
|
|---|
| 167 | * Giver a map element, return the value.
|
|---|
| 168 | *
|
|---|
| 169 | * @param elem Map element
|
|---|
| 170 | * @return Value
|
|---|
| 171 | */
|
|---|
| 172 | void *intmap_elem_get_value(map_elem_t *elem)
|
|---|
| 173 | {
|
|---|
| 174 | return elem->value;
|
|---|
| 175 | }
|
|---|