Repository navigation
feat(ts): translate set operations of hash and B-tree sets - #302
Merged
Merged
Conversation
Quest-Claim: 6d74af3846513f70
`union`, `intersection`, `difference`, and `symmetric_difference` of a `HashSet` and a `BTreeSet`, their operators `|`, `&`, `-`, and `^`, and `is_subset`, `is_superset`, and `is_disjoint` map to runtime functions that follow std's algorithms: a B-tree's merge or lookup by relative size, which picks which set's key of two equal ones is yielded, and a hash set's walk of the larger or smaller set. Each is lazy, and lends `[u8; N]` keys as slices. A set an operator builds has its root, so even empty it checks a range's bounds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts: # ts/fixtures/lang/caller.ts # ts/fixtures/lang/expected/harness.ts # ts/fixtures/lang/src/harness.rs # ts/runtime/rt/collections.ts
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts: # ts/fixtures/lang/expected/harness.ts
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kixelated
marked this pull request as ready for review
October 30, 2026 21:11
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 54 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This was referenced Oct 30, 2026
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.
Problem
union,intersection,difference, andsymmetric_differenceof aHashSetand aBTreeSetwere refused for every element type. So were the operator forms (&a | &b,&a & &b,&a - &b,&a ^ &b) andis_subset,is_superset, andis_disjoint. Reproduced on955ac19fwith the newcollections::set_opsfixture: 120 refusals, each at its span. Plain elements got "unsupported call tostd::collections::BTreeSet::<u8>::union", and so on.[u8; 2]elements got "std::collections::HashSet::unionof a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it".Approach
ts/runtime/rt/collections.ts):hashUnion,hashIntersection,hashDifference,hashSymmetricDifference,hashIsSubset,hashIsSuperset,hashIsDisjoint, thebtree*equivalents, andbtreeFromSorted. Each follows std's algorithm, which also decides which set's key of two equal ones is yielded:MergeIterInnermerge. Where the sizes differ byITER_PERFORMANCE_TIPPING_SIZE_DIFF(16) or more, it looks the smaller set's keys up in the larger instead. It takes std's shortcuts when the sets' ends meet.unionwalks the larger set first, andintersectionandis_disjointwalk the smaller. On a tie,selfwins.size_hint(feat(ts): translate size_hint and reserve by it in collect and extend #294 madesize_hinttranslate). Iterators that stitch keep a peeked key apart from "not yet peeked", so a cachedNonekey isn't skipped.ts/src/lower/collections.rs): maps the seven methods and the four operators. An operator builds a new set from the op's keys, each through the element'sClone, as std's does:hashExtendKeysinto a fresh set, checking hashbrown's capacity limit ascollectdoes.btreeFromSorted, which keeps std's root even when empty, sorangestill checks its bounds.lends_callaccepts the other set of a set op or operator as an argument when its keys are[u8; N]. It accepts nothing else:set.extend(&other)is still refused, as feat(ts): lend the byte arrays a collection holds as slices #295 intended.collections::set_opsruns against native wasm32. It covers element typesu8,[u8; 2],Option<u8>,Reverse<Option<u8>>(whereNoneis the greatest key), andNibble, whose equal keys differ in their high bits, so the output shows which set's key std picks.rangepanic.size_hintbefore and after one item (B-tree) or before any (hash), and the operator-built sets read withiter()anditer().rev().caller.tsreach the lookup path for every element type, and hash sets of equal and unequal size.Nonetaken as unpeeked, and the size hints). The differential failed on every one.Choices made without asking:
DoubleEndedIterator, sorev()is tested on the sets the operators build. The ops' own order is checked forward.CloneandDebugof the set-op iterators stay refused with a span, as other std iterators' are.Impact
rtexports: the 14 set functions above andbtreeFromSorted. Nothing breaks.Alternatives
from_sorted_iter, and std's iterator structs hold std's iterators, which the runtime models itself.Follow-ups
[bool; 3].map(u8::from)was refused while writing the fixture.Review
Codex
gpt-6.1-sol, medium effort, viaquest review. Round 2 reviewed headc7d13638de063686d850d1ec57906aa371d9bab5againstorigin/main4b965ce1and found nothing.Round 1 (head
2c5f77b4) found two issues:Differenceusedundefinedboth for a cachedNonekey and for "not yet peeked", so withReverse<Option<u8>>keys it skipped a key. It now keeps a separatepeekedflag, and the newReverse<Option<u8>>elements reproduce the bug when the fix is reverted.ts/runtime/rt/iter.ts, but the branch never touched that file. The base had moved past feat(ts): translate size_hint and reserve by it in collect and extend #294, so the round-1 diff showed feat(ts): translate size_hint and reserve by it in collect and extend #294's changes reversed. After merging main,git diff origin/main...HEAD -- ts/runtime/rt/iter.tsis empty.🤖 Generated with Claude Code
(written by Claude Opus 5.5)