You might want to consider clarifying the title a bit. flat_map usually refers to an ordered map container where lookup is a binary search. For example https://en.cppreference.com/cpp/container/flat_map.
You give a single example from the C++ stdlib, but before that type was added I had never seen "flat map" used to mean this weird type.
Notably for example Rust's FlatMap is an implementation detail of Iterator::flat_map which is what you'd use to map each object into multiple items then flatten the result into a single stream of items in a single operation.
Do you have other examples? WG21 has a bad habit of making up their own names for ideas which have better names, or using names which mean something else already.
Whether the C++ standards names are ideal or not, you shouldn't be surprised if C++ programmers reading a C++ subreddit expect the terms to match the standard.
I kinda missed why the pointers from the array start pointing at the element before the first hash element suddenly...?
Also, shame that the clarifying images disappear when we get to the abseil version. An image is worth a thousand words - and when template-expanded, surely even more...
Occupied contains 1 byte per element, which stores an enum of Empty | Deleted | Full(7-bit hash) | Sentinel.
We have two hash functions: H1 and H2. H1 is an "ordinary" hash function, producing an index into Occupied and Data. H2 produces a 7-bit hash.
In order to find something, we take H1 to get a starting position into Occupied. The speed is gained from iterating through Occupied, only ever dereferencing the Data array when the 7-bit hashes match. You can also SIMD this.
Why are Sentinel and Deleted required? I'd expect tombstones to only be necessary in a concurrent table, and I'd expect that this will perform awfully in a concurrent setting.
Aha, the Deleted variant is required because Empty also indicates that a lookup can cease probing. So H(A) == H(B) [Full(A), Full(B), Empty] after delete A must become [Deleted, Full(B), Empty], otherwise lookup(B) would probe at index 0, see empty, and return false.
25
u/kirgel Sep 07 '26
You might want to consider clarifying the title a bit. flat_map usually refers to an ordered map container where lookup is a binary search. For example https://en.cppreference.com/cpp/container/flat_map.
3
u/johannes1971 Sep 07 '26
I kinda missed why the pointers from the array start pointing at the element before the first hash element suddenly...?
Also, shame that the clarifying images disappear when we get to the abseil version. An image is worth a thousand words - and when template-expanded, surely even more...
3
u/Life_Sink9598 Sep 07 '26
So, you have two arrays of equal size:
Occupied contains 1 byte per element, which stores an enum of
Empty | Deleted | Full(7-bit hash) | Sentinel.We have two hash functions: H1 and H2. H1 is an "ordinary" hash function, producing an index into Occupied and Data. H2 produces a 7-bit hash.
In order to find something, we take H1 to get a starting position into Occupied. The speed is gained from iterating through Occupied, only ever dereferencing the Data array when the 7-bit hashes match. You can also SIMD this.
Why are Sentinel and Deleted required? I'd expect tombstones to only be necessary in a concurrent table, and I'd expect that this will perform awfully in a concurrent setting.