Skip to content

Commit

Permalink
Only allow adding data when safetycopy = false
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Jul 3, 2024
1 parent 8410033 commit 7cb9ef8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/DataInterpolations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ function Base.showerror(io::IO, e::MustCopyError)
print(io, MUST_COPY_ERROR)
end

const UNSAFE_ADD_DATA_ERROR = "Can only add data to the interpolation if safetycopy = false."
struct UnsafeAddDataError <: Exception end
function Base.showerror(io::IO, e::UnsafeAddDataError)
print(io, UNSAFE_ADD_DATA_ERROR)
end

export LinearInterpolation, QuadraticInterpolation, LagrangeInterpolation,
AkimaInterpolation, ConstantInterpolation, QuadraticSpline, CubicSpline,
BSplineInterpolation, BSplineApprox, CubicHermiteSpline,
Expand Down
6 changes: 6 additions & 0 deletions src/online.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Base: append!, push!

function push!(A::LinearInterpolation{U, T}, u::eltype(U), t::eltype(T)) where {U, T}
A.safetycopy && throw(UnsafeAddDataError())
push!(A.u.parent, u)
push!(A.t.parent, t)
slope = LinearInterpolationParameters(A.u, A.t, length(A.t) - 1)
Expand All @@ -9,6 +10,7 @@ function push!(A::LinearInterpolation{U, T}, u::eltype(U), t::eltype(T)) where {
end

function push!(A::QuadraticInterpolation{U, T}, u::eltype(U), t::eltype(T)) where {U, T}
A.safetycopy && throw(UnsafeAddDataError())
push!(A.u.parent, u)
push!(A.t.parent, t)
l₀, l₁, l₂ = QuadraticInterpolationParameters(A.u, A.t, length(A.t) - 2)
Expand All @@ -19,6 +21,7 @@ function push!(A::QuadraticInterpolation{U, T}, u::eltype(U), t::eltype(T)) wher
end

function push!(A::ConstantInterpolation{U, T}, u::eltype(U), t::eltype(T)) where {U, T}
A.safetycopy && throw(UnsafeAddDataError())
push!(A.u.parent, u)
push!(A.t.parent, t)
A
Expand All @@ -27,6 +30,7 @@ end
function append!(
A::LinearInterpolation{ReadOnlyVector{eltypeU, U}, ReadOnlyVector{eltypeT, T}}, u::U, t::T) where {
eltypeU, U, eltypeT, T}
A.safetycopy && throw(UnsafeAddDataError())
length_old = length(A.t)
u, t = munge_data(u, t, true)
append!(A.u.parent, u)
Expand All @@ -40,6 +44,7 @@ end
function append!(
A::ConstantInterpolation{ReadOnlyVector{eltypeU, U}, ReadOnlyVector{eltypeT, T}}, u::U, t::T) where {
eltypeU, U, eltypeT, T}
A.safetycopy && throw(UnsafeAddDataError())
u, t = munge_data(u, t, true)
append!(A.u.parent, u)
append!(A.t.parent, t)
Expand All @@ -49,6 +54,7 @@ end
function append!(
A::QuadraticInterpolation{ReadOnlyVector{eltypeU, U}, ReadOnlyVector{eltypeT, T}}, u::U, t::T) where {
eltypeU, U, eltypeT, T}
A.safetycopy && throw(UnsafeAddDataError())
length_old = length(A.t)
u, t = munge_data(u, t, true)
append!(A.u.parent, u)
Expand Down
2 changes: 1 addition & 1 deletion test/online_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ u2 = Float64[1, 2, 1]
ts = 1.0:0.5:6.0

for method in [LinearInterpolation, QuadraticInterpolation, ConstantInterpolation]
func1 = method(u1, t1)
func1 = method(u1, t1; safetycopy = false)
append!(func1, u2, t2)
func2 = method(vcat(u1, u2), vcat(t1, t2))
@test func1.u == func2.u
Expand Down

0 comments on commit 7cb9ef8

Please sign in to comment.