fix(set): setUnion returns wrong element order when sets overlap#3675
Open
JSap0914 wants to merge 1 commit into
Open
fix(set): setUnion returns wrong element order when sets overlap#3675JSap0914 wants to merge 1 commit into
JSap0914 wants to merge 1 commit into
Conversation
setUnion([1,2,3,4],[3,4,5,6]) returned [1,2,5,6,3,4] instead of the documented [1,2,3,4,5,6]. The implementation used concat(setSymDifference, setIntersect) which places the intersection at the end. The correct formula is concat(setDifference(a,b), setIntersect(a,b), setDifference(b,a)) so that elements unique to a come first (sorted), then the common elements (sorted), then elements unique to b (sorted). Fixes #ISSUE
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
math.setUnion()produces incorrect output whenever the two input sets share elements — the intersection is placed at the end instead of its sorted position.Root Cause
The previous implementation:
setSymDifferencereturns[a-only, b-only], so the result is[a-only, b-only, a∩b]— the shared elements land last.Fix
Replace with three explicit pieces that are each sorted by their respective helpers:
This gives the correct sorted union: a-only ++ common ++ b-only.
Changes
src/function/set/setUnion.js: swapsetSymDifferencedependency forsetDifference; fix the formula.test/unit-tests/function/set/setUnion.test.js: add overlapping-set assertions (were RED); correct the multiset expected value (was documenting wrong output).Closes #3674