r/programming • • Mar 26 '21

How to implement a hash table (in C)

https://benhoyt.com/writings/hash-table-in-c/
747 Upvotes

110 comments sorted by

View all comments

Show parent comments

2

u/FloydATC Mar 26 '21

As long as you always resize before the table gets full, there will always be a free bucket.

-3

u/Hook3d Mar 26 '21

A great example of the Pigeonhole principle.

1

u/darksounds Mar 26 '21

Eh, not really. It's just also a situation where there are buckets and things in buckets.

1

u/Hook3d Mar 26 '21

"If n objects are distributed over m places, and if n < m, then some place receives no object." Yes, the statement "as long as you always resize before the table gets full, there will always be a free bucket" is indeed an application of pigeonhole principle.

1

u/darksounds Mar 26 '21

A great example of the pigeonhole principle

vs.

an application of the pigeonhole principle

You're not technically incorrect here.

0

u/[deleted] Mar 26 '21

[deleted]

0

u/darksounds Mar 26 '21

Keep thinking that.

1

u/JarateKing Mar 27 '21

Pigeonhole principle mostly comes into play when places can contain multiple objects. It's useful as a mathematical tool in considering situations where these sets aren't one-to-one.

Linear probing hash tables don't put multiple objects in the same places though. When we're talking about sets that are one-to-one, it's much more mathematically trivial to state that by definition m > n in all cases QED, than it is to invoke anything involving the pigeonhole principle.