2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief simplified hashmap for pointer -> pointer mappings
23 * @author Hubert Schmid
36 int dummy; /* dummy entry */
40 #define INITIAL_SLOTS 64
42 /** map a pmap into a set */
43 #define M2S(map) (set *)(map)
46 * compare the keys of two entry pairs
48 static int pmap_entry_cmp(const void *p1, const void *p2, size_t size)
50 const pmap_entry *entry1 = (const pmap_entry*) p1;
51 const pmap_entry *entry2 = (const pmap_entry*) p2;
54 return entry1->key != entry2->key;
57 /* Creates a new empty map with an initial number of slots. */
58 pmap *pmap_create_ex(size_t slots)
60 return (pmap *)new_set(pmap_entry_cmp, slots);
63 pmap *pmap_create(void)
65 return pmap_create_ex(INITIAL_SLOTS);
68 void pmap_destroy(pmap *map)
73 void pmap_insert(pmap *map, const void *key, void *value)
78 p = (pmap_entry*) set_insert(M2S(map), &entry, sizeof(pmap_entry), hash_ptr(key));
82 int pmap_contains(pmap *map, const void *key)
84 return set_find(M2S(map), &key, sizeof(pmap_entry), hash_ptr(key)) != NULL;
87 pmap_entry * pmap_find(pmap *map, const void *key)
89 return (pmap_entry *)set_find(M2S(map), &key, sizeof(pmap_entry), hash_ptr(key));
93 void * pmap_get(pmap *map, const void *key)
95 pmap_entry * entry = pmap_find(map, key);
96 return entry == NULL ? NULL : entry->value;
99 size_t pmap_count(pmap *map)
101 return set_count(M2S(map));
104 pmap_entry *pmap_first(pmap *map)
106 return (pmap_entry *) set_first(M2S(map));
109 pmap_entry *pmap_next(pmap *map)
111 return (pmap_entry *) set_next(M2S(map));
114 void pmap_break(pmap *map)