File tree Expand file tree Collapse file tree 2 files changed +25
-2
lines changed Expand file tree Collapse file tree 2 files changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -67,9 +67,11 @@ function union!(s::AbstractSet, sets...)
67
67
end
68
68
69
69
max_values (:: Type ) = typemax (Int)
70
- max_values (T:: Type{<:Union{Nothing,BitIntegerSmall}} ) = 1 << (8 * sizeof (T))
71
- max_values (T:: Union ) = max (max_values (T. a), max_values (T. b))
70
+ max_values (T:: Union{map(X -> Type{X}, BitIntegerSmall_types)...} ) = 1 << (8 * sizeof (T))
71
+ # saturated addition to prevent overflow with typemax(Int)
72
+ max_values (T:: Union ) = max (max_values (T. a), max_values (T. b), max_values (T. a) + max_values (T. b))
72
73
max_values (:: Type{Bool} ) = 2
74
+ max_values (:: Type{Nothing} ) = 1
73
75
74
76
function union! (s:: AbstractSet{T} , itr) where T
75
77
haslength (itr) && sizehint! (s, length (s) + length (itr))
Original file line number Diff line number Diff line change 613
613
end
614
614
end
615
615
616
+ @testset " optimized union! with max_values" begin
617
+ # issue #30315
618
+ T = Union{Nothing, Bool}
619
+ @test Base. max_values (T) == 3
620
+ d = Set {T} ()
621
+ union! (d, (nothing , true , false ))
622
+ @test length (d) == 3
623
+ @test d == Set ((nothing , true , false ))
624
+ @test nothing in d
625
+ @test true in d
626
+ @test false in d
627
+
628
+ for X = (Int8, Int16, Int32, Int64)
629
+ @test Base. max_values (Union{Nothing, X}) == (sizeof (X) < sizeof (Int) ?
630
+ 2 ^ (8 * sizeof (X)) + 1 :
631
+ typemax (Int))
632
+ end
633
+ # this does not account for non-empty intersections of the unioned types
634
+ @test Base. max_values (Union{Int8,Int16}) == 2 ^ 8 + 2 ^ 16
635
+ end
636
+
616
637
struct OpenInterval{T}
617
638
lower:: T
618
639
upper:: T
You can’t perform that action at this time.
0 commit comments