2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Simplified hashmap for pointer->pointer relations
9 * @author Hubert Schmid
12 #ifndef FIRM_ADT_PMAP_H
13 #define FIRM_ADT_PMAP_H
21 * @defgroup pmap Pointer Map
22 * Pointer->Pointer hashmap
26 /** A map which maps addresses to addresses. */
27 typedef struct pmap pmap;
32 typedef struct pmap_entry {
33 const void *key; /**< The key. */
34 void *value; /**< The value. */
38 /** Creates a new empty map. */
39 FIRM_API pmap *pmap_create(void);
41 /** Creates a new empty map with an initial number of slots. */
42 FIRM_API pmap *pmap_create_ex(size_t slots);
45 FIRM_API void pmap_destroy(pmap *);
48 * Inserts a pair (key,value) into the map. If an entry with key
49 * "key" already exists, its "value" is overwritten.
51 FIRM_API void pmap_insert(pmap *map, const void * key, void * value);
53 /** Checks if an entry with key "key" exists. */
54 FIRM_API int pmap_contains(pmap *map, const void * key);
56 /** Returns the key, value pair of "key". */
57 FIRM_API pmap_entry *pmap_find(pmap *map, const void * key);
59 /** Returns the value of "key". */
60 FIRM_API void * pmap_get(pmap *map, const void * key);
63 * Returns the value of "key".
64 * This is a wrapper for pmap_get(map, key); It allows to express the
65 * intended type of the set elements (instead of weakly typed void*).
67 #define pmap_get(type, map, key) ((type*)pmap_get(map, key))
69 /** Return number of elements in the map */
70 FIRM_API size_t pmap_count(pmap *map);
73 * Returns the first entry of a map if the map is not empty.
75 FIRM_API pmap_entry *pmap_first(pmap *map);
78 * Returns the next entry of a map or NULL if all entries were visited.
80 FIRM_API pmap_entry *pmap_next(pmap *);
83 * Iterate over all elements in the map setting curr to the current element.
85 #define foreach_pmap(pmap, curr) \
86 for (curr = pmap_first(pmap); curr; curr = pmap_next(pmap))
88 /** Breaks an iteration.
89 * Must be called, if a iteration ends before p_map_next() returns NULL.
91 FIRM_API void pmap_break(pmap *map);