Fixed some typos.
[libfirm] / ir / adt / pmap.c
1 /*
2  * Copyright (C) 1995-2011 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  */
26 #include "config.h"
27
28 #include "pmap.h"
29
30 #include <assert.h>
31 #include "set.h"
32 #include "hashptr.h"
33
34
35 struct pmap {
36         int dummy; /* dummy entry */
37 };
38
39
40 #define INITIAL_SLOTS 64
41
42 /** map a pmap into a set */
43 #define M2S(map)  (set *)(map)
44
45 /**
46  * compare the keys of two entry pairs
47  */
48 static int pmap_entry_cmp(const void *p1, const void *p2, size_t size)
49 {
50         const pmap_entry *entry1 = (const pmap_entry*) p1;
51         const pmap_entry *entry2 = (const pmap_entry*) p2;
52         (void) size;
53
54         return entry1->key != entry2->key;
55 }
56
57 /* Creates a new empty map with an initial number of slots. */
58 pmap *pmap_create_ex(size_t slots)
59 {
60         return (pmap *)new_set(pmap_entry_cmp, slots);
61 }
62
63 pmap *pmap_create(void)
64 {
65         return pmap_create_ex(INITIAL_SLOTS);
66 }
67
68 void pmap_destroy(pmap *map)
69 {
70         del_set(M2S(map));
71 }
72
73 void pmap_insert(pmap *map, const void *key, void *value)
74 {
75         pmap_entry entry, *p;
76
77         entry.key = key;
78         p = set_insert(pmap_entry, M2S(map), &entry, sizeof(pmap_entry), hash_ptr(key));
79         p->value = value;
80 }
81
82 int pmap_contains(pmap *map, const void *key)
83 {
84         return pmap_find(map, key) != NULL;
85 }
86
87 pmap_entry * pmap_find(pmap *map, const void *key)
88 {
89         pmap_entry const entry = { key, 0 };
90         return set_find(pmap_entry, M2S(map), &entry, sizeof(entry), hash_ptr(key));
91 }
92
93
94 void * (pmap_get)(pmap *map, const void *key)
95 {
96         pmap_entry * entry = pmap_find(map, key);
97         return entry == NULL ? NULL : entry->value;
98 }
99
100 size_t pmap_count(pmap *map)
101 {
102         return set_count(M2S(map));
103 }
104
105 pmap_entry *pmap_first(pmap *map)
106 {
107         return set_first(pmap_entry, M2S(map));
108 }
109
110 pmap_entry *pmap_next(pmap *map)
111 {
112         return set_next(pmap_entry, M2S(map));
113 }
114
115 void pmap_break(pmap *map)
116 {
117         set_break(M2S(map));
118 }