module firmod

type FIR{in_t,coef_t}
    in::Vector{in_t}
    coef::Vector{coef_t}
    function FIR (coef::Vector{coef_t})
        new (zeros (in_t, size (coef)), copy (coef))
    end
end

  ## template<typename i_t>
  ## void shift (i_t i) {
  ##   std::copy_backward (in.begin(), in.end()-boost::size(i), in.end());
  ##   std::reverse_copy (i.begin(), i.end(), in.begin());
  ## }
function shift!{in_t,coef_t} (f::FIR{in_t,coef_t}, u::Vector{in_t},
                              start, stop)
    si = length(f.in)
    su = stop - start + 1
    for i =si-su:-1:1
        f.in[i+su] = f.in[i]
    end

    for i = 1:su
        f.in[i] = u[stop - i + 1]
    end
end

function compute1{in_t, coef_t} (f::FIR{in_t,coef_t})
    s = zero (promote_type (in_t, coef_t))
    size = length(f.coef)
    for i = 1:size
        s += f.in[i] * f.coef[i]
    end
    return s
end

function shift_compute1{in_t,coef_t} (f::FIR{in_t,coef_t}, u::Vector{in_t},
                                      start, stop)
    shift! (f, u, start, stop)
    return compute1 (f)
end

function compute{in_t,coef_t} (f::FIR{in_t,coef_t}, u::Vector{in_t})
    out = similar (u, promote_type (in_t,coef_t))
    size = length (u)
    for i = 1:size
        out[i] = shift_compute1 (f, u, i, i)
    end
    return out
end

const c = float ([1:4])
const g = FIR{Float64,Float64} (c)
const h = FIR{Complex{Float64},Float64} (c)
const j = float ([1:10])
const k = compute (g, j)

function timeit()
    n = 10
    l = 1000000
    u = zeros(Float64, l)

    for i=1:n
        v = compute (g, u)
    end
end


end

