Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support negative k for k-subsets #63

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/IterTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ mutable struct BinomialIterState
done::Bool
end

function iterate(it::Binomial, state=BinomialIterState(collect(Int64, 1:it.k), it.k > it.n))
function iterate(it::Binomial, state=BinomialIterState(collect(Int64, 1:it.k), it.k > it.n || it.k < 0))
state.done && return nothing

idx = state.idx
Expand Down Expand Up @@ -556,11 +556,14 @@ end
IteratorSize(::Type{StaticSizeBinomial{K, C}}) where {K, C} = HasLength()
IteratorEltype(::Type{StaticSizeBinomial{K, C}}) where {K, C} = IteratorEltype(C)

eltype(::Type{StaticSizeBinomial{K, C}}) where {K, C} = NTuple{K, eltype(C)}
eltype(::Type{StaticSizeBinomial{K, C}}) where {K, C} = K < 0 ? Union{} : NTuple{K, eltype(C)}
length(it::StaticSizeBinomial{K}) where {K} = binomial(length(it.xs), K)

subsets(xs::C, ::Val{K}) where {K, C} = StaticSizeBinomial{K, C}(xs)

# Special case for K < 0
iterate(it::StaticSizeBinomial{k}) where k = k < 0 ? nothing : _iterate(it)
iterate(it::StaticSizeBinomial{k}, state) where k = k < 0 ? nothing : _iterate(it, state)
# Special case for K == 0
iterate(it::StaticSizeBinomial{0}, state=false) = state ? nothing : ((), true)

Expand All @@ -579,7 +582,7 @@ function advance(it::StaticSizeBinomial{K}, idx) where {K}
end
advance(it::StaticSizeBinomial, idx::NTuple{1}) = (idx[end]+1,)

function iterate(it::StaticSizeBinomial{K}, idx=ntuple(identity, Val{K}())) where K
function _iterate(it::StaticSizeBinomial{K}, idx=ntuple(identity, Val{K}())) where K
idx[end] > length(it.xs) && return nothing
return (map(i -> it.xs[i], idx), advance(it, idx))
end
Expand Down
9 changes: 6 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ include("testing_macros.jl")
@test eltype(eltype(sk4)) == Symbol
@test collect(sk4) == Vector{Symbol}[]

@testset for i in 1:5
@testset for i in -1:5
sk5 = subsets(collect(1:4), i)
@test eltype(eltype(sk5)) == Int
@test length(collect(sk5)) == binomial(4, i)
Expand Down Expand Up @@ -264,9 +264,12 @@ include("testing_macros.jl")
@test eltype(eltype(sk5)) == Symbol
@test collect(sk5) == []

@testset for i in 1:6
@testset for i in -1:6
sk5 = subsets(collect(1:4), Val{i}())
@test eltype(eltype(sk5)) == Int
# there is no sensible element type information for i < 1
i < 0 && @test eltype(sk5) == Union{}
i == 0 && @test eltype(sk5) == Tuple{}
i >= 1 && @test eltype(eltype(sk5)) == Int
@test length(collect(sk5)) == binomial(4, i)
end

Expand Down