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

Alternate #81

Open
wants to merge 2 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
81 changes: 73 additions & 8 deletions src/IterTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export
takewhile,
properties,
propertyvalues,
fieldvalues
fieldvalues,
alternate


function has_length(it)
it_size = IteratorSize(it)
Expand Down Expand Up @@ -590,14 +592,14 @@ iterate(it::StaticSizeBinomial{0}, state=false) = state ? nothing : ((), true)
pop(t::NTuple) = reverse(tail(reverse(t))), t[end]

function advance(it::StaticSizeBinomial{K}, idx) where {K}
xs = it.xs
lidx, i = pop(idx)
xs = it.xs
lidx, i = pop(idx)
i += 1
if i > length(xs) - K + length(idx)
lidx = advance(it, lidx)
i = lidx[end] + 1
end
return (lidx..., i)
if i > length(xs) - K + length(idx)
lidx = advance(it, lidx)
i = lidx[end] + 1
end
return (lidx..., i)
end
advance(it::StaticSizeBinomial, idx::NTuple{1}) = (idx[end]+1,)

Expand Down Expand Up @@ -1031,4 +1033,67 @@ function iterate(fs::FieldValues, state=1)
return (getfield(fs.x, state), state + 1)
end

# Alternate

struct Alternate{Is<:Tuple}
xs::Is
end
"""
alternate(xs...)
Alternates through each of the given iterators in order, shuffling their values
until one of them runs out.

```jldoctest
julia> collect(alternate(1:5,10:-1:1))
1
10
2
9
3
8
4
7
5
6
```
"""
alternate(xs...) = length(xs) == 1 ? xs[1] : Alternate(xs)
function Base.iterate(it::Alternate, state=1)
if state == 1
state=[Array{Any}(nothing,length(it.xs)),1]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not type stable, so will most likely perform poorly. I'd probably do something like

function iterate(it::Interleave, state=(true, iterate(it.a), iterate(it.b)))
    if nothing nonsense
    if state[1]
        state[2][1], (false, iterate(state[2][2], state[3])
    else
        state[3][1], (true, state[2], iterate(state[3][2])
    end
end

end
if isnothing(state[1][state[2]])
itreturn = iterate(it.xs[state[2]])
else
itreturn = iterate(it.xs[state[2]],state[1][state[2]])
end
isnothing(itreturn) && return nothing
state[1][state[2]] = itreturn[2]
state[2] = (state[2] % length(it.xs)) + 1
return (itreturn[1],state)
end
function IteratorEltype(::Type{Alternate{Is}}) where Is
iteratoreltypes = IteratorEltype.(fieldtypes(Is))
any(iteratoreltypes .== Ref(EltypeUnknown())) && return EltypeUnknown()
return HasEltype()
end
eltype(::Type{Alternate{Is}}) where Is = Union{eltype.(fieldtypes(Is))...}
function IteratorSize(::Type{Alternate{Is}}) where Is
iteratorsizes = IteratorSize.(fieldtypes(Is))
any(iteratorsizes .== Ref(SizeUnknown())) && return SizeUnknown()
all(iteratorsizes .== Ref(IsInfinite())) && return IsInfinite()
return HasLength() # Never returns HasShape
end
function length(i::Alternate)
m = Int64(minimum(IteratorSize(x) isa IsInfinite ? Inf : length(x) for x in i.xs))
l = length(i.xs)*m
for x in i.xs
if IteratorSize(x) isa IsInfinite || length(x) > m
l = l+1
else
return l
end
end
end

end # module IterTools
18 changes: 18 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,24 @@ include("testing_macros.jl")
@test collect(fv3) == Any[]
end

@testset "alternate" begin
intlist1 = 1:5
intlist2 = 2:4:14
inflist = countfrom(1)
unklist = takewhile(x -> x ≤ 10, countfrom(1))
shapedlist = [1 2 3;2 4 6]
str = "String"
@test eltype(alternate(intlist1,intlist2)) == Int64
@test eltype(alternate(intlist1,str)) == Union{Int64,Char}
@test length(alternate(intlist1,intlist2)) == 9
@test length(alternate(intlist2,intlist1)) == 8
@test IteratorSize(alternate(inflist,inflist)) isa IsInfinite
@test IteratorSize(alternate(intlist1,unklist)) isa SizeUnknown
@test IteratorSize(alternate(shapedlist,shapedlist)) isa HasLength
@test alternate(intlist1) == intlist1
@test collect(alternate(intlist1,intlist2,inflist)) == [1,2,1,2,6,2,3,10,3,4,14,4,5]
end

@testset "traits overriding defaults" begin
iters = [
firstrest(1:10),
Expand Down