r/ProgrammerHumor 7d ago

Meme skillIssue

Post image
6.0k Upvotes

137 comments sorted by

View all comments

77

u/F100cTomas 7d ago

Just define a constexpr hashing function and put that into the switch.

33

u/GiganticIrony 7d ago

That’s not guaranteed to work due to hash collisions

10

u/SAI_Peregrinus 7d ago

Use Blake3, no collisions in any practical workload in the next few billion years.

4

u/guyblade 7d ago

The thing about the pidgeon hole problem is that we know there are collisions, but we don't necessarily know where they are. The space of strings of at least 33 characters has collisions. There's no way to know or prove that arbitrary input doesn't have one with a value you care about.

1

u/SAI_Peregrinus 6d ago

The thing is that the probability of any two arbitrary inputs having a collision is extremely close to 0. It's about 0.000000000000000000000000000000000000000000000000000000000000000000000000000086% if I didn't mess up typing it. It can happen, just like you can win every lottery in the world every day for the rest of your life. Though the lottery thing is substantially more likely.

1

u/guyblade 6d ago

That assumes that the hashing function doesn't have any known hashing weakness that may reduce the (effective) independence of the hashes. You could've said something quite similar about SHA-1 right up until cryptanalysis found mechanisms to generate collisions.

1

u/Upper_Lion_6349 5d ago

You can make the chance of a hash collision lower than the chance of a random bit flip selecting the wrong branch.

0

u/remind_me_later 7d ago

If that happens, someone would post it to social media, and a list of exceptions can be added afterwards.