more code sharing

This commit is contained in:
Mike J Innes 2017-06-05 18:46:36 +01:00
parent 7505ee3a16
commit b2baf512e1
2 changed files with 28 additions and 32 deletions

View File

@ -4,8 +4,6 @@ struct Batch{T,S} <: BatchLike{T,S}
data::Storage{T,S}
end
@forward Batch.data size, eltype, getindex, setindex!, rawbatch
Batch(xs) = Batch(Storage(xs))
convert{T,S}(::Type{Batch{T,S}},storage::S) =
@ -35,8 +33,6 @@ struct Seq{T,S} <: BatchLike{T,S}
data::Storage{T,S}
end
@forward Seq.data size, eltype, getindex, setindex!, rawbatch
Seq(xs) = Seq(Storage(xs))
convert{T,S}(::Type{Seq{T,S}},storage::S) =

View File

@ -1,8 +1,36 @@
import Base: eltype, size, getindex, setindex!, convert
rawbatch(xs) = xs
abstract type BatchLike{T,S} <: AbstractVector{T}
end
eltype{T}(::BatchLike{T}) = T
rawbatch(xs::BatchLike) = rawbatch(xs.data)
size(b::BatchLike) = (size(rawbatch(b), 1),)
getindex(b::BatchLike, i)::eltype(b) = slicedim(rawbatch(b), 1, i)
setindex!(b::BatchLike, v, i::Integer) = rawbatch(b)[i, :] = v
function setindex!(b::BatchLike, xs, ::Colon)
for (i, x) in enumerate(xs)
b[i] = x
end
end
typename(b::Type) = b
typename(b::Type{<:BatchLike}) =
Row(Juno.typ("$(b.name.name)"), text"{", typename(eltype(b)), text"}")
@render Juno.Inline b::BatchLike begin
Tree(Row(typename(typeof(b)),
Juno.fade("[$(length(b))]")),
Juno.trim(collect(b)))
end
struct Storage{T,S} <: BatchLike{T,S}
data::S
end
@ -10,20 +38,6 @@ end
convert{T,S}(::Type{Storage{T,S}},storage::S) =
Storage{T,S}(storage)
eltype{T}(::Storage{T}) = T
size(b::Storage) = (size(b.data, 1),)
getindex(b::Storage, i)::eltype(b) = slicedim(b.data, 1, i)
setindex!(b::Storage, v, i::Integer) = b.data[i, :] = v
function setindex!(b::Storage, xs, ::Colon)
for (i, x) in enumerate(xs)
b[i] = x
end
end
allequal(xs) = all(x -> x == first(xs), xs)
function (::Type{Storage{T,S}}){T,S}(xs, storage::S)
@ -45,17 +59,3 @@ function Storage(xs)
xs = promote(xs...)
Storage{eltype(xs)}(xs)
end
typename(b::Type) = b
typename(b::Type{<:BatchLike}) =
Row(Juno.typ("$(b.name.name)"), text"{", typename(eltype(b)), text"}")
@render Juno.Inline b::BatchLike begin
Tree(Row(typename(typeof(b)),
Juno.fade("[$(length(b))]")),
Juno.trim(collect(b)))
end
rawbatch(xs) = xs
rawbatch(xs::Storage) = xs.data