about summary refs log tree commit diff
path: root/src/rt/util/hash_map.h
blob: 0ff6ed190d3fc4c06db86e240fb920d1cd06db64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// -*- c++ -*-
/**
 * A C++ wrapper around uthash.
 */

#ifndef HASH_MAP
#define HASH_MAP

#include <assert.h>
#include "../uthash/uthash.h"

template<typename K, typename V> class hash_map {
    struct map_entry {
        K key;
        V value;
        UT_hash_handle hh;
    };
    map_entry * _head;
public:
    hash_map();
    ~hash_map();

    /**
     * Associates a value with the specified key in this hash map.
     * If a mapping already exists the old value is replaced.
     *
     * returns:
     * true if the mapping was successfully created and false otherwise.
     */
    bool put(K key, V value);

    /**
     * Updates the value associated with the specified key in this hash map.
     *
     * returns:
     * true if the value was updated, or false if the key was not found.
     */
    bool set(K key, V value);

    /**
     * Gets the value associated with the specified key in this hash map.
     *
     * returns:
     * true if the value was found and updates the specified *value parameter
     * with the associated value, or false otherwise.
     */
    bool get(K key, V *value);

    /**
     * Removes a key-value pair from this hash map.
     *
     * returns:
     * true if a key-value pair exists and updates the specified
     * *key and *value parameters, or false otherwise.
     */
    bool pop(K *key, V *value);

    /**
     * Checks if the specified key exists in this hash map.
     *
     * returns:
     * true if the specified key exists in this hash map, or false otherwise.
     */
    bool contains(K key);

    /**
     * Removes the value associated with the specified key from this hash map.
     *
     * returns:
     * true if the specified key exists and updates the specified *old_value
     * parameter with the associated value, or false otherwise.
     */
    bool remove(K key, V *old_value);
    bool remove(K key);

    /**
     * Returns the number of key-value pairs in this hash map.
     */
    size_t count();

    bool is_empty() {
        return count() == 0;
    }

    /**
     * Clears all the key-value pairs in this hash map.
     *
     * returns:
     * the number of deleted key-value pairs.
     */
    size_t clear();
};

template<typename K, typename V>
hash_map<K,V>::hash_map() {
    _head = NULL;
}

template<typename K, typename V>
hash_map<K,V>::~hash_map() {
    clear();
}

template<typename K, typename V> bool
hash_map<K,V>::put(K key, V value) {
    if (contains(key)) {
        return set(key, value);
    }
    map_entry *entry = (map_entry *) malloc(sizeof(map_entry));
    entry->key = key;
    entry->value = value;
    HASH_ADD(hh, _head, key, sizeof(K), entry);
    return true;
}

template<typename K, typename V> bool
hash_map<K,V>::get(K key, V *value) {
    map_entry *entry = NULL;
    HASH_FIND(hh, _head, &key, sizeof(K), entry);
    if (entry == NULL) {
        return false;
    }
    *value = entry->value;
    return true;
}

template<typename K, typename V> bool
hash_map<K,V>::set(K key, V value) {
    map_entry *entry = NULL;
    HASH_FIND(hh, _head, &key, sizeof(K), entry);
    if (entry == NULL) {
        return false;
    }
    entry->value = value;
    return true;
}

template<typename K, typename V> bool
hash_map<K,V>::contains(K key) {
    V value;
    return get(key, &value);
}

template<typename K, typename V> bool
hash_map<K,V>::remove(K key, V *old_value) {
    map_entry *entry = NULL;
    HASH_FIND(hh, _head, &key, sizeof(K), entry);
    if (entry == NULL) {
        return false;
    }
    *old_value = entry->value;
    HASH_DEL(_head, entry);
    free(entry);
    return true;
}

template<typename K, typename V> bool
hash_map<K,V>::pop(K *key, V *value) {
    if (is_empty()) {
        return false;
    }
    map_entry *entry = _head;
    HASH_DEL(_head, entry);
    *key = entry->key;
    *value = entry->value;
    free(entry);
    return true;
}

template<typename K, typename V> bool
hash_map<K,V>::remove(K key) {
    V old_value;
    return remove(key, &old_value);
}

template<typename K, typename V> size_t
hash_map<K,V>::count() {
    return HASH_CNT(hh, _head);
}

template<typename K, typename V> size_t
hash_map<K,V>::clear() {
    size_t deleted_entries = 0;
    while (_head != NULL) {
        map_entry *entry = _head;
        HASH_DEL(_head, entry);
        free(entry);
        deleted_entries ++;
    }
    assert(count() == 0);
    return deleted_entries;
}

#endif /* HASH_MAP */