Skip to content

Commit 5f6dad3

Browse files
committed
Speed up special cases of Cartesian products
If the second argument of `Data.Set.cartesianProduct` happens to be empty or a singleton, then we don't need to merge a bunch of trees. So let's not.
1 parent 8b0b600 commit 5f6dad3

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

Data/Set/Internal.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,6 +1727,15 @@ powerSet xs0 = insertMin empty (foldr' step Tip xs0) where
17271727
--
17281728
-- @since 0.5.11
17291729
cartesianProduct :: Set a -> Set b -> Set (a, b)
1730+
-- We don't know if this implementation (slightly modified from one
1731+
-- that Edward Kmett hacked together) is optimal. It would be interesting
1732+
-- to find out. TODO: try to get some clue about the big-O performance
1733+
-- so we can advise users.
1734+
1735+
-- When the second argument has at most one element, we can be a little
1736+
-- clever.
1737+
cartesianProduct !_as Tip = Tip
1738+
cartesianProduct as (Bin 1 b _ _) = mapMonotonic (flip (,) b) as
17301739
cartesianProduct as bs =
17311740
getMergeSet $ foldMap (\a -> MergeSet $ mapMonotonic ((,) a) bs) as
17321741

0 commit comments

Comments
 (0)