-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
64 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,66 @@ | ||
#include <rice/rice.hpp> | ||
#include <rice/stl.hpp> | ||
#include <ruby.h> | ||
|
||
#include "bayestest.hpp" | ||
|
||
using bayestest::BinaryTest; | ||
|
||
void binary_test_free(void* data) | ||
{ | ||
free(data); | ||
} | ||
|
||
size_t binary_test_size(const void* data) | ||
{ | ||
// does not currently include size of variants | ||
return sizeof(BinaryTest); | ||
} | ||
|
||
static const rb_data_type_t binary_test_type = { | ||
.wrap_struct_name = "binary_test", | ||
.function = { | ||
.dmark = NULL, | ||
.dfree = binary_test_free, | ||
.dsize = binary_test_size, | ||
}, | ||
.data = NULL, | ||
.flags = RUBY_TYPED_FREE_IMMEDIATELY, | ||
}; | ||
|
||
VALUE binary_test_alloc(VALUE self) | ||
{ | ||
BinaryTest* data = new BinaryTest(); | ||
|
||
return TypedData_Wrap_Struct(self, &binary_test_type, data); | ||
} | ||
|
||
VALUE binary_test_add(VALUE self, VALUE participants, VALUE conversions) | ||
{ | ||
BinaryTest* data; | ||
TypedData_Get_Struct(self, BinaryTest, &binary_test_type, data); | ||
|
||
data->add(NUM2INT(participants), NUM2INT(conversions)); | ||
|
||
return Qnil; | ||
} | ||
|
||
VALUE binary_test_probabilities(VALUE self) | ||
{ | ||
BinaryTest* data; | ||
TypedData_Get_Struct(self, BinaryTest, &binary_test_type, data); | ||
|
||
VALUE arr = rb_ary_new(); | ||
for (auto &v : data->probabilities()) { | ||
rb_ary_push(arr, DBL2NUM(v)); | ||
} | ||
return arr; | ||
} | ||
|
||
extern "C" | ||
void Init_ext() { | ||
auto rb_mFieldTest = Rice::define_module("FieldTest"); | ||
auto mFieldTest = rb_define_module("FieldTest"); | ||
|
||
Rice::define_class_under<BinaryTest>(rb_mFieldTest, "BinaryTest") | ||
.define_constructor(Rice::Constructor<BinaryTest>()) | ||
.define_method("add", &BinaryTest::add) | ||
.define_method("probabilities", &BinaryTest::probabilities); | ||
auto cBinaryTest = rb_define_class_under(mFieldTest, "BinaryTest", rb_cObject); | ||
rb_define_alloc_func(cBinaryTest, binary_test_alloc); | ||
rb_define_method(cBinaryTest, "add", binary_test_add, 2); | ||
rb_define_method(cBinaryTest, "probabilities", binary_test_probabilities, 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
require "mkmf-rice" | ||
require "mkmf" | ||
|
||
$CXXFLAGS << " -std=c++17 $(optflags)" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters