aboutsummaryrefslogtreecommitdiff
path: root/src/string_set.cpp
blob: fb4640c201d7cc8d40fcf6582b8ea2ef9a7e2ba7 (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
struct StringSetEntry {
	u32      hash;
	MapIndex next;
	String   value;

	operator String const() const noexcept {
		return this->value;
	}
	operator String const &() const noexcept {
		return this->value;
	}
};

struct StringSet {
	Slice<MapIndex>       hashes;
	Array<StringSetEntry> entries;
};


gb_internal void string_set_init   (StringSet *s, isize capacity = 16);
gb_internal void string_set_destroy(StringSet *s);
gb_internal void string_set_add    (StringSet *s, String const &str);
gb_internal bool string_set_update (StringSet *s, String const &str); // returns true if it previously existed
gb_internal bool string_set_exists (StringSet *s, String const &str);
gb_internal void string_set_remove (StringSet *s, String const &str);
gb_internal void string_set_clear  (StringSet *s);
gb_internal void string_set_grow   (StringSet *s);
gb_internal void string_set_rehash (StringSet *s, isize new_count);

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

gb_internal gb_inline void string_set_init(StringSet *s, isize capacity) {
	capacity = next_pow2_isize(gb_max(16, capacity));
	
	slice_init(&s->hashes,  string_set_allocator(), capacity);
	array_init(&s->entries, string_set_allocator(), 0, capacity);
	for (isize i = 0; i < capacity; i++) {
		s->hashes.data[i] = MAP_SENTINEL;
	}
}

gb_internal gb_inline void string_set_destroy(StringSet *s) {
	if (s->entries.allocator.proc == nullptr) {
		s->entries.allocator = string_set_allocator();
	}
	slice_free(&s->hashes, s->entries.allocator);
	array_free(&s->entries);
}

gb_internal MapIndex string_set__add_entry(StringSet *s, StringHashKey const &key) {
	StringSetEntry e = {};
	e.hash = key.hash;
	e.next = MAP_SENTINEL;
	e.value = key.string;
	array_add(&s->entries, e);
	return cast(MapIndex)(s->entries.count-1);
}

gb_internal MapFindResult string_set__find(StringSet *s, StringHashKey const &key) {
	MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL};
	if (s->hashes.count > 0) {
		fr.hash_index = cast(MapIndex)(((u64)key.hash) % s->hashes.count);
		fr.entry_index = s->hashes[fr.hash_index];
		while (fr.entry_index != MAP_SENTINEL) {
			auto const &entry = s->entries[fr.entry_index];
			if (entry.hash == key.hash && entry.value == key.string) {
				return fr;
			}
			fr.entry_prev = fr.entry_index;
			fr.entry_index = s->entries[fr.entry_index].next;
		}
	}
	return fr;
}
gb_internal MapFindResult string_set__find_from_entry(StringSet *s, StringSetEntry *e) {
	MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL};
	if (s->hashes.count > 0) {
		fr.hash_index = cast(MapIndex)(e->hash % s->hashes.count);
		fr.entry_index = s->hashes[fr.hash_index];
		while (fr.entry_index != MAP_SENTINEL) {
			if (&s->entries[fr.entry_index] == e) {
				return fr;
			}
			fr.entry_prev = fr.entry_index;
			fr.entry_index = s->entries[fr.entry_index].next;
		}
	}
	return fr;
}


gb_internal b32 string_set__full(StringSet *s) {
	return 0.75f * s->hashes.count <= s->entries.count;
}

gb_internal gb_inline void string_set_grow(StringSet *s) {
	isize new_count = gb_max(s->hashes.count<<1, 16);
	string_set_rehash(s, new_count);
}


gb_internal void string_set_reset_entries(StringSet *s) {
	for (isize i = 0; i < s->hashes.count; i++) {
		s->hashes.data[i] = MAP_SENTINEL;
	}
	for (isize i = 0; i < s->entries.count; i++) {
		MapFindResult fr;
		StringSetEntry *e = &s->entries.data[i];
		e->next = MAP_SENTINEL;
		fr = string_set__find_from_entry(s, e);
		if (fr.entry_prev == MAP_SENTINEL) {
			s->hashes[fr.hash_index] = cast(MapIndex)i;
		} else {
			s->entries[fr.entry_prev].next = cast(MapIndex)i;
		}
	}
}

gb_internal void string_set_reserve(StringSet *s, isize cap) {
	if (s->entries.allocator.proc == nullptr) {
		s->entries.allocator = string_set_allocator();
	}
	array_reserve(&s->entries, cap);
	if (s->entries.count*2 < s->hashes.count) {
		return;
	}
	slice_resize(&s->hashes, s->entries.allocator, cap*2);
	string_set_reset_entries(s);
}


gb_internal void string_set_rehash(StringSet *s, isize new_count) {
	string_set_reserve(s, new_count);
}

gb_inline bool string_set_exists(StringSet *s, String const &str) {
	StringHashKey key = string_hash_string(str);
	isize index = string_set__find(s, key).entry_index;
	return index != MAP_SENTINEL;
}

gb_internal void string_set_add(StringSet *s, String const &str) {
	MapIndex index;
	MapFindResult fr;
	StringHashKey key = string_hash_string(str);
	if (s->hashes.count == 0) {
		string_set_grow(s);
	}
	fr = string_set__find(s, key);
	if (fr.entry_index != MAP_SENTINEL) {
		index = fr.entry_index;
	} else {
		index = string_set__add_entry(s, key);
		if (fr.entry_prev != MAP_SENTINEL) {
			s->entries[fr.entry_prev].next = index;
		} else {
			s->hashes[fr.hash_index] = index;
		}
	}
	s->entries[index].value = str;

	if (string_set__full(s)) {
		string_set_grow(s);
	}
}

gb_internal bool string_set_update(StringSet *s, String const &str) {
	bool exists = false;
	MapIndex index;
	MapFindResult fr;
	StringHashKey key = string_hash_string(str);
	if (s->hashes.count == 0) {
		string_set_grow(s);
	}
	fr = string_set__find(s, key);
	if (fr.entry_index != MAP_SENTINEL) {
		index = fr.entry_index;
		exists = true;
	} else {
		index = string_set__add_entry(s, key);
		if (fr.entry_prev != MAP_SENTINEL) {
			s->entries[fr.entry_prev].next = index;
		} else {
			s->hashes[fr.hash_index] = index;
		}
	}
	s->entries[index].value = str;

	if (string_set__full(s)) {
		string_set_grow(s);
	}
	return exists;
}


gb_internal void string_set__erase(StringSet *s, MapFindResult fr) {
	MapFindResult last;
	if (fr.entry_prev == MAP_SENTINEL) {
		s->hashes[fr.hash_index] = s->entries[fr.entry_index].next;
	} else {
		s->entries[fr.entry_prev].next = s->entries[fr.entry_index].next;
	}
	if (fr.entry_index == s->entries.count-1) {
		array_pop(&s->entries);
		return;
	}
	auto *entry = &s->entries[fr.entry_index];
	*entry = s->entries[s->entries.count-1];
	StringHashKey key = {entry->hash, entry->value};
	last = string_set__find(s, key);
	if (last.entry_prev != MAP_SENTINEL) {
		s->entries[last.entry_prev].next = fr.entry_index;
	} else {
		s->hashes[last.hash_index] = fr.entry_index;
	}
}

gb_internal void string_set_remove(StringSet *s, String const &str) {
	StringHashKey key = string_hash_string(str);
	MapFindResult fr = string_set__find(s, key);
	if (fr.entry_index != MAP_SENTINEL) {
		string_set__erase(s, fr);
	}
}

gb_internal gb_inline void string_set_clear(StringSet *s) {
	array_clear(&s->entries);
	for_array(i, s->hashes) {
		s->hashes.data[i] = MAP_SENTINEL;
	}
}


gb_internal StringSetEntry *begin(StringSet &m) noexcept {
	return m.entries.data;
}
gb_internal StringSetEntry const *begin(StringSet const &m) noexcept {
	return m.entries.data;
}


gb_internal StringSetEntry *end(StringSet &m) noexcept {
	return m.entries.data + m.entries.count;
}

gb_internal StringSetEntry const *end(StringSet const &m) noexcept {
	return m.entries.data + m.entries.count;
}