-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringmap.h
203 lines (159 loc) · 4 KB
/
stringmap.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
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
195
196
197
198
199
200
201
202
203
#ifndef STRINGMAP_H
#define STRINGMAP_H
#pragma once
#include <string.h>
#include <new>
#include "hash.h"
#include "strings.h"
#include "macros.h"
template<typename T>
struct StringMap{
Hash* hashes;
String* names;
T* values;
int capacity;
int count;
StringMap(){
hashes = nullptr;
names = nullptr;
values = nullptr;
capacity = 0;
count = 0;
}
StringMap(const StringMap& orig){
capacity = 0;
count = 0;
hashes = nullptr;
names = nullptr;
values = nullptr;
Assign(orig);
}
~StringMap() {
Destroy();
}
StringMap& operator=(const StringMap& orig) {
Clear();
Assign(orig);
return *this;
}
void Assign(const StringMap& orig){
EnsureCapacity(orig.capacity);
count = orig.count;
for(int i = 0; i < count; i++){
new (&values[i]) T(orig.values[i]);
}
for(int i = 0; i < count; i++){
hashes[i] = orig.hashes[i];
}
for(int i = 0; i < count; i++){
new (&names[i]) String(orig.names[i]);
}
}
void EnsureCapacity(int newCapacity){
if(capacity < newCapacity){
T* newValues = (T*)malloc(newCapacity*sizeof(T));
String* newNames = (String*)malloc(newCapacity*sizeof(String));
Hash* newHashes = (Hash*)malloc(newCapacity*sizeof(Hash));
BNS_MEMCPY(newHashes, hashes, count * sizeof(Hash));
BNS_MEMCPY(newNames, names, count * sizeof(String));
BNS_MEMCPY(newValues, values, count * sizeof(T));
free(hashes);
free(names);
free(values);
hashes = newHashes;
values = newValues;
names = newNames;
capacity = newCapacity;
}
}
void Destroy(){
Clear();
if (hashes != nullptr) { free(hashes); }
if (names != nullptr) { free(names); }
if (values != nullptr) { free(values); }
}
void Clear(){
for(int i = 0; i < count; i++){
names[i].~String();
values[i].~T();
}
count = 0;
}
void Insert(const String& name, const T& value){
Hash hash = ComputeHash(name.string);
// TODO: This could be faster with a binary search
if (count == 0){
InsertAtIndex(name, hash, value, 0);
}
else{
int low = 0;
for( ; low < count && hash > hashes[low]; low++){
}
if(low < count && hashes[low] == hash){
bool found = false;
for(int i = low; i < count; i++){
if(name == names[i]){
found = true;
values[i] = value;
break;
}
else if(hash < hashes[i]){
break;
}
}
if(!found){
InsertAtIndex(name, hash, value, low);
}
}
else{
InsertAtIndex(name, hash, value, low);
}
}
}
bool LookUpByHashAndName(Hash hash, const char* str, int len, T* out) const {
int low = 0;
for ( ; low < count && hash > hashes[low]; low++) {
}
if(low < count && hashes[low] == hash){
for(int i = low; i < count; i++){
if((*str == '\0' && names[i].string == nullptr) ||
(names[i].GetLength() == len && StrEqualN(names[i].string, str, len))){
*out = values[i];
return true;
}
else if(hash < hashes[i]){
break;
}
}
}
return false;
}
bool LookUp(const String& name, T* out) const {
Hash hash = ComputeHash(name.string);
return LookUpByHashAndName(hash, name.string, name.GetLength(), out);
}
bool LookUp(const SubString& name, T* out) const {
Hash hash = ComputeHash(name.start, name.length);
return LookUpByHashAndName(hash, name.start, name.length, out);
}
bool LookUp(const char* name, T* out) const {
Hash hash = ComputeHash(name);
return LookUpByHashAndName(hash, name, StrLen(name), out);
}
//Internal use
void InsertAtIndex(const String& name, Hash hash, const T& value, int index){
if(count >= capacity){
EnsureCapacity((capacity > 0) ? (capacity * 2) : 2);
}
memmove(&hashes[index+1], &hashes[index], (count - index) * sizeof(Hash));
memmove(&names[index+1], &names[index], (count - index) * sizeof(String));
memmove(&values[index+1], &values[index], (count - index) * sizeof(T));
new(&names[index]) String();
new(&values[index]) T();
hashes[index] = hash;
names[index] = name;
values[index] = value;
count++;
}
};
#endif