view stencil.jl @ 123:5df4ccb19476 cell_based_test

Undo changes of 3560f54e3eb3 and change to result a little bit to avoid allocations. Trying different signatres reveals some strange interactions with julias dispatch: allocate: getrange(gridsize::Integer, closuresize::Integer, region::Type{<:Region}) no allocate: getrange(gridsize::Integer, closuresize::Integer, region::R) where R allocate: getrange(gridsize::Integer, closuresize::Integer, region::R) where R <: Type{<:Region} no allocate: getrange(gridsize::Integer, closuresize::Integer, region::DataType)
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 13 Feb 2019 10:58:57 +0100
parents 6c6979ff17f4
children 7c0b9bb7ab4d
line wrap: on
line source

struct Stencil{T<:Real,N}
    range::Tuple{Int,Int}
    weights::NTuple{N,T}
end

function flip(s::Stencil)
    range = (-s.range[2], -s.range[1])
    return Stencil(range, reverse(s.weights))
end

# Provides index into the Stencil based on offset for the root element
function Base.getindex(s::Stencil, i::Int)
    @boundscheck if i < s.range[1] || s.range[2] < i
        return eltype(s.weights)(0)
    end

    return s.weights[1 + i - s.range[1]]
end

Base.@propagate_inbounds function apply(s::Stencil, v::AbstractVector, i::Int)
    w = zero(eltype(v))
    for j ∈ s.range[1]:s.range[2]
        @inbounds weight = s[j]
        w += weight*v[i+j]
    end
    return w
end

Base.@propagate_inbounds function apply_backwards(s::Stencil, v::AbstractVector, i::Int)
    w = zero(eltype(v))
    for j ∈ s.range[2]:-1:s.range[1]
        @inbounds weight = s[j]
        w += weight*v[i-j]
    end
    return w
end