mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
I have been using this in a bunch of places in the compiler and rather than implement it by hand over and over (and maybe messing up), this commit just commits a correct implementation. This data structure is a map backed by a vector like data structure. It has two phases: 1. An insertion phase when the map is mutable and one inserts (key, value) pairs into the map. These are just appeneded into the storage array. 2. A frozen stage when the map is immutable and one can now perform map queries on the multimap. The map transitions from the mutable, thawed phase to the immutable, frozen phase by performing a stable_sort of its internal storage by only the key. Since this is a stable_sort, we know that the relative insertion order of values is preserved if their keys equal. Thus the sorting will have created contiguous regions in the array of values, all mapped to the same key, that are insertion order. Thus by finding the lower_bound for a given key, we are guaranteed to get the first element in that continguous range. We can then do a forward search to find the end of the region, allowing us to then return an ArrayRef to these internal values. The reason why I keep on finding myself using this is that this map enables one to map a key to an array of values without needing to store small vectors in a map or use heap allocated memory, all key, value pairs are stored inline (in potentially a single SmallVector given that one is using SmallFrozenMultiMap).
4.8 KiB
4.8 KiB