forked from Zeda/spasm-ng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhash.cpp
82 lines (69 loc) · 1.85 KB
/
hash.cpp
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
#include "stdafx.h"
#include "spasm.h"
#include "hash.h"
#include "storage.h"
hash_t::hash_t(void (*remove_callback)(void *)) : htt_base() {
this->remove_callback = remove_callback;
}
/*
* Creates a new hash table with an upper bound size
*/
hash_t *hash_init (void remove_callback(void *)) {
hash_t *ht = new hash_t(remove_callback);
return ht;
}
/*
* Inserts a value into a hash table.
*
* `store` is the value to be taken, where its first element is assumed
* to be a store_t providing the key.
*/
void hash_insert (hash_t *ht, void *store) {
store_t *key = (store_t *)store;
(*ht)[key->name] = store;
}
/*
* Looks up a value from a hash table
* returns NULL if not found
*/
void *hash_lookup (hash_t *ht, const char *name) {
hash_t::iterator location = ht->find(name);
if (location == ht->end()) {
return NULL;
} else {
return location->second;
}
}
/*
* Removes a hash from a hash table
*
* Returns 1 on success, or 0 if the name doesn't exist.
*/
int hash_remove (hash_t *ht, const char *name) {
hash_t::iterator location = ht->find(name);
if (location == ht->end()) {
return 0;
}
ht->remove_callback(location->second);
ht->erase(location);
return 1;
}
void hash_enum (hash_t *ht, void enum_callback(void *, void *), void *arg) {
for (hash_t::iterator i = ht->begin(); i != ht->end(); i++) {
enum_callback(i->second, arg);
}
}
int hash_count (hash_t *ht) {
return ht->size();
}
/*
* Free a hash table, removing elements
*/
void hash_free (hash_t *ht) {
// Invoke all the remove callbacks.
// This invalidates all of the map's contents.
for (hash_t::iterator i = ht->begin(); i != ht->end(); i++) {
ht->remove_callback(i->second);
}
delete ht;
}