Minor fixes and API change
- fix stupid spaces in Makefile - small type annotation fix - Modify Expression constructor API to use varargs - Update kiwi.lua to support future C API module
This commit is contained in:
6
Makefile
6
Makefile
@@ -5,14 +5,14 @@ CFLAGS += -Wall -I$(SRCDIR)/kiwi
|
|||||||
LIBFLAG := -shared
|
LIBFLAG := -shared
|
||||||
LIB_EXT := so
|
LIB_EXT := so
|
||||||
|
|
||||||
ifeq ($(findstring gcc, $(CC)), gcc)
|
ifeq ($(findstring gcc,$(CC)),gcc)
|
||||||
CXX := $(subst gcc, g++, $(CC))
|
CXX := $(subst gcc,g++,$(CC))
|
||||||
CXXFLAGS += -std=c++14
|
CXXFLAGS += -std=c++14
|
||||||
ifneq ($(SANITIZE),)
|
ifneq ($(SANITIZE),)
|
||||||
CFLAGS += -fsanitize=undefined -fsanitize=address
|
CFLAGS += -fsanitize=undefined -fsanitize=address
|
||||||
endif
|
endif
|
||||||
else
|
else
|
||||||
ifeq ($(CC), clang)
|
ifeq ($(CC),clang)
|
||||||
CXX := clang++
|
CXX := clang++
|
||||||
CXXFLAGS += -std=c++14
|
CXXFLAGS += -std=c++14
|
||||||
ifneq ($(SANITIZE),)
|
ifneq ($(SANITIZE),)
|
||||||
|
|||||||
45
kiwi.lua
45
kiwi.lua
@@ -1,6 +1,15 @@
|
|||||||
local kiwi = {}
|
-- kiwi.lua - LuaJIT FFI bindings with C API fallback to kiwi constraint solver.
|
||||||
|
|
||||||
local ffi = require("ffi")
|
local ffi
|
||||||
|
do
|
||||||
|
local ffi_loader = package.preload["ffi"]
|
||||||
|
if ffi_loader == nil then
|
||||||
|
return require("ckiwi")
|
||||||
|
end
|
||||||
|
ffi = ffi_loader() --[[@as ffilib]]
|
||||||
|
end
|
||||||
|
|
||||||
|
local kiwi = {}
|
||||||
|
|
||||||
local ckiwi
|
local ckiwi
|
||||||
do
|
do
|
||||||
@@ -101,7 +110,7 @@ local ffi_copy, ffi_gc, ffi_istype, ffi_new, ffi_string =
|
|||||||
local concat = table.concat
|
local concat = table.concat
|
||||||
local has_table_new, new_tab = pcall(require, "table.new")
|
local has_table_new, new_tab = pcall(require, "table.new")
|
||||||
if not has_table_new or type(new_tab) ~= "function" then
|
if not has_table_new or type(new_tab) ~= "function" then
|
||||||
new_tab = function()
|
new_tab = function(_, _)
|
||||||
return {}
|
return {}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -532,7 +541,7 @@ end
|
|||||||
do
|
do
|
||||||
--- Expressions are a sum of terms with an added constant.
|
--- Expressions are a sum of terms with an added constant.
|
||||||
---@class kiwi.Expression: ffi.cdata*
|
---@class kiwi.Expression: ffi.cdata*
|
||||||
---@overload fun(terms: kiwi.Term[], constant: number?): kiwi.Expression
|
---@overload fun(constant: number, ...: kiwi.Term): kiwi.Expression
|
||||||
---@field constant number
|
---@field constant number
|
||||||
---@field package term_count number
|
---@field package term_count number
|
||||||
---@field package terms_ ffi.cdata*
|
---@field package terms_ ffi.cdata*
|
||||||
@@ -610,7 +619,8 @@ do
|
|||||||
function Expression_cls:value()
|
function Expression_cls:value()
|
||||||
local sum = self.constant
|
local sum = self.constant
|
||||||
for i = 0, self.term_count - 1 do
|
for i = 0, self.term_count - 1 do
|
||||||
sum = sum + self.terms_[i]:value()
|
local t = self.terms_[i]
|
||||||
|
sum = sum + t.var:value() * t.coefficient
|
||||||
end
|
end
|
||||||
return sum
|
return sum
|
||||||
end
|
end
|
||||||
@@ -636,17 +646,16 @@ do
|
|||||||
__index = Expression_cls,
|
__index = Expression_cls,
|
||||||
}
|
}
|
||||||
|
|
||||||
function Expression_mt.__new(T, terms, constant)
|
function Expression_mt.__new(T, constant, ...)
|
||||||
local term_count = terms and #terms or 0
|
local term_count = select("#", ...)
|
||||||
local e = ffi_gc(ffi_new(T, term_count), ckiwi.kiwi_expression_del_vars) --[[@as kiwi.Expression]]
|
local e = ffi_gc(ffi_new(T, term_count), ckiwi.kiwi_expression_del_vars) --[[@as kiwi.Expression]]
|
||||||
e.term_count = term_count
|
e.term_count = term_count
|
||||||
e.constant = constant or 0.0
|
e.constant = constant
|
||||||
if terms then
|
for i = 1, term_count do
|
||||||
for i, t in ipairs(terms) do
|
local t = select(i, ...)
|
||||||
local dt = e.terms_[i - 1] --[[@as kiwi.Term]]
|
local dt = e.terms_[i - 1] --[[@as kiwi.Term]]
|
||||||
dt.var = ckiwi.kiwi_var_clone(t.var)
|
dt.var = ckiwi.kiwi_var_clone(t.var)
|
||||||
dt.coefficient = t.coefficient
|
dt.coefficient = t.coefficient
|
||||||
end
|
|
||||||
end
|
end
|
||||||
return e
|
return e
|
||||||
end
|
end
|
||||||
@@ -729,7 +738,7 @@ do
|
|||||||
---@return kiwi.Expression
|
---@return kiwi.Expression
|
||||||
---@nodiscard
|
---@nodiscard
|
||||||
function Constraint_cls:expression()
|
function Constraint_cls:expression()
|
||||||
local SZ = 8
|
local SZ = 7 -- 2**7 bytes on x64
|
||||||
local expr = ffi_new(Expression, SZ) --[[@as kiwi.Expression]]
|
local expr = ffi_new(Expression, SZ) --[[@as kiwi.Expression]]
|
||||||
local n = ckiwi.kiwi_constraint_expression(self, expr, SZ)
|
local n = ckiwi.kiwi_constraint_expression(self, expr, SZ)
|
||||||
if n > SZ then
|
if n > SZ then
|
||||||
@@ -865,7 +874,7 @@ do
|
|||||||
--- Produce a custom error raise mask
|
--- Produce a custom error raise mask
|
||||||
--- Error kinds specified in the mask will not cause a lua
|
--- Error kinds specified in the mask will not cause a lua
|
||||||
--- error to be raised.
|
--- error to be raised.
|
||||||
---@param kinds (kiwi.ErrKind|number)[]
|
---@param kinds (kiwi.ErrKind|integer)[]
|
||||||
---@param invert boolean?
|
---@param invert boolean?
|
||||||
---@return integer
|
---@return integer
|
||||||
function kiwi.error_mask(kinds, invert)
|
function kiwi.error_mask(kinds, invert)
|
||||||
@@ -948,7 +957,7 @@ do
|
|||||||
end
|
end
|
||||||
---@class kiwi.Solver: ffi.cdata*
|
---@class kiwi.Solver: ffi.cdata*
|
||||||
---@field package error_mask_ integer
|
---@field package error_mask_ integer
|
||||||
---@overload fun(error_mask: (integer|(kiwi.ErrKind|number)[] )?): kiwi.Solver
|
---@overload fun(error_mask: (integer|(kiwi.ErrKind|integer)[] )?): kiwi.Solver
|
||||||
local Solver_cls = {
|
local Solver_cls = {
|
||||||
--- Test whether a constraint is in the solver.
|
--- Test whether a constraint is in the solver.
|
||||||
---@type fun(self: kiwi.Solver, constraint: kiwi.Constraint): boolean
|
---@type fun(self: kiwi.Solver, constraint: kiwi.Constraint): boolean
|
||||||
@@ -978,7 +987,7 @@ do
|
|||||||
}
|
}
|
||||||
|
|
||||||
--- Sets the error mask for the solver.
|
--- Sets the error mask for the solver.
|
||||||
---@param mask integer|(kiwi.ErrKind|number)[] the mask value or an array of kinds
|
---@param mask integer|(kiwi.ErrKind|integer)[] the mask value or an array of kinds
|
||||||
---@param invert boolean? whether to invert the mask if an array was passed for mask
|
---@param invert boolean? whether to invert the mask if an array was passed for mask
|
||||||
function Solver_cls:set_error_mask(mask, invert)
|
function Solver_cls:set_error_mask(mask, invert)
|
||||||
if type(mask) == "table" then
|
if type(mask) == "table" then
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ describe("Constraint", function()
|
|||||||
c = kiwi.Constraint(lhs / 2, nil, "LE", kiwi.strength.MEDIUM)
|
c = kiwi.Constraint(lhs / 2, nil, "LE", kiwi.strength.MEDIUM)
|
||||||
assert.equal("0.5 foo + 0.5 <= 0 | medium", tostring(c))
|
assert.equal("0.5 foo + 0.5 <= 0 | medium", tostring(c))
|
||||||
|
|
||||||
c = kiwi.Constraint(lhs, kiwi.Expression(nil, 3), "GE", kiwi.strength.WEAK)
|
c = kiwi.Constraint(lhs, kiwi.Expression(3), "GE", kiwi.strength.WEAK)
|
||||||
assert.equal("1 foo + -2 >= 0 | weak", tostring(c))
|
assert.equal("1 foo + -2 >= 0 | weak", tostring(c))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
@@ -68,7 +68,7 @@ describe("Constraint", function()
|
|||||||
end)
|
end)
|
||||||
it("combines lhs and rhs", function()
|
it("combines lhs and rhs", function()
|
||||||
local v2 = kiwi.Var("bar")
|
local v2 = kiwi.Var("bar")
|
||||||
local rhs = kiwi.Expression({ 5 * v2, 3 * v }, 3)
|
local rhs = kiwi.Expression(3, 5 * v2, 3 * v)
|
||||||
local c = kiwi.Constraint(lhs, rhs)
|
local c = kiwi.Constraint(lhs, rhs)
|
||||||
|
|
||||||
local e = c:expression()
|
local e = c:expression()
|
||||||
|
|||||||
Reference in New Issue
Block a user