comparison stencil.jl @ 67:7fd4e7a1cd38 cell_based_test

Make stencil more type stabeler
author Jonatan Werpers <jonatan@werpers.com>
date Thu, 17 Jan 2019 15:52:25 +0100
parents ef060ab3b035
children 18d0d794d3bb fbf7398f8154
comparison
equal deleted inserted replaced
66:543b7a5ab831 67:7fd4e7a1cd38
1 struct Stencil 1 struct Stencil{T<:Real}
2 range::NTuple{2,Int} 2 range::NTuple{2,Int}
3 weights::Vector # TBD: Should this be a tuple? 3 weights::Vector{T} # Should this be a tuple?? (Check type stability)
4
4 function Stencil(range, weights) 5 function Stencil(range, weights)
5 width = range[2]-range[1]+1 6 width = range[2]-range[1]+1
6 if width != length(weights) 7 if width != length(weights)
7 error("The width and the number of weights must be the same") 8 error("The width and the number of weights must be the same")
8 end 9 end
9 new(range, weights) 10 new{eltype(weights)}(range, weights)
10 end 11 end
11 end 12 end
12 13
13 function flip(s::Stencil) 14 function flip(s::Stencil)
14 range = (-s.range[2], -s.range[1]) 15 range = (-s.range[2], -s.range[1])
18 # Provides index into the Stencil based on offset for the root element 19 # Provides index into the Stencil based on offset for the root element
19 function Base.getindex(s::Stencil, i::Int) 20 function Base.getindex(s::Stencil, i::Int)
20 if s.range[1] <= i <= s.range[2] 21 if s.range[1] <= i <= s.range[2]
21 return s.weights[1 + i - s.range[1]] 22 return s.weights[1 + i - s.range[1]]
22 else 23 else
23 return 0 24 return eltype(s.weights)(0)
24 end 25 end
25 end 26 end
26 27
27 function apply(s::Stencil, v::AbstractVector, i::Int) 28 function apply(s::Stencil, v::AbstractVector, i::Int)
28 w = zero(eltype(v)) 29 w = zero(eltype(v))