An immutable sorted vector#4482
Draft
copybara-service[bot] wants to merge 1 commit into
Draft
Conversation
We often need to deterministically iterate over some `const` collection. In some cases, we could use a `std::vector` and sort it, and do so at the time of creating the collection. In API signatures, `std::vector` doesn't capture this bit of information - i.e. that the producer took steps to guarantee determinism (see next para); or that the consumer intends to iterate deterministically. `SortedVector` does. `SortedVector` is not formally guaranteeing this property; it relies on the user defining a meaningful comparer. It's easy, though, to make sure the production of these things is done correctly (i.e. it's easy to spot a SortedVector returned with naked pointers, sorted naturally rather than a custom comparer, for example) The design is slightly more complicated than aliasing std::vector, to also block vector mutation APIs. Contents could still be mutated, but traversal order wouldn't change. PiperOrigin-RevId: 938810558
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
An immutable sorted vector
We often need to deterministically iterate over some
constcollection. In some cases, we could use astd::vectorand sort it, and do so at the time of creating the collection. In API signatures,std::vectordoesn't capture this bit of information - i.e. that the producer took steps to guarantee determinism (see next para); or that the consumer intends to iterate deterministically.SortedVectordoes.SortedVectoris not formally guaranteeing this property; it relies on the user defining a meaningful comparer. It's easy, though, to make sure the production of these things is done correctly (i.e. it's easy to spot a SortedVector returned with naked pointers, sorted naturally rather than a custom comparer, for example)The design is slightly more complicated than aliasing std::vector, to also block vector mutation APIs. Contents could still be mutated, but traversal order wouldn't change.