comparison stencil.jl @ 80:700a74c41b26 patch_based_test

Improve type stability
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Thu, 24 Jan 2019 14:33:49 +0100
parents 8cd8d83b92e7
children 7f72e7e14659
comparison
equal deleted inserted replaced
79:015bd5f33a54 80:700a74c41b26
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} # TBD: Should this be a tuple?
4 function Stencil(range, weights) 4 function Stencil(range, weights)
5 width = range[2]-range[1]+1 5 width = range[2]-range[1]+1
6 if width != length(weights) 6 if width != length(weights)
7 error("The width and the number of weights must be the same") 7 error("The width and the number of weights must be the same")
8 end 8 end
9 new(range, weights) 9 new{eltype(weights)}(range, weights)
10 end 10 end
11 end 11 end
12 12
13 function flip(s::Stencil) 13 function flip(s::Stencil)
14 range = (-s.range[2], -s.range[1]) 14 range = (-s.range[2], -s.range[1])
19 function Base.getindex(s::Stencil, i::Int) 19 function Base.getindex(s::Stencil, i::Int)
20 # TBD: Rearrange to mark with @boundscheck? 20 # TBD: Rearrange to mark with @boundscheck?
21 if s.range[1] <= i <= s.range[2] 21 if s.range[1] <= i <= s.range[2]
22 return s.weights[1 + i - s.range[1]] 22 return s.weights[1 + i - s.range[1]]
23 else 23 else
24 return 0 24 return eltype(s.weights)(0)
25 end 25 end
26 end 26 end
27 27
28 function apply(s::Stencil, v::AbstractVector, i::Int) 28 function apply(s::Stencil, v::AbstractVector, i::Int)
29 w = zero(eltype(v)) 29 w = zero(eltype(v))