Skip to content

WIP Add iteratorsize2 #40

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

Open
wants to merge 1 commit 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
5 changes: 5 additions & 0 deletions src/IterableTables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ using NamedTuples, Requires

export getiterator, isiterable, isiterabletable

struct HasLengthAfterStart <: Base.IteratorSize end

iteratorsize2(x) = iteratorsize2(typeof(x))
iteratorsize2{T}(::Type{T}) = Base.iteratorsize(T)

isiterable{T}(x::T) = method_exists(start, Tuple{T})

function getiterator(x)
Expand Down
47 changes: 32 additions & 15 deletions src/integrations/datatables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ end
end
end

@generated function _filldt_with_length(columns, enumerable)
@generated function _filldt_with_length(columns, enumerable, state)
n = length(columns.types)
push_exprs = Expr(:block)
for col_idx in 1:n
Expand All @@ -108,8 +108,14 @@ end
end

quote
for (i,v) in enumerate(enumerable)
state_internal = state
i = 1
while !done(enumerable, state_internal)
res = next(enumerable, state_internal)
v = res[1]
state_internal = res[2]
$push_exprs
i += 1
end
end
end
Expand All @@ -125,22 +131,33 @@ function _DataTable(x)
column_types = IterableTables.column_types(iter)
column_names = IterableTables.column_names(iter)

rows = Base.iteratorsize(typeof(iter))==Base.HasLength() ? length(iter) : 0

columns = []
for t in column_types
if isa(t, TypeVar)
push!(columns, Array{Any}(rows))
elseif t <: DataValue
push!(columns, NullableArray(t.parameters[1],rows))
else
push!(columns, Array{t}(rows))
if iteratorsize2(iter) in (Base.HasLength(), HasLengthAfterStart())
state = start(iter)

rows = iteratorsize2(iter)==HasLengthAfterStart() ? length(iter, state) : length(iter)

for t in column_types
if isa(t, TypeVar)
push!(columns, Array{Any}(rows))
elseif t <: DataValue
push!(columns, NullableArray(t.parameters[1],rows))
else
push!(columns, Array{t}(rows))
end
end
_filldt_with_length((columns...), iter, state)
else
for t in column_types
if isa(t, TypeVar)
push!(columns, Array{Any}(0))
elseif t <: DataValue
push!(columns, NullableArray(t.parameters[1],0))
else
push!(columns, Array{t}(0))
end
end
end

if Base.iteratorsize(typeof(iter))==Base.HasLength()
_filldt_with_length((columns...), iter)
else
_filldt_without_length((columns...), iter)
end

Expand Down