Skip to content

Commit

Permalink
Refactor constraint in rust binding a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
jkl1337 committed Mar 8, 2024
1 parent 7a09429 commit 717a0b1
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 270 deletions.
29 changes: 22 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ endif
COVERAGE_FLAGS := --coverage
LTO_FLAGS := -flto=auto

rust_dylib_name := librjkiwi.so

ifeq ($(OS),Windows_NT)
is_clang = $(filter %clang++,$(CXX))
is_gcc = $(filter %g++,$(CXX))

ifdef FSANITIZE
$(error "FSANITIZE is not supported on Windows")
endif

rust_dylib_name := rjkiwi.dll
else
uname_s := $(shell uname -s)
ifeq ($(uname_s),Darwin)
Expand All @@ -35,6 +39,7 @@ else
CC := env MACOSX_DEPLOYMENT_TARGET=11.0 gcc
CXX := env MACOSX_DEPLOYMENT_TARGET=11.0 g++
LIBFLAG := -bundle -undefined dynamic_lookup
rust_dylib_name := librjkiwi.dylib

else
is_clang = $(filter %clang++,$(CXX))
Expand Down Expand Up @@ -105,6 +110,8 @@ ifneq ($(LUA_VERSION),5.1)
endif
endif

rust_lib_srcs := expr.rs lib.rs solver.rs util.rs var.rs Cargo.toml Cargo.lock

kiwi_lib_srcs := AssocVector.h constraint.h debug.h errors.h expression.h kiwi.h maptype.h \
row.h shareddata.h solver.h solverimpl.h strength.h symbol.h symbolics.h term.h \
util.h variable.h version.h
Expand All @@ -118,18 +125,17 @@ endif

vpath %.cpp $(SRCDIR)/ckiwi $(SRCDIR)/luakiwi
vpath %.h $(SRCDIR)/ckiwi $(SRCDIR)/luakiwi $(SRCDIR)/kiwi/kiwi
vpath %.rs $(SRCDIR)/rjkiwi/src
vpath Cargo.% $(SRCDIR)/rjkiwi

all: ljkiwi.$(LIB_EXT)
all: ljkiwi.$(LIB_EXT) $(if $(FRUST),rjkiwi.$(LIB_EXT))

install:
$(CP) -f ljkiwi.$(LIB_EXT) $(INST_LIBDIR)/ljkiwi.$(LIB_EXT)
$(CP) -f ljkiwi.$(LIB_EXT) rjkiwi.$(LIB_EXT) $(INST_LIBDIR)/
$(CP) -f kiwi.lua $(INST_LUADIR)/kiwi.lua

mostlyclean:
$(RM) -f ljkiwi.$(LIB_EXT) $(objs) $(objs:.o=.gcda) $(objs:.o=.gcno)

clean: mostlyclean
$(RM) -f $(PCH)
clean:
$(RM) -f ljkiwi.$(LIB_EXT) rjkiwi.$(LIB_EXT) $(objs) $(objs:.o=.gcda) $(objs:.o=.gcno)

ckiwi.o: ckiwi.cpp ckiwi.h $(kiwi_lib_srcs)
luakiwi.o: luakiwi-int.h luacompat.h $(kiwi_lib_srcs)
Expand All @@ -146,4 +152,13 @@ ljkiwi.$(LIB_EXT): $(objs)
%.o: %.cpp
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<

rjkiwi.$(LIB_EXT): rjkiwi/target/$(if $(FDEBUG),debug,release)/$(rust_dylib_name)
$(CP) -f $< $@

rjkiwi/target/debug/$(rust_dylib_name): $(rust_lib_srcs)
cd rjkiwi && cargo build

rjkiwi/target/release/$(rust_dylib_name): $(rust_lib_srcs)
cd rjkiwi && cargo build --release

.PHONY: all install clean mostlyclean
125 changes: 75 additions & 50 deletions kiwi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,41 @@ do
)
end

ffi.cdef([[
typedef struct KiwiVar KiwiVar;
enum KiwiRelOp { LE, GE, EQ };
typedef struct KiwiTerm {
KiwiVar* var;
double coefficient;
} KiwiTerm;
typedef struct KiwiExpression {
double constant;
int term_count;
void* owner;
KiwiTerm terms_[?];
} KiwiExpression;
void kiwi_expression_retain(KiwiExpression* expr);
void kiwi_expression_destroy(KiwiExpression* expr);
]])

if RUST then
ffi.cdef("void kiwi_constraint_type_info(unsigned sz_align[2]);")
local ti = ffi.new("unsigned[2]")
ljkiwi.kiwi_constraint_type_info(ti)
ffi.cdef(
"typedef struct KiwiConstraint { unsigned char b_[$]; } __attribute__((aligned($))) KiwiConstraint;",
ti[0],
ti[1]
)
ffi.cdef([[
typedef struct KiwiExpression KiwiExpression;
typedef struct KiwiConstraint {
double constant;
int term_count;
enum KiwiRelOp op_;
double strength_;
void* owner;
KiwiTerm terms_[?];
} KiwiConstraint;
void kiwi_constraint_init(
KiwiConstraint* c,
Expand All @@ -69,12 +93,25 @@ struct KiwiVar {
const char* name_;
};
]])
else
ffi.cdef([[
typedef struct KiwiConstraint KiwiConstraint;
KiwiConstraint* kiwi_constraint_new(
const KiwiExpression* lhs,
const KiwiExpression* rhs,
enum KiwiRelOp op,
double strength
);
void kiwi_constraint_release(KiwiConstraint* c);
void kiwi_constraint_retain(KiwiConstraint* c);
double kiwi_constraint_strength(const KiwiConstraint* c);
enum KiwiRelOp kiwi_constraint_op(const KiwiConstraint* c);
]])
end

ffi.cdef([[
typedef struct KiwiConstraint KiwiConstraint;
typedef struct KiwiVar KiwiVar;
enum KiwiErrKind {
KiwiErrNone,
KiwiErrUnsatisfiableConstraint = 1,
Expand All @@ -89,21 +126,6 @@ enum KiwiErrKind {
KiwiErrUnknown,
};
enum KiwiRelOp { LE, GE, EQ };
typedef struct KiwiTerm {
KiwiVar* var;
double coefficient;
} KiwiTerm;
typedef struct KiwiExpression {
double constant;
int term_count;
void* owner;
KiwiTerm terms_[?];
} KiwiExpression;
typedef struct KiwiErr {
enum KiwiErrKind kind;
const char* message;
Expand All @@ -124,20 +146,6 @@ void kiwi_var_set_name(KiwiVar* var, const char* name);
double kiwi_var_value(const KiwiVar* var);
void kiwi_var_set_value(KiwiVar* var, double value);
void kiwi_expression_retain(KiwiExpression* expr);
void kiwi_expression_destroy(KiwiExpression* expr);
KiwiConstraint* kiwi_constraint_new(
const KiwiExpression* lhs,
const KiwiExpression* rhs,
enum KiwiRelOp op,
double strength
);
void kiwi_constraint_release(KiwiConstraint* c);
void kiwi_constraint_retain(KiwiConstraint* c);
double kiwi_constraint_strength(const KiwiConstraint* c);
enum KiwiRelOp kiwi_constraint_op(const KiwiConstraint* c);
bool kiwi_constraint_violated(const KiwiConstraint* c);
int kiwi_constraint_expression(KiwiConstraint* c, KiwiExpression* out, int out_size);
Expand Down Expand Up @@ -251,7 +259,7 @@ local new_constraint
if RUST then
---@return kiwi.Constraint
function new_constraint(lhs, rhs, op, strength)
local c = ffi_new(Constraint)
local c = ffi_new(Constraint, (lhs and lhs.term_count or 0) + (rhs and rhs.term_count or 0))
ljkiwi.kiwi_constraint_init(c, lhs, rhs, op or "EQ", strength or REQUIRED)
return ffi_gc(c, ljkiwi.kiwi_constraint_destroy) --[[@as kiwi.Constraint]]
end
Expand Down Expand Up @@ -813,21 +821,38 @@ do
--- Constraints can be built with arbitrary left and right hand expressions. But
--- ultimately they all have the form `expression [op] 0`.
---@class kiwi.Constraint: ffi.cdata*
---@field package constant number
---@field package term_count number
---@field package owner ffi.cdata*
---@field package op_ kiwi.RelOp
---@field package strength_ number
---@field package terms_ ffi.cdata*
---@overload fun(lhs: kiwi.Expression?, rhs: kiwi.Expression?, op: kiwi.RelOp?, strength: number?): kiwi.Constraint
local Constraint_cls = {
--- The strength of the constraint.
---@type fun(self: kiwi.Constraint): number
strength = ljkiwi.kiwi_constraint_strength,

--- The relational operator of the constraint.
---@type fun(self: kiwi.Constraint): kiwi.RelOp
op = ljkiwi.kiwi_constraint_op,

--- Whether the constraint is violated in the current solution.
---@type fun(self: kiwi.Constraint): boolean
violated = ljkiwi.kiwi_constraint_violated,
}

if RUST then
--- The strength of the constraint.
---@return number
---@nodiscard
function Constraint_cls:strength()
return self.strength_
end

--- The relational operator of the constraint.
---@return kiwi.RelOp
---@nodiscard
function Constraint_cls:op()
return self.op_
end
else
Constraint_cls.strength = ljkiwi.kiwi_constraint_strength
Constraint_cls.op = ljkiwi.kiwi_constraint_op
end

--- The reduced expression defining the constraint.
---@return kiwi.Expression
---@nodiscard
Expand Down
Loading

0 comments on commit 717a0b1

Please sign in to comment.