I know that Odin doesn’t have a built-in separate hash set type, because one can simply use a map
and don’t care about it’s values.
What would be the best way to declare such a map? Let’s say I want to create a set of strings:
map[string]<something>
I recall asking this on Discord and getting an answer which suggested using some “dummy” type for the map values, but I forgot which type that was. Some kind of “unit” type? (iirc there’s no such thing in Odin)
I figured I’d ask here too as this may be helpful to someone else later.
1 Like
If the set of expected values is small enough, a dynamic array where you scan over every value can work well. If I need a set that’s based on map
and I don’t have any notion of how to better optimize it yet, I just use a bool
as the value.
If your keys are unique, like indexes, you could use a bitmap. It works well with packed, linear keys. Bloom filters are fun to play with, on this note.
1 Like
To use map to create a set, just type map[string]struct{}
.
When I have many sets in the same file, I tend to do
void :: struct{}
int_set: map[int]void
string_set: map[string]void
It just looks better to me, but is exactly the same, of course.
1 Like
Exactly! This was the trick I got recommended once.
1 Like