Replace a few loops with ffi.copy where possible
This commit is contained in:
18
kiwi.lua
18
kiwi.lua
@@ -175,6 +175,7 @@ function kiwi.is_var(o)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local Term = ffi.typeof("struct KiwiTerm") --[[@as kiwi.Term]]
|
local Term = ffi.typeof("struct KiwiTerm") --[[@as kiwi.Term]]
|
||||||
|
local SIZEOF_TERM = assert(ffi.sizeof(Term))
|
||||||
kiwi.Term = Term
|
kiwi.Term = Term
|
||||||
|
|
||||||
function kiwi.is_term(o)
|
function kiwi.is_term(o)
|
||||||
@@ -201,12 +202,7 @@ end
|
|||||||
---@nodiscard
|
---@nodiscard
|
||||||
local function add_expr_term(expr, var, coeff)
|
local function add_expr_term(expr, var, coeff)
|
||||||
local ret = ffi_gc(ffi_new(Expression, expr.term_count + 1), ljkiwi.kiwi_expression_destroy) --[[@as kiwi.Expression]]
|
local ret = ffi_gc(ffi_new(Expression, expr.term_count + 1), ljkiwi.kiwi_expression_destroy) --[[@as kiwi.Expression]]
|
||||||
for i = 0, expr.term_count - 1 do
|
ffi_copy(ret.terms_, expr.terms_, SIZEOF_TERM * expr.term_count)
|
||||||
local st = expr.terms_[i] --[[@as kiwi.Term]]
|
|
||||||
local dt = ret.terms_[i] --[[@as kiwi.Term]]
|
|
||||||
dt.var = st.var
|
|
||||||
dt.coefficient = st.coefficient
|
|
||||||
end
|
|
||||||
local dt = ret.terms_[expr.term_count]
|
local dt = ret.terms_[expr.term_count]
|
||||||
dt.var = var
|
dt.var = var
|
||||||
dt.coefficient = coeff or 1.0
|
dt.coefficient = coeff or 1.0
|
||||||
@@ -282,8 +278,6 @@ local OP_NAMES = {
|
|||||||
EQ = "==",
|
EQ = "==",
|
||||||
}
|
}
|
||||||
|
|
||||||
local SIZEOF_TERM = ffi.sizeof(Term) --[[@as integer]]
|
|
||||||
|
|
||||||
local tmpexpr = ffi_new(Expression, 2) --[[@as kiwi.Expression]]
|
local tmpexpr = ffi_new(Expression, 2) --[[@as kiwi.Expression]]
|
||||||
local tmpexpr_r = ffi_new(Expression, 1) --[[@as kiwi.Expression]]
|
local tmpexpr_r = ffi_new(Expression, 1) --[[@as kiwi.Expression]]
|
||||||
|
|
||||||
@@ -622,13 +616,7 @@ do
|
|||||||
---@nodiscard
|
---@nodiscard
|
||||||
local function new_expr_constant(expr, constant)
|
local function new_expr_constant(expr, constant)
|
||||||
local ret = ffi_gc(ffi_new(Expression, expr.term_count), ljkiwi.kiwi_expression_destroy) --[[@as kiwi.Expression]]
|
local ret = ffi_gc(ffi_new(Expression, expr.term_count), ljkiwi.kiwi_expression_destroy) --[[@as kiwi.Expression]]
|
||||||
|
ffi_copy(ret.terms_, expr.terms_, SIZEOF_TERM * expr.term_count)
|
||||||
for i = 0, expr.term_count - 1 do
|
|
||||||
local dt = ret.terms_[i] --[[@as kiwi.Term]]
|
|
||||||
local st = expr.terms_[i] --[[@as kiwi.Term]]
|
|
||||||
dt.var = st.var
|
|
||||||
dt.coefficient = st.coefficient
|
|
||||||
end
|
|
||||||
ret.constant = constant
|
ret.constant = constant
|
||||||
ret.term_count = expr.term_count
|
ret.term_count = expr.term_count
|
||||||
ljkiwi.kiwi_expression_retain(ret)
|
ljkiwi.kiwi_expression_retain(ret)
|
||||||
|
|||||||
Reference in New Issue
Block a user