Skip to content

Commit

Permalink
remove ._, get_tuple, replace with Tuple(x)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonDanisch committed Jun 27, 2016
1 parent 3c2e3c3 commit b3b4e15
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 34 deletions.
6 changes: 3 additions & 3 deletions src/FixedSizeArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ 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}}
values::NTuple{Column, NTuple{Row, T}}
end

# most common FSA types
immutable Vec{N, T} <: FixedVector{N, T}
_::NTuple{N, T}
values::NTuple{N, T}
end
immutable Point{N, T} <: FixedVector{N, T}
_::NTuple{N, T}
values::NTuple{N, T}
end

include("mapreduce.jl")
Expand Down
4 changes: 2 additions & 2 deletions src/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ E.g. Vec4f0(Vec3f0(1), 0)
@compat @generated function (::Type{FSA}){FSA <: FixedArray, T1 <: FixedArray}(a::T1, b...)
if isempty(b) # this is the conversion constructor for 2 FSA's
#easiest way is to just construct from the tuple
expr = :(FSA(get_tuple(a)))
expr = :(FSA(Tuple(a)))
if size_or(FSA, nothing) == nothing # no concrete size
return expr
else #has a size
len1 = size(FSA, 1)
len2 = size(T1, 1)
if len1 < len2 # we need to shrink
return :(FSA(get_tuple(a)[1:$len1]))
return :(FSA(Tuple(a)[1:$len1]))
elseif len1==len2
return expr
else
Expand Down
2 changes: 1 addition & 1 deletion src/conversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ end

#conversion
convert{N}(T::Type{NTuple{N}}, x::Real) = ntuple(ConstFunctor(eltype(T)(x)), Val{N})
convert{T <: Tuple, FSA <: FixedArray}(::Type{T}, x::FSA) = convert(T, get_tuple(x))
convert{T <: Tuple, FSA <: FixedArray}(::Type{T}, x::FSA) = convert(T, Tuple(x))

function convert{FSA1 <: FixedArray}(t::Type{FSA1}, f::FixedArray)
map(ConversionIndexFunctor(f, eltype_or(FSA1, eltype(typeof(f)))), FSA1)
Expand Down
10 changes: 7 additions & 3 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,22 @@ end
@pure similar_type{FSA <: FixedArray}(::Type{FSA}, sz::Tuple) = similar_type(FSA, eltype(FSA), sz)

# Deprecated similar() -> similar_type()
function get_tuple(f::FixedArray)
Base.depwarn("get_tuple(f::FixedArray) is deprecated, use Tuple(f) instead", :get_tuple)
Tuple(f)
end
function similar{FSA <: FixedArray}(::Type{FSA}, args...)
Base.depwarn("similar{FSA<:FixedArray}(::Type{FSA}, ...) is deprecated, use similar_type instead", :similar)
similar_type(FSA, args...)
end
similar{FSA <: FixedArray}(::Type{FSA}, sz::Int...) = similar(FSA, eltype(FSA), sz)
similar{FSA <: FixedArray,T}(::Type{FSA}, ::Type{T}, sz::Int...) = similar(FSA, T, sz)

@generated function get_tuple{N, T}(f::FixedVectorNoTuple{N, T})
@generated function (::Type{T}){T<:Tuple, N, T1}(f::FixedVectorNoTuple{N, T1})
return Expr(:tuple, ntuple(i->:(f[$i]), N)...)
end
function get_tuple(f::FixedArray)
f._
@compat function (::Type{T}){T<:Tuple}(f::FixedArray)
getfield(f, 1)
end


Expand Down
24 changes: 12 additions & 12 deletions src/indexing.jl
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
@inline getindex{T <: FixedVector}(x::T, i::Union{Range, Integer}) = x._[i]
@inline getindex{T <: FixedVectorNoTuple}(x::T, i::Integer) = getfield(x,i)
@inline getindex{T <: FixedVector}(x::T, i::Union{Range, Integer}) = Tuple(x)[i]
@inline getindex{T <: FixedVectorNoTuple}(x::T, i::Integer) = getfield(x, i)
@inline getindex{N, M, T}(a::Mat{N, M, T}, i::Range, j::Int) = ntuple(IndexFunc(a, j), Val{length(i)})::NTuple{length(i), T}
@inline getindex{N, M, T}(a::Mat{N, M, T}, i::Int, j::Union{Range, Int}) = a._[j][i]
@inline getindex{N, M, T}(a::Mat{N, M, T}, i::Int, j::Union{Range, Int}) = Tuple(a)[j][i]
@inline getindex{N, M, T}(a::Mat{N, M, T}, i::Int) = a[ind2sub((N,M), i)...]
@inline getindex(A::FixedArray, I::Tuple) = map(IndexFunctor(A), I)

@inline setindex(a::FixedArray, value, index::Int...) = map(SetindexFunctor(a, value, index), typeof(a))

@inline column{N, T}(v::FixedVector{N, T}) = v
@inline column{R, C, T}(a::Mat{R, C, T}, i::Union{Range, Int}) = a._[i]
@inline column{R, C, T}(a::Mat{R, C, T}, i::Union{Range, Int}) = Tuple(a)[i]

@inline row{N, T}(v::FixedVector{N, T}) = Mat{1, N, T}(v...)
@inline row{N, T}(v::FixedVector{N, T}, i::Int) = (v[i],)
@inline row{R, C, T}(a::Mat{R, C, T}, j::Int) = ntuple(IndexFunc(a, j), Val{C})::NTuple{C, T}
@inline row{R, T}(a::Mat{R, 1, T}, j::Int) = (a._[1][j],)
@inline row{R, T}(a::Mat{R, 2, T}, j::Int) = (a._[1][j], a._[2][j])
@inline row{R, T}(a::Mat{R, 3, T}, j::Int) = (a._[1][j], a._[2][j], a._[3][j])
@inline row{R, T}(a::Mat{R, 4, T}, j::Int) = (a._[1][j], a._[2][j], a._[3][j], a._[4][j],)
@inline row{R, T}(a::Mat{R, 1, T}, j::Int) = (Tuple(a)[1][j],)
@inline row{R, T}(a::Mat{R, 2, T}, j::Int) = (Tuple(a)[1][j], Tuple(a)[2][j])
@inline row{R, T}(a::Mat{R, 3, T}, j::Int) = (Tuple(a)[1][j], Tuple(a)[2][j], Tuple(a)[3][j])
@inline row{R, T}(a::Mat{R, 4, T}, j::Int) = (Tuple(a)[1][j], Tuple(a)[2][j], Tuple(a)[3][j], Tuple(a)[4][j],)


# the columns of the ctranspose are the complex conjugate rows
@inline crow{R, C, T}(a::Mat{R, C, T}, j::Int) = ntuple(IndexFunc(a, j)', Val{C})::NTuple{C, T}
@inline crow{R, T}(a::Mat{R, 1, T}, j::Int) = (a._[1][j]',)
@inline crow{R, T}(a::Mat{R, 2, T}, j::Int) = (a._[1][j]', a._[2][j]')
@inline crow{R, T}(a::Mat{R, 3, T}, j::Int) = (a._[1][j]', a._[2][j]', a._[3][j]')
@inline crow{R, T}(a::Mat{R, 4, T}, j::Int) = (a._[1][j]', a._[2][j]', a._[3][j]', a._[4][j]',)
@inline crow{R, T}(a::Mat{R, 1, T}, j::Int) = (Tuple(a)[1][j]',)
@inline crow{R, T}(a::Mat{R, 2, T}, j::Int) = (Tuple(a)[1][j]', Tuple(a)[2][j]')
@inline crow{R, T}(a::Mat{R, 3, T}, j::Int) = (Tuple(a)[1][j]', Tuple(a)[2][j]', Tuple(a)[3][j]')
@inline crow{R, T}(a::Mat{R, 4, T}, j::Int) = (Tuple(a)[1][j]', Tuple(a)[2][j]', Tuple(a)[3][j]', Tuple(a)[4][j]',)


# Unified slicing along combinations of fixed and variable dimensions
Expand Down
4 changes: 2 additions & 2 deletions src/mapreduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ end
@inline function reduce(f::Functor{2}, a::Mat)
length(a) == 1 && return a[1,1]
@inbounds begin
red = reduce(f, a._[1])
red = reduce(f, Tuple(a)[1])
for i=2:size(a, 2)
red = f(red, reduce(f, a._[i]))
red = f(red, reduce(f, Tuple(a)[i]))
end
end
red
Expand Down
14 changes: 8 additions & 6 deletions src/ops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,14 @@ end

@inline ctranspose{R, C, T}(a::Mat{R, C, T}) = Mat{C,R,T}(ntuple(CRowFunctor(a), Val{R}))
@generated function ctranspose{N,T}(b::Vec{N,T})
expr = [:(b._[$i]',) for i=1:N]
expr = [:(Tuple(b)[$i]',) for i=1:N]
return quote
Mat{1,N,T}($(expr...))
end
end
@inline transpose{R, C, T}(a::Mat{R, C, T}) = Mat(ntuple(RowFunctor(a), Val{R}))
@generated function transpose{N,T}(b::Vec{N,T})
expr = [:(transpose(b._[$i]),) for i=1:N]
expr = [:(transpose(Tuple(b)[$i]),) for i=1:N]
return quote
Mat{1,N,T}($(expr...))
end
Expand All @@ -141,7 +141,9 @@ immutable BilinearDotFunctor <: Functor{2} end
@inline bilindot(a::FixedVector, b::FixedVector) = sum(map(BilinearDotFunctor(), a, b))


#cross{T}(a::FixedVector{2, T}, b::FixedVector{2, T}) = a[1]*b[2]-a[2]*b[1] # not really used!?
function cross{T}(a::FixedVector{2, T}, b::FixedVector{2, T})
return a[1]*b[2]-a[2]*b[1]
end
@inline function cross{T1, T2}(a::FixedVector{3, T1}, b::FixedVector{3, T2})
@inbounds elements = (a[2]*b[3]-a[3]*b[2],
a[3]*b[1]-a[1]*b[3],
Expand Down Expand Up @@ -309,10 +311,10 @@ function (==)(a::FixedVectorNoTuple, b::FixedVectorNoTuple)
end
true
end
(==)(a::FixedArray, b::FixedArray) = a._ == b._
(==)(a::FixedArray, b::FixedArray) = Tuple(a) == Tuple(b)

(==){R, T, FSA <: FixedVector}(a::FSA, b::Mat{R, 1, T}) = a._ == column(b,1)
(==){R, T, FSA <: FixedVector}(a::Mat{R, 1, T}, b::FSA) = column(a,1) == b._
(==){R, T, FSA <: FixedVector}(a::FSA, b::Mat{R, 1, T}) = Tuple(a) == column(b, 1)
(==){R, T, FSA <: FixedVector}(a::Mat{R, 1, T}, b::FSA) = column(a,1) == Tuple(b)
function (==)(a::FixedArray, b::AbstractArray)
s_a = size(a)
s_b = size(b)
Expand Down
15 changes: 10 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ using Compat
import FixedSizeArrays: similar_type

immutable Normal{N, T} <: FixedVector{N, T}
_::NTuple{N, T}
values::NTuple{N, T}
end
immutable D3{N1, N2, N3, T} <: FixedArray{T, 3, Tuple{N1, N2, N3}}
_::NTuple{N1, NTuple{N2, NTuple{N3, T}}}
values::NTuple{N1, NTuple{N2, NTuple{N3, T}}}
end
immutable RGB{T} <: FixedVectorNoTuple{3, T}
r::T
Expand All @@ -20,7 +20,7 @@ end

# subtyping:
immutable TestType{N,T} <: FixedVector{N,T}
_::NTuple{N,T}
values::NTuple{N,T}
end

# Custom FSA with non-parameterized size and eltype
Expand Down Expand Up @@ -686,14 +686,19 @@ context("Ops") do
@fact @inferred(0.2f0*a) --> Vec{1,Float32}(3.2f0*0.2f0)
end
context("vector norm+cross product") do

@fact norm(Vec3d(1.0,2.0,2.0)) --> 3.0

# cross product
@fact cross(v1,v2) --> Vec3d(-7.0,14.0,-7.0)
@fact isa(cross(v1,v2),Vec3d) --> true
@fact isa(cross(v1,v2), Vec3d) --> true

@fact cross(vi,v2) --> Vec3d(-7.0,14.0,-7.0)
@fact isa(cross(vi,v2),Vec3d) --> true

a,b = Vec2d(0,1), Vec2d(1,0)
@fact cross(a,b) --> -1.0
@fact isa(cross(a,b), Float64) --> true
end

context("hypot") do
Expand Down Expand Up @@ -1211,7 +1216,7 @@ end

facts("show for subtype") do

Base.show(io::IO, x::TestType) = print(io, "$(x._)") # show for new type
Base.show(io::IO, x::TestType) = print(io, "$(Tuple(x))") # show for new type

x = TestType(1, 2)
@fact string(x) --> "(1,2)"
Expand Down

0 comments on commit b3b4e15

Please sign in to comment.