aboutsummaryrefslogtreecommitdiff
path: root/src/string_map.cpp
blob: 4de88bbf9904e22dc01fc0674fab7e0d34e5d912 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
GB_STATIC_ASSERT(sizeof(MapIndex) == sizeof(u32));


struct StringHashKey {
	String string;
	u32    hash;

	operator String() const noexcept {
		return this->string;
	}
	operator String const &() const noexcept {
		return this->string;
	}
};
gb_internal gb_inline u32 string_hash(String const &s) {
	u32 res = fnv32a(s.text, s.len) & 0x7fffffff;
	return res | (res == 0);
}

gb_internal gb_inline StringHashKey string_hash_string(String const &s) {
	StringHashKey hash_key = {};
	hash_key.hash = string_hash(s);
	hash_key.string = s;
	return hash_key;
}

template <typename T>
struct StringMapEntry {
	String key;
	u32    hash;
	T      value;
};

template <typename T>
struct StringMap {
	StringMapEntry<T> *entries;
	u32                count;
	u32                capacity;
};


template <typename T> gb_internal void string_map_init    (StringMap<T> *h, usize capacity = 16);
template <typename T> gb_internal void string_map_destroy (StringMap<T> *h);

template <typename T> gb_internal T *  string_map_get     (StringMap<T> *h, char const *key);
template <typename T> gb_internal T *  string_map_get     (StringMap<T> *h, String const &key);
template <typename T> gb_internal T *  string_map_get     (StringMap<T> *h, StringHashKey const &key);

template <typename T> gb_internal T &  string_map_must_get(StringMap<T> *h, char const *key);
template <typename T> gb_internal T &  string_map_must_get(StringMap<T> *h, String const &key);
template <typename T> gb_internal T &  string_map_must_get(StringMap<T> *h, StringHashKey const &key);

template <typename T> gb_internal void string_map_set     (StringMap<T> *h, char const *key,   T const &value);
template <typename T> gb_internal void string_map_set     (StringMap<T> *h, String const &key, T const &value);
template <typename T> gb_internal void string_map_set     (StringMap<T> *h, StringHashKey const &key, T const &value);

// template <typename T> gb_internal void string_map_remove  (StringMap<T> *h, StringHashKey const &key);
template <typename T> gb_internal void string_map_clear   (StringMap<T> *h);
template <typename T> gb_internal void string_map_grow    (StringMap<T> *h);
template <typename T> gb_internal void string_map_reserve (StringMap<T> *h, usize new_count);

gb_internal gbAllocator string_map_allocator(void) {
	return heap_allocator();
}

template <typename T>
gb_internal gb_inline void string_map_init(StringMap<T> *h, usize capacity) {
	capacity = next_pow2_isize(capacity);
	string_map_reserve(h, capacity);
}

template <typename T>
gb_internal gb_inline void string_map_destroy(StringMap<T> *h) {
	gb_free(string_map_allocator(), h->entries);
}


template <typename T>
gb_internal void string_map__insert(StringMap<T> *h, u32 hash, String const &key, T const &value) {
	if (h->count+1 >= h->capacity) {
		string_map_grow(h);
	}
	GB_ASSERT(h->count+1 < h->capacity);

	u32 mask = h->capacity-1;
	MapIndex index = hash & mask;
	MapIndex original_index = index;
	do {
		auto *entry = h->entries+index;
		if (entry->hash == 0) {
			entry->key   = key;
			entry->hash  = hash;
			entry->value = value;

			h->count += 1;
			return;
		}
		index = (index+1)&mask;
	} while (index != original_index);

	GB_PANIC("Full map");
}

template <typename T>
gb_internal b32 string_map__full(StringMap<T> *h) {
	return 0.75f * h->count <= h->capacity;
}

template <typename T>
gb_inline void string_map_grow(StringMap<T> *h) {
	isize new_capacity = gb_max(h->capacity<<1, 16);
	string_map_reserve(h, new_capacity);
}


template <typename T>
gb_internal void string_map_reserve(StringMap<T> *h, usize cap) {
	if (cap < h->capacity) {
		return;
	}
	cap = next_pow2_isize(cap);

	StringMap<T> new_h = {};
	new_h.count    = 0;
	new_h.capacity = cast(u32)cap;
	new_h.entries = gb_alloc_array(string_map_allocator(), StringMapEntry<T>, new_h.capacity);

	if (h->count) {
		for (u32 i = 0; i < h->capacity; i++) {
			auto *entry = h->entries+i;
			if (entry->hash) {
				string_map__insert(&new_h, entry->hash, entry->key, entry->value);
			}
		}
	}
	string_map_destroy(h);
	*h = new_h;
}

template <typename T>
gb_internal T *string_map_get(StringMap<T> *h, u32 hash, String const &key) {
	if (h->count == 0) {
		return nullptr;
	}
	u32 mask = (h->capacity-1);
	u32 index = hash & mask;
	u32 original_index = index;
	do {
		auto *entry = h->entries+index;
		u32 curr_hash = entry->hash;
		if (curr_hash == 0) {
			// NOTE(bill): no found, but there isn't any key removal for this hash map
			return nullptr;
		} else if (curr_hash == hash && entry->key == key) {
			return &entry->value;
		}
		index = (index+1) & mask;
	} while (original_index != index);
	return nullptr;
}


template <typename T>
gb_internal gb_inline T *string_map_get(StringMap<T> *h, StringHashKey const &key) {
	return string_map_get(h, key.hash, key.string);
}

template <typename T>
gb_internal gb_inline T *string_map_get(StringMap<T> *h, String const &key) {
	return string_map_get(h, string_hash(key), key);
}

template <typename T>
gb_internal gb_inline T *string_map_get(StringMap<T> *h, char const *key) {
	String k = make_string_c(key);
	return string_map_get(h, string_hash(k), k);
}

template <typename T>
gb_internal T &string_map_must_get(StringMap<T> *h, u32 hash, String const &key) {
	T *found = string_map_get(h, hash, key);
	GB_ASSERT(found != nullptr);
	return *found;
}

template <typename T>
gb_internal T &string_map_must_get(StringMap<T> *h, StringHashKey const &key) {
	return string_map_must_get(h, key.hash, key.string);
}

template <typename T>
gb_internal gb_inline T &string_map_must_get(StringMap<T> *h, String const &key) {
	return string_map_must_get(h, string_hash(key), key);
}

template <typename T>
gb_internal gb_inline T &string_map_must_get(StringMap<T> *h, char const *key) {
	String k = make_string_c(key);
	return string_map_must_get(h, string_hash(k), k);
}

template <typename T>
gb_internal void string_map_set(StringMap<T> *h, u32 hash, String const &key, T const &value) {
	if (h->count == 0) {
		string_map_grow(h);
	}
	auto *found = string_map_get(h, hash, key);
	if (found) {
		*found = value;
		return;
	}
	string_map__insert(h, hash, key, value);
}

template <typename T>
gb_internal gb_inline void string_map_set(StringMap<T> *h, String const &key, T const &value) {
	string_map_set(h, string_hash_string(key), value);
}

template <typename T>
gb_internal gb_inline void string_map_set(StringMap<T> *h, char const *key, T const &value) {
	string_map_set(h, string_hash_string(make_string_c(key)), value);
}

template <typename T>
gb_internal gb_inline void string_map_set(StringMap<T> *h, StringHashKey const &key, T const &value) {
	string_map_set(h, key.hash, key.string, value);
}


template <typename T>
gb_internal gb_inline void string_map_clear(StringMap<T> *h) {
	h->count = 0;
	gb_zero_array(h->entries, h->capacity);
}


template <typename T>
struct StringMapIterator {
	StringMap<T> *map;
	MapIndex index;

	StringMapIterator<T> &operator++() noexcept {
		for (;;) {
			++index;
			if (map->capacity == index) {
				return *this;
			}
			StringMapEntry<T> *entry = map->entries+index;
			if (entry->hash != 0) {
				return *this;
			}
		}
	}

	bool operator==(StringMapIterator<T> const &other) const noexcept {
		return this->map == other->map && this->index == other->index;
	}

	operator StringMapEntry<T> *() const {
		return map->entries+index;
	}
};


template <typename T>
gb_internal StringMapIterator<T> end(StringMap<T> &m) noexcept {
	return StringMapIterator<T>{&m, m.capacity};
}

template <typename T>
gb_internal StringMapIterator<T> const end(StringMap<T> const &m) noexcept {
	return StringMapIterator<T>{&m, m.capacity};
}



template <typename T>
gb_internal StringMapIterator<T> begin(StringMap<T> &m) noexcept {
	if (m.count == 0) {
		return end(m);
	}

	MapIndex index = 0;
	while (index < m.capacity) {
		if (m.entries[index].hash) {
			break;
		}
		index++;
	}
	return StringMapIterator<T>{&m, index};
}
template <typename T>
gb_internal StringMapIterator<T> const begin(StringMap<T> const &m) noexcept {
	if (m.count == 0) {
		return end(m);
	}

	MapIndex index = 0;
	while (index < m.capacity) {
		if (m.entries[index].hash) {
			break;
		}
		index++;
	}
	return StringMapIterator<T>{&m, index};
}