From 8bd03aff71e4f94a38c79364e0118cd83a43b6a9 Mon Sep 17 00:00:00 2001 From: Stephan Hilb Date: Tue, 6 Aug 2019 17:16:28 +0200 Subject: [PATCH 1/3] subsets: support negative binomial k --- src/IterTools.jl | 2 +- test/runtests.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/IterTools.jl b/src/IterTools.jl index 5b6b468..e76de36 100644 --- a/src/IterTools.jl +++ b/src/IterTools.jl @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 383fec0..7a020e6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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) From da7ea916806d6a4813d293d00a93b1cc0f7c7c62 Mon Sep 17 00:00:00 2001 From: Stephan Hilb Date: Wed, 7 Aug 2019 10:48:03 +0200 Subject: [PATCH 2/3] static subsets: support negative binomial k --- src/IterTools.jl | 7 +++++-- test/runtests.jl | 5 +++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/IterTools.jl b/src/IterTools.jl index e76de36..3174df6 100644 --- a/src/IterTools.jl +++ b/src/IterTools.jl @@ -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) @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 7a020e6..a7229e2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -264,9 +264,10 @@ 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 >= 1 && @test eltype(eltype(sk5)) == Int @test length(collect(sk5)) == binomial(4, i) end From c43cd24c6871b0d6f4c82ab181181c1eb80c0634 Mon Sep 17 00:00:00 2001 From: Stephan Hilb Date: Thu, 8 Aug 2019 10:21:24 +0200 Subject: [PATCH 3/3] static binomial: add eltype tests --- test/runtests.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index a7229e2..c4c58ac 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -267,6 +267,8 @@ include("testing_macros.jl") @testset for i in -1:6 sk5 = subsets(collect(1:4), Val{i}()) # 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