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

port to Julia 0.7 #182

Merged
merged 12 commits into from
Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ os:
- linux
- osx
julia:
- 0.6
- nightly
Copy link
Member

Choose a reason for hiding this comment

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

Should add 0.7 and 1.0 as well.

notifications:
email: false
# uncomment the following lines to override the default test script
Expand Down
3 changes: 1 addition & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
julia 0.6
julia 0.7-
Compat 0.19
NamedTuples 2.1.0
OnlineStats 0.17
PooledArrays 0.2.0
WeakRefStrings 0.4.4
Expand Down
10 changes: 6 additions & 4 deletions src/IndexedTables.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
__precompile__()
module IndexedTables

using Compat
using NamedTuples, PooledArrays
using PooledArrays

import Base:
show, eltype, length, getindex, setindex!, ndims, map, convert, keys, values,
==, broadcast, empty!, copy, similar, sum, merge, merge!, mapslices,
permutedims, reducedim, serialize, deserialize, sort, sort!
permutedims, reducedim, sort, sort!

using Serialization
import Serialization: serialize, deserialize

export NDSparse, flush!, aggregate!, aggregate_vec, where, pairs, convertdim, columns, column, rows,
itable, update!, aggregate, reducedim_vec, dimlabels, collect_columns
Expand Down Expand Up @@ -58,6 +60,6 @@ include("join.jl")
include("reshape.jl")

# TableTraits.jl integration
include("tabletraits.jl")
#include("tabletraits.jl")

end # module
88 changes: 46 additions & 42 deletions src/collect.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import DataValues: DataValue

_is_subtype(::Type{S}, ::Type{T}) where {S, T} = promote_type(S, T) == T

Base.@pure function dataarrayof(T)
if T<:DataValue
DataValueArray{T.parameters[1],1}
else
Vector{T}
end
Vector{T}
end

"""
Expand Down Expand Up @@ -36,28 +30,30 @@ julia> collect_columns(s)
7
```
"""
collect_columns(itr) = collect_columns(itr, Base.iteratorsize(itr))
collect_columns(itr) = collect_columns(itr, Base.IteratorSize(itr))

function collect_empty_columns(itr::T) where {T}
S = Core.Inference.return_type(first, Tuple{T})
S = Core.Compiler.return_type(first, Tuple{T})
similar(arrayof(S), 0)
end

function collect_columns(itr::ANY, ::Union{Base.HasShape, Base.HasLength})
st = start(itr)
done(itr, st) && return collect_empty_columns(itr)
el, st = next(itr, st)
function collect_columns(@nospecialize(itr), ::Union{Base.HasShape, Base.HasLength})
st = iterate(itr)
st === nothing && return collect_empty_columns(itr)
el, i = st
dest = similar(arrayof(typeof(el)), length(itr))
dest[1] = el
collect_to_columns!(dest, itr, 2, st)
collect_to_columns!(dest, itr, 2, i)
end

function collect_to_columns!(dest::AbstractArray{T}, itr, offs, st = start(itr)) where {T}
function collect_to_columns!(dest::AbstractArray{T}, itr, offs, st) where {T}
# collect to dest array, checking the type of each result. if a result does not
# match, widen the result type and re-dispatch.
i = offs
while !done(itr, st)
el, st = next(itr, st)
while true
elem = iterate(itr, st)
elem === nothing && break
el, st = elem
if fieldwise_isa(el, T)
@inbounds dest[i] = el
i += 1
Expand All @@ -71,41 +67,46 @@ function collect_to_columns!(dest::AbstractArray{T}, itr, offs, st = start(itr))
end

function collect_columns(itr, ::Base.SizeUnknown)
st = start(itr)
done(itr, st) && return collect_empty_columns(itr)
el, st = next(itr, st)
elem = iterate(itr)
elem === nothing && return collect_empty_columns(itr)
el, st = elem
dest = similar(arrayof(typeof(el)), 1)
dest[1] = el
grow_to_columns!(dest, itr, st)
grow_to_columns!(dest, itr, iterate(itr, st))
end

function collect_columns_flattened(itr)
st = start(itr)
el, st = next(itr, st)
elem = iterate(itr)
@assert elem !== nothing
el, st = elem
collect_columns_flattened(itr, el, st)
end

function collect_columns_flattened(itr, el, st)
while isempty(el)
done(itr, st) && return collect_empty_columns(el)
el, st = next(itr, st)
elem = iterate(itr, st)
elem === nothing && return collect_empty_columns(el)
el, st = elem
end
dest = collect_columns(el)
collect_columns_flattened!(dest, itr, el, st)
end

function collect_columns_flattened!(dest, itr, el, st)
while !done(itr, st)
el, st = next(itr, st)
while true
elem = iterate(itr, st)
elem === nothing && break
el, st = elem
dest = grow_to_columns!(dest, el)
end
return dest
end

function collect_columns_flattened(itr, el::Pair, st)
while isempty(el.second)
done(itr, st) && return collect_empty_columns(el.first => i for i in el.second)
el, st = next(itr, st)
elem = iterate(itr, st)
elem === nothing && return collect_empty_columns(el.first => i for i in el.second)
el, st = elem
end
dest_data = collect_columns(el.second)
dest_key = collect_columns(el.first for i in dest_data)
Expand All @@ -114,28 +115,31 @@ end

function collect_columns_flattened!(dest::Columns{<:Pair}, itr, el::Pair, st)
dest_key, dest_data = dest.columns
while !done(itr, st)
el, st = next(itr, st)
while true
elem = iterate(itr, st)
elem === nothing && break
el, st = elem
n = length(dest_data)
dest_data = grow_to_columns!(dest_data, el.second)
dest_key = grow_to_columns!(dest_key, el.first for i in (n+1):length(dest_data))
end
return Columns(dest_key => dest_data)
end

function grow_to_columns!(dest::AbstractArray{T}, itr, st = start(itr)) where {T}
function grow_to_columns!(dest::AbstractArray{T}, itr, elem = iterate(itr)) where {T}
# collect to dest array, checking the type of each result. if a result does not
# match, widen the result type and re-dispatch.
i = length(dest)+1
while !done(itr, st)
el, st = next(itr, st)
while elem !== nothing
el, st = elem
if fieldwise_isa(el, T)
push!(dest, el)
elem = iterate(itr, st)
i += 1
else
new = widencolumns(dest, i, el, T)
push!(new, el)
return grow_to_columns!(new, itr, st)
return grow_to_columns!(new, itr, iterate(itr, st))
end
end
return dest
Expand All @@ -147,7 +151,7 @@ fieldwise_isa(el::S, ::Type{Tuple}) where {S<:Tup} = _is_subtype(S, Tuple)
fieldwise_isa(el::S, ::Type{NamedTuple}) where {S<:Tup} = _is_subtype(S, NamedTuple)

@generated function fieldwise_isa(el::S, ::Type{T}) where {S<:Tup, T<:Tup}
if (fieldnames(S) == fieldnames(T)) && all(_is_subtype(s, t) for (s, t) in zip(S.parameters, T.parameters))
if (fieldnames(S) == fieldnames(T)) && all(_is_subtype(s, t) for (s, t) in zip(fieldtypes(S), fieldtypes(T)))
return :(true)
else
return :(false)
Expand All @@ -168,15 +172,15 @@ fieldwise_isa(el::Pair, ::Type{Pair{T1, T2}}) where {T1, T2} =
function widencolumns(dest, i, el::S, ::Type{T}) where{S <: Tup, T<:Tup}
if fieldnames(S) != fieldnames(T) || T == Tuple || T == NamedTuple
R = (S <: Tuple) && (T <: Tuple) ? Tuple : (S <: NamedTuple) && (T <: NamedTuple) ? NamedTuple : Any
new = Array{R}(length(dest))
copy!(new, 1, dest, 1, i-1)
new = Array{R}(undef, length(dest))
copyto!(new, 1, dest, 1, i-1)
else
sp, tp = S.parameters, T.parameters
idx = find(!(s <: t) for (s, t) in zip(sp, tp))
sp, tp = fieldtypes(S), fieldtypes(T)
idx = findall(collect(!(s <: t) for (s, t) in zip(sp, tp)))
new = dest
for l in idx
newcol = dataarrayof(promote_type(sp[l], tp[l]))(length(dest))
copy!(newcol, 1, column(dest, l), 1, i-1)
copyto!(newcol, 1, column(dest, l), 1, i-1)
new = setcol(new, l, newcol)
end
end
Expand All @@ -185,7 +189,7 @@ end

function widencolumns(dest, i, el::S, ::Type{T}) where{S, T}
new = dataarrayof(promote_type(S, T))(length(dest))
copy!(new, 1, dest, 1, i-1)
copyto!(new, 1, dest, 1, i-1)
new
end

Expand Down
Loading