What a Hash Table Cannot Do
Last timeAsking Only Whether It Is There
The hash that scattered the keys destroyed their order, and with it every question about nearest, next, between and in order. That missing half is what the next course on this shelf is for.
Six lessons have been spent making the lookup instant. This one is about the
bill for it, which is larger than it first appears.
| a hash table | keys kept in order | |
|---|---|---|
| is this exact key presen | 1 | 2 |
| what is stored under thi | 1 | 2 |
| what is the smallest key | 3 | 1 |
| what key comes after thi | 3 | 1 |
| which keys lie between t | 3 | 2 |
| which keys start with th | 3 | 2 |
| give me everything in or | 3 | 2 |
| give me the ten largest | 3 | 2 |
import bisect, random, time
n = 1_000_000
keys = random.sample(range(10 * n), n)
table = {k: 1 for k in keys}
ordered = sorted(keys)
lo, hi = 4_000_000, 4_010_000
t = time.perf_counter()
for k in keys[:1000]:
table[k]
print('exact lookup, dictionary', round(time.perf_counter() - t, 5))
t = time.perf_counter()
for k in keys[:1000]:
i = bisect.bisect_left(ordered, k)
print('exact lookup, sorted list', round(time.perf_counter() - t, 5))
t = time.perf_counter()
in_range = [k for k in table if lo <= k <= hi]
print('range, dictionary', round(time.perf_counter() - t, 5), len(in_range))
t = time.perf_counter()
a, b = bisect.bisect_left(ordered, lo), bisect.bisect_right(ordered, hi)
in_range = ordered[a:b]
print('range, sorted list', round(time.perf_counter() - t, 5), len(in_range))The decision, in one line
If every question is an exact key, use a hash table and enjoy the single
step. If any question refers to neighbours, to order, to a range or to a
prefix, something ordered is needed.
That something is the next course on this shelf. It is also why a database
offers two kinds of index and asks which one you want.
The lesson stops here
1 more paragraph to go
You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.
The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.
See the planThe contentsThis is the reading half
Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.
The contents