Skip to content

Commit

Permalink
feat: add support for inplace getp
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushSabharwal committed Feb 21, 2024
1 parent 46070b2 commit 4f5758c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/parameter_indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ end

function _getp(sys, ::ScalarSymbolic, ::SymbolicTypeTrait, p)
idx = parameter_index(sys, p)
return function getter(sol)
return parameter_values(sol, idx)
return let idx = idx
function getter(sol)
return parameter_values(sol, idx)
end
end
end

Expand All @@ -70,8 +72,16 @@ for (t1, t2) in [
@eval function _getp(sys, ::NotSymbolic, ::$t1, p::$t2)
getters = getp.((sys,), p)

return function getter(sol)
map(g -> g(sol), getters)
return let getters = getters
function getter(sol)
map(g -> g(sol), getters)
end
function getter(buffer, sol)
for (i, g) in zip(eachindex(buffer), getters)
buffer[i] = g(sol)
end
buffer

Check warning on line 83 in src/parameter_indexing.jl

View check run for this annotation

Codecov / codecov/patch

src/parameter_indexing.jl#L80-L83

Added lines #L80 - L83 were not covered by tests
end
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions test/parameter_indexing_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,15 @@ for (sym, oldval, newval, check_inference) in [
set!(p, oldval)
@test get(p) == oldval
end

for (sym, val) in [
([:a, :b, :c], p),
([:c, :a], p[[3, 1]]),
((:b, :a), p[[2, 1]]),
((1, :c), p[[1, 3]])
]
buffer = zeros(length(sym))
get = getp(sys, sym)
@inferred get(buffer, fi)
@test buffer == val
end

0 comments on commit 4f5758c

Please sign in to comment.