2 Commits

Author SHA1 Message Date
08e9bf08e7 Initial implementation of tuple demonstration
This allows allocating a buffer of double pointers pointing to
the memory location in a bunch of variables. It is not particularly
ergonomic as is, and it seems unlikely the real world performance
benefit will exist.
2024-02-27 21:59:10 -06:00
ef29b8abcb unprivate kiwi::VariableData 2024-02-27 21:53:16 -06:00
5 changed files with 67 additions and 3 deletions

View File

@@ -93,7 +93,7 @@ else
endif
override CPPFLAGS += -I$(SRCDIR) -I$(SRCDIR)/kiwi -I"$(LUA_INCDIR)"
override CXXFLAGS += -std=c++14 -fno-rtti $(CCFLAGS)
override CXXFLAGS += -std=c++17 -fno-rtti $(CCFLAGS)
ifeq ($(OS),Windows_NT)
override CPPFLAGS += -DLUA_BUILD_AS_DLL

View File

@@ -3,6 +3,7 @@
#include <kiwi/kiwi.h>
#include <climits>
#include <cstdarg>
#include <cstdlib>
#include <cstring>
#include <string>
@@ -373,4 +374,31 @@ char* kiwi_solver_dumps(const KiwiSolver* s) {
return buf;
}
void kiwi_tuple_init(KiwiTuple* tuple, int count, ...) {
if (lk_unlikely(!tuple))
return;
va_list args;
va_start(args, count);
for (int i = 0; i < count; ++i) {
auto* var = va_arg(args, KiwiVar*);
retain_unmanaged(var);
tuple->values[i] = &var->m_value;
}
tuple->count = count;
va_end(args);
}
void kiwi_tuple_destroy(KiwiTuple* tuple) {
if (lk_unlikely(!tuple))
return;
for (int i = 0; i < tuple->count; ++i) {
auto* value = const_cast<double*>(tuple->values[i]);
release_unmanaged(
reinterpret_cast<KiwiVar*>(reinterpret_cast<char*>(value) - offsetof(KiwiVar, m_value))
);
}
}
} // extern "C"

View File

@@ -61,7 +61,7 @@ typedef struct KiwiExpression {
#if defined(LJKIWI_LUAJIT_DEF)
KiwiTerm terms_[?];
#elif defined(LJKIWI_USE_FAM_1)
KiwiTerm terms_[1]; // LuaJIT: struct KiwiTerm terms_[?];
KiwiTerm terms_[1];
#else
KiwiTerm terms_[];
#endif
@@ -74,8 +74,22 @@ typedef struct KiwiErr {
bool must_free;
} KiwiErr;
typedef struct KiwiTuple {
int count;
#if defined(LJKIWI_LUAJIT_DEF)
const double* values[?];
#elif defined(LJKIWI_USE_FAM_1)
const double* values[1];
#else
const double* values[];
#endif
} KiwiTuple;
struct KiwiSolver;
LJKIWI_EXP void kiwi_tuple_init(KiwiTuple* tuple, int count, ...);
LJKIWI_EXP void kiwi_tuple_destroy(KiwiTuple* tuple);
LJKIWI_EXP KiwiVar* kiwi_var_construct(const char* name);
LJKIWI_EXP void kiwi_var_release(KiwiVar* var);
LJKIWI_EXP void kiwi_var_retain(KiwiVar* var);

View File

@@ -64,8 +64,16 @@ typedef struct KiwiErr {
bool must_free;
} KiwiErr;
typedef struct KiwiTuple {
int count;
const double* values[?];
} KiwiTuple;
struct KiwiSolver;
void kiwi_tuple_init(KiwiTuple* tuple, int count, ...);
void kiwi_tuple_destroy(KiwiTuple* tuple);
KiwiVar* kiwi_var_construct(const char* name);
void kiwi_var_release(KiwiVar* var);
void kiwi_var_retain(KiwiVar* var);
@@ -196,6 +204,21 @@ function kiwi.is_constraint(o)
return ffi_istype(Constraint, o)
end
local Tuple = ffi.typeof("struct KiwiTuple") --[[@as kiwi.Tuple]]
kiwi.Tuple = Tuple
---@class kiwi.Tuple: ffi.cdata*
---@field values ffi.cdata*
---@field count integer
---@overload fun(vars: kiwi.Var[]): kiwi.Tuple
ffi.metatype(Tuple, {
__new = function(self, vars)
local t = ffi_new(self, #vars)
ljkiwi.kiwi_tuple_init(t, #vars, unpack(vars))
return ffi_gc(t, ljkiwi.kiwi_tuple_destroy)
end,
})
---@param expr kiwi.Expression
---@param var kiwi.Var
---@param coeff number?

View File

@@ -33,7 +33,6 @@ public:
double value() const { return m_value; }
void setValue(double value) { m_value = value; }
private:
std::string m_name;
double m_value;