Skip to content

Commit

Permalink
issubset: check IteratorSize trait before calling length
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Aug 24, 2018
1 parent cd850de commit c2fb5b3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
17 changes: 9 additions & 8 deletions base/abstractset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,15 @@ end
<=(l::AbstractSet, r::AbstractSet) = l r

function issubset(l, r)

rlen = length(r)
#This threshold was empirically determined by repeatedly
#sampling using these two methods.
lenthresh = 70

if rlen > lenthresh && !isa(r, AbstractSet)
return issubset(l, Set(r))
if IteratorSize(r) isa Union{HasLength,HasShape}
rlen = length(r)
#This threshold was empirically determined by repeatedly
#sampling using these two methods (see #26198)
lenthresh = 70

if rlen > lenthresh && !isa(r, AbstractSet)
return issubset(l, Set(r))
end
end

for elt in l
Expand Down
13 changes: 13 additions & 0 deletions test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -604,3 +604,16 @@ end
end
end
end

struct OpenInterval{T}
lower::T
upper::T
end
Base.in(x, i::OpenInterval) = i.lower < x < i.upper
Base.IteratorSize(::Type{<:OpenInterval}) = Base.SizeUnknown()

@testset "Continuous sets" begin
i = OpenInterval(2, 4)
@test 3 i
@test issubset(3, i)
end

0 comments on commit c2fb5b3

Please sign in to comment.