Skip to content

An immutable sorted vector#4482

Draft
copybara-service[bot] wants to merge 1 commit into
mainfrom
test_938810558
Draft

An immutable sorted vector#4482
copybara-service[bot] wants to merge 1 commit into
mainfrom
test_938810558

Conversation

@copybara-service

Copy link
Copy Markdown

An immutable sorted vector

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.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant