forked from capythulhu/coxinhacoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashmap.h
141 lines (116 loc) · 2.89 KB
/
hashmap.h
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
#ifndef HASHMAP_H
#define HASHMAP_H
#ifndef STDLIB_H
#define STDLIB_H
#include <stdlib.h>
#endif
#ifndef STDBOOL_H
#define STDBOOL_H
#include <stdbool.h>
#endif
#include "bytes.h"
// Nó do hashmap
typedef struct _hashnode{
buffer key;
void *val;
struct _hashnode *next;
} hashnode;
// Hashmap
typedef struct _hashmap{
unsigned size;
struct _hashnode *first;
} hashmap;
hashmap *new_hashmap(void);
hashnode *new_hashnode(buffer key, void *val);
bool put_hashnode(hashmap *h, hashnode *n);
bool rem_key_from_hashmap(hashmap *h, buffer key);
void *get_val_from_hashmap(hashmap *h, buffer key);
bool put_val_on_hashmap(hashmap *h, buffer key, void *val);
// Novo hashmap
hashmap *new_hashmap(void) {
hashmap *output = malloc(sizeof(hashmap));
output->size = 0;
output->first = NULL;
return output;
}
// Novo nó do hashmap
hashnode *new_hashnode(buffer key, void *val) {
hashnode *output = malloc(sizeof(hashnode));
output->key = key;
output->val = val;
output->next = NULL;
return output;
}
// Insere nó no hashmap
bool put_hashnode(hashmap *h, hashnode *n) {
if(!n || !h) return false;
hashnode *temp = h->first;
if(h->size <= 0 || !temp) {
h->first = n;
h->size = 1;
return true;
}
while(temp->next) {
if(compare_buffer(temp->key, n->key)) return false;
temp = temp->next;
}
temp->next = n;
h->size++;
return true;
}
// Remove nó do hashmap
bool rem_key_from_hashmap(hashmap *h, buffer key) {
if(!h) return false;
hashnode *temp = h->first;
if(h->size <= 0 || !temp) return false;
if(compare_buffer(temp->key, key)) {
h->first = temp->next;
free(temp);
h->size--;
return true;
}
if(!temp->next) return false;
hashnode *prev;
do {
prev = temp;
temp = temp->next;
if(compare_buffer(temp->key, key)) {
prev->next = temp->next;
free(temp);
h->size--;
return true;
}
} while(temp->next);
return false;
}
// Obtém um valor a partir da chave associada a ele
void *get_val_from_hashmap(hashmap *h, buffer key) {
if(!h) return NULL;
hashnode *temp = h->first;
if(!temp) return NULL;
while(temp->next) {
if(compare_buffer(temp->key, key)) return temp->val;
temp = temp->next;
}
if(compare_buffer(temp->key, key)) return temp->val;
return NULL;
}
// Insere o nó no hashmap
bool put_val_on_hashmap(hashmap *h, buffer key, void *val) {
if(!h) return false;
hashnode *n = malloc(sizeof(hashnode));
n->key = key;
n->val = val;
n->next = NULL;
hashnode *temp = h->first;
if(h->size <= 0 || !temp) {
h->first = n;
h->size = 1;
return true;
}
while(temp->next) temp = temp->next;
temp->next = n;
h->size++;
return true;
}
#endif