Skip to content

Conversation

@tetromino
Copy link
Collaborator

Also update the docs and tests.

Working towards #584

…forms

Also update the docs and tests.

Working towards google#584
@tetromino tetromino requested a review from adonovan March 26, 2025 18:28
Copy link
Collaborator

@adonovan adonovan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing these operators.

leaves the set `S` unchanged.
`intersection_update` fails if the set `S` is frozen or has active iterators, or
if any element of any of the `*others` is unhashable.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the record, this precludes a short cut when S is empty.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems reasonable? We usually prohibit no-op mutation attempts - for example, if a set is immutable, we don't allow trying to update it with an element that it already contains.

… not iterators

This allows preserving lhs iteration order and prevents unnecessary
copying.

Note that for intersection_update, we have to manually iterate over the
lhs set's hash table to modify it in-place - normal iteration doesn't
allow mutating the hash table.

Also take the opportunity to rename Set.InsertAll to Update, to match
the starlark API.

Add tests to verify iteration order.
@tetromino
Copy link
Collaborator Author

I've added a new Set.IntersectionUpdate() which preserves lhs iteration order - please take a look. Notably, since IntersectionUpdate needs to delete from the lhs hash table whilst iterating over it, it needs to iterate manually instead of using a normal iterator. I believe this is sound as long as the entry being deleted is an entry that has already been examined, and as long as we save the next pointer (since deletion will mutate the entry struct).

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.

2 participants