Skip to content

Commit

Permalink
Cleanup similar_type a little
Browse files Browse the repository at this point in the history
* Avoid some duplication in similar_type definitions
* Move `@pure` somewhere more sensible
* Remove similar_type convenience function for simplicity
  • Loading branch information
c42f authored and Chris Foster committed Jun 1, 2016
1 parent 7cc58f4 commit 39a9595
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
21 changes: 14 additions & 7 deletions src/FixedSizeArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,28 @@ import Base.LinAlg.chol!
# for 0.5 and 0.4 compat, use our own functor type
abstract Functor{N}

include("core.jl")
include("functors.jl")
include("constructors.jl")

if VERSION <= v"0.5.0"
supertype(x) = super(x)
end

if VERSION < v"0.5.0-dev+698"
macro pure(ex)
esc(ex)
end
else
import Base: @pure
end

include("core.jl")
include("functors.jl")
include("constructors.jl")

# put them here due to #JuliaLang/julia#12814
# needs to be before indexing and ops, but after constructors
immutable Mat{Row, Column, T} <: FixedMatrix{Row, Column, T}
_::NTuple{Column, NTuple{Row, T}}
end
similar_type{FSA<:Mat,T}(::Type{FSA}, ::Type{T}, sz::NTuple{2, Int}) = Mat{sz[1], sz[2], T}
similar_type{N,M,S, T}(::Type{Mat{N,M,S}}, ::Type{T}) = Mat{N,M,T}
@pure similar_type{FSA<:Mat,T}(::Type{FSA}, ::Type{T}, sz::NTuple{2, Int}) = Mat{sz[1], sz[2], T}

# most common FSA types
immutable Vec{N, T} <: FixedVector{N, T}
Expand All @@ -32,7 +39,7 @@ end
immutable Point{N, T} <: FixedVector{N, T}
_::NTuple{N, T}
end
similar_type{FSA<:Point,T}(::Type{FSA}, ::Type{T}, sz::Tuple{Int}) = Point{sz[1],T}
@pure similar_type{FSA<:Point,T}(::Type{FSA}, ::Type{T}, sz::Tuple{Int}) = Point{sz[1],T}

include("mapreduce.jl")
include("destructure.jl")
Expand Down
13 changes: 1 addition & 12 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,6 @@ done(A::FixedArray, state::Integer) = length(A) < state
:($(T.name.primary))
end

if VERSION < v"0.5.0-dev+698"
macro pure(ex)
esc(ex)
end
else
import Base: @pure
end

@pure function similar_type{FSA <: FixedArray, T}(::Type{FSA}, ::Type{T}, n::Tuple)
# Exact match - return the same type again
if eltype(FSA) == T && n == size(FSA)
Expand All @@ -101,13 +93,10 @@ end
end
end

# Versions with defaults
# Versions with defaulted eltype and size
@pure similar_type{FSA <: FixedArray, T}(::Type{FSA}, ::Type{T}) = similar_type(FSA, T, size(FSA))
@pure similar_type{FSA <: FixedArray}(::Type{FSA}, sz::Tuple) = similar_type(FSA, eltype(FSA), sz)

# Convenience function
@pure similar_type{FSA <: FixedArray, T}(::Type{FSA}, ::Type{T}, sz::Int...) = similar_type(FSA, T, sz)


@generated function get_tuple{N, T}(f::FixedVectorNoTuple{N, T})
:(tuple($(ntuple(i->:(f[$i]), N)...)))
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ context("core") do
end
context("similar_type") do
@fact similar_type(Vec{3}, Float32) --> Vec{3, Float32}
@fact similar_type(Vec, Float32, 3) --> Vec{3, Float32}
@fact similar_type(Vec, Float32, (3,)) --> Vec{3, Float32}

@fact similar_type(RGB, Float32) --> RGB{Float32}
@fact similar_type(RGB{Float32}, Int) --> RGB{Int}
Expand Down

0 comments on commit 39a9595

Please sign in to comment.