Good day and welcome to the FIRM XMALLOC*() macros. These macros are provided for...
[libfirm] / ir / adt / pmap.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       simplified hashmap for pointer -> pointer mappings
23  * @author      Hubert Schmid
24  * @date        09.06.2002
25  * @version     $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include "pmap.h"
32
33 #include <assert.h>
34 #include "set.h"
35 #include "hashptr.h"
36
37
38 struct pmap {
39         int dummy; /* dummy entry */
40 };
41
42
43 #define INITIAL_SLOTS 64
44
45 /** map a pmap into a set */
46 #define M2S(map)  (set *)(map)
47
48 /**
49  * compare the keys of two entry pairs
50  */
51 static int pmap_entry_cmp(const void *p1, const void *p2, size_t size) {
52         const pmap_entry *entry1 = p1;
53         const pmap_entry *entry2 = p2;
54         (void) size;
55
56         return entry1->key != entry2->key;
57 }
58
59 /* Creates a new empty map with an initial number of slots. */
60 pmap *pmap_create_ex(int slots) {
61         return (pmap *)new_set(pmap_entry_cmp, slots);
62 }
63
64 pmap *pmap_create(void) {
65         return pmap_create_ex(INITIAL_SLOTS);
66 }
67
68 void pmap_destroy(pmap *map) {
69         del_set(M2S(map));
70 }
71
72 void pmap_insert(pmap *map, const void *key, void *value) {
73         pmap_entry entry, *p;
74
75         entry.key = key;
76         p = set_insert(M2S(map), &entry, sizeof(pmap_entry), HASH_PTR(key));
77         p->value = value;
78 }
79
80 int pmap_contains(pmap *map, const void *key) {
81         return set_find(M2S(map), &key, sizeof(pmap_entry), HASH_PTR(key)) != NULL;
82 }
83
84 pmap_entry * pmap_find(pmap *map, const void *key) {
85         return (pmap_entry *)set_find(M2S(map), &key, sizeof(pmap_entry), HASH_PTR(key));
86 }
87
88
89 void * pmap_get(pmap *map, const void *key) {
90         pmap_entry * entry = pmap_find(map, key);
91         return entry == NULL ? NULL : entry->value;
92 }
93
94 int pmap_count(pmap *map) {
95         return set_count(M2S(map));
96 }
97
98 pmap_entry *pmap_first(pmap *map) {
99         return (pmap_entry *) set_first(M2S(map));
100 }
101
102 pmap_entry *pmap_next(pmap *map) {
103         return (pmap_entry *) set_next(M2S(map));
104 }
105
106 void pmap_break(pmap *map) {
107         set_break(M2S(map));
108 }