Skip to content

Commit

Permalink
port to Julia 0.7/1.0 (#182)
Browse files Browse the repository at this point in the history
port to Julia 0.7/1.0
  • Loading branch information
shashi authored and JeffBezanson committed Sep 24, 2018
1 parent 1341d14 commit 4e0d676
Show file tree
Hide file tree
Showing 21 changed files with 551 additions and 621 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ os:
- linux
- osx
julia:
- 0.6
- 0.7
- 1.0
- nightly
notifications:
email: false
# uncomment the following lines to override the default test script
script:
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
- julia -e 'Pkg.clone(pwd()); Pkg.build("IndexedTables"); Pkg.test("IndexedTables"; coverage=true)'
- julia -e 'using Pkg; Pkg.clone(pwd()); Pkg.build("IndexedTables"); Pkg.test("IndexedTables"; coverage=true)'
after_success:
- julia -e 'cd(Pkg.dir("IndexedTables")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder()); Codecov.submit(Codecov.process_folder())'
12 changes: 6 additions & 6 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
julia 0.6
Compat 0.19
NamedTuples 2.1.0
julia 0.7
OnlineStats 0.17
PooledArrays 0.2.0
PooledArrays 0.4.1
WeakRefStrings 0.4.4
TableTraits 0.0.1
TableTraitsUtils 0.0.1
TableTraits 0.3.0
TableTraitsUtils 0.2.0
IteratorInterfaceExtensions 0.1.0
DataValues
8 changes: 3 additions & 5 deletions src/IndexedTables.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
__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, sort, sort!, iterate, pairs

export NDSparse, flush!, aggregate!, aggregate_vec, where, pairs, convertdim, columns, column, rows,
export NDSparse, flush!, aggregate!, aggregate_vec, where, convertdim, columns, column, rows,
itable, update!, aggregate, reducedim_vec, dimlabels, collect_columns

const Tup = Union{Tuple,NamedTuple}
Expand Down
93 changes: 49 additions & 44 deletions src/collect.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ 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
end
dataarrayof(::Type{<:DataValue{T}}, len) where {T} = DataValueArray{T,1}(len)
dataarrayof(::Type{T}, len) where {T} = Vector{T}(undef, len)

"""
`collect_columns(itr)`
Expand Down Expand Up @@ -36,28 +31,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 +68,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 +116,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 +152,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,24 +173,24 @@ 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)
newcol = dataarrayof(promote_type(sp[l], tp[l]), length(dest))
copyto!(newcol, 1, column(dest, l), 1, i-1)
new = setcol(new, l, newcol)
end
end
new
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)
new = dataarrayof(promote_type(S, T), length(dest))
copyto!(new, 1, dest, 1, i-1)
new
end

Expand Down
Loading

0 comments on commit 4e0d676

Please sign in to comment.