89bbda93237ed3d68d1f62daf2a479be22c60ada
[libfirm] / ir / adt / pmap.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/adt/eset.c
4  * Purpose:     Datentyp: Vereinfachte Map (hash-map) zum Speichern von
5  *              Zeigern/Adressen -> Zeigern/Adressen.
6  * Author:      Hubert Schmid
7  * Modified by:
8  * Created:     09.06.2002
9  * CVS-ID:      $Id$
10  * Copyright:   (c) 2002 Universität Karlsruhe
11  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
12  */
13
14 #include "pmap.h"
15
16 #include <assert.h>
17 #include "set.h"
18
19
20 struct pmap {
21   int dummy; /* dummy entry */
22 };
23
24
25 static const int INITIAL_SLOTS = 64;
26
27
28 static int pmap_entry_cmp(const pmap_entry * entry1, const pmap_entry * entry2, size_t size) {
29   return entry1->key == entry2->key ? 0 : 1;
30 }
31
32
33 pmap * pmap_create(void) {
34   return (pmap *) new_set((set_cmp_fun) pmap_entry_cmp, INITIAL_SLOTS);
35 }
36
37
38 void pmap_destroy(pmap * map) {
39   del_set((set *) map);
40 }
41
42
43 void pmap_insert(pmap * map, void * key, void * value) {
44   if (pmap_contains(map, key)) {
45     pmap_entry * entry = pmap_find(map, key);
46     entry->value = value;
47   } else {
48     pmap_entry entry;
49     entry.key = key;
50     entry.value = value;
51     set_insert((set *) map, &entry, sizeof(pmap_entry), (unsigned) key);
52   }
53 }
54
55
56 bool pmap_contains(pmap * map, void * key) {
57   return set_find((set *) map, &key, sizeof(pmap_entry), (unsigned) key) != NULL;
58 }
59
60
61 pmap_entry * pmap_find(pmap * map, void * key) {
62   return (pmap_entry *) set_find((set *) map, &key, sizeof(pmap_entry), (unsigned) key);
63 }
64
65
66 void * pmap_get(pmap * map, void * key) {
67   pmap_entry * entry = pmap_find(map, key);
68   return entry == NULL ? NULL : entry->value;
69 }
70
71
72 pmap_entry * pmap_first(pmap * map) {
73   return (pmap_entry *) set_first((set *) map);
74 }
75
76
77 pmap_entry * pmap_next(pmap * map) {
78   return (pmap_entry *) set_next((set *) map);
79 }