ext/isotree/ext.cpp in isotree-0.1.5 vs ext/isotree/ext.cpp in isotree-0.2.0
- old
+ new
@@ -1,81 +1,103 @@
// isotree
#include <isotree.hpp>
// rice
-#include <rice/Array.hpp>
-#include <rice/Hash.hpp>
-#include <rice/Module.hpp>
-#include <rice/Object.hpp>
-#include <rice/String.hpp>
-#include <rice/Symbol.hpp>
+#include <rice/rice.hpp>
using Rice::Array;
using Rice::Hash;
using Rice::Module;
using Rice::Object;
using Rice::String;
using Rice::Symbol;
using Rice::define_class_under;
using Rice::define_module;
-template<>
-NewCategAction from_ruby<NewCategAction>(Object x)
+namespace Rice::detail
{
- auto value = x.to_s().str();
- if (value == "weighted") return Weighted;
- if (value == "smallest") return Smallest;
- if (value == "random") return Random;
- throw std::runtime_error("Unknown new categ action: " + value);
-}
+ template<>
+ class From_Ruby<NewCategAction>
+ {
+ public:
+ NewCategAction convert(VALUE x)
+ {
+ auto value = Object(x).to_s().str();
+ if (value == "weighted") return Weighted;
+ if (value == "smallest") return Smallest;
+ if (value == "random") return Random;
+ throw std::runtime_error("Unknown new categ action: " + value);
+ }
+ };
-template<>
-MissingAction from_ruby<MissingAction>(Object x)
-{
- auto value = x.to_s().str();
- if (value == "divide") return Divide;
- if (value == "impute") return Impute;
- if (value == "fail") return Fail;
- throw std::runtime_error("Unknown missing action: " + value);
-}
+ template<>
+ class From_Ruby<MissingAction>
+ {
+ public:
+ MissingAction convert(VALUE x)
+ {
+ auto value = Object(x).to_s().str();
+ if (value == "divide") return Divide;
+ if (value == "impute") return Impute;
+ if (value == "fail") return Fail;
+ throw std::runtime_error("Unknown missing action: " + value);
+ }
+ };
-template<>
-CategSplit from_ruby<CategSplit>(Object x)
-{
- auto value = x.to_s().str();
- if (value == "subset") return SubSet;
- if (value == "single_categ") return SingleCateg;
- throw std::runtime_error("Unknown categ split: " + value);
-}
+ template<>
+ class From_Ruby<CategSplit>
+ {
+ public:
+ CategSplit convert(VALUE x)
+ {
+ auto value = Object(x).to_s().str();
+ if (value == "subset") return SubSet;
+ if (value == "single_categ") return SingleCateg;
+ throw std::runtime_error("Unknown categ split: " + value);
+ }
+ };
-template<>
-CoefType from_ruby<CoefType>(Object x)
-{
- auto value = x.to_s().str();
- if (value == "uniform") return Uniform;
- if (value == "normal") return Normal;
- throw std::runtime_error("Unknown coef type: " + value);
-}
+ template<>
+ class From_Ruby<CoefType>
+ {
+ public:
+ CoefType convert(VALUE x)
+ {
+ auto value = Object(x).to_s().str();
+ if (value == "uniform") return Uniform;
+ if (value == "normal") return Normal;
+ throw std::runtime_error("Unknown coef type: " + value);
+ }
+ };
-template<>
-UseDepthImp from_ruby<UseDepthImp>(Object x)
-{
- auto value = x.to_s().str();
- if (value == "lower") return Lower;
- if (value == "higher") return Higher;
- if (value == "same") return Same;
- throw std::runtime_error("Unknown depth imp: " + value);
-}
+ template<>
+ class From_Ruby<UseDepthImp>
+ {
+ public:
+ UseDepthImp convert(VALUE x)
+ {
+ auto value = Object(x).to_s().str();
+ if (value == "lower") return Lower;
+ if (value == "higher") return Higher;
+ if (value == "same") return Same;
+ throw std::runtime_error("Unknown depth imp: " + value);
+ }
+ };
-template<>
-WeighImpRows from_ruby<WeighImpRows>(Object x)
-{
- auto value = x.to_s().str();
- if (value == "inverse") return Inverse;
- if (value == "prop") return Prop;
- if (value == "flat") return Flat;
- throw std::runtime_error("Unknown weight imp rows: " + value);
+ template<>
+ class From_Ruby<WeighImpRows>
+ {
+ public:
+ WeighImpRows convert(VALUE x)
+ {
+ auto value = Object(x).to_s().str();
+ if (value == "inverse") return Inverse;
+ if (value == "prop") return Prop;
+ if (value == "flat") return Flat;
+ throw std::runtime_error("Unknown weight imp rows: " + value);
+ }
+ };
}
extern "C"
void Init_ext()
{
@@ -83,13 +105,13 @@
Module rb_mExt = define_module_under(rb_mIsoTree, "Ext");
define_class_under<ExtIsoForest>(rb_mExt, "ExtIsoForest");
rb_mExt
- .define_singleton_method(
+ .define_singleton_function(
"fit_iforest",
- *[](Hash options) {
+ [](Hash options) {
// model
ExtIsoForest iso;
// data
size_t nrows = options.get<size_t, Symbol>("nrows");
@@ -202,13 +224,13 @@
nthreads
);
return iso;
})
- .define_singleton_method(
+ .define_singleton_function(
"predict_iforest",
- *[](ExtIsoForest& iso, Hash options) {
+ [](ExtIsoForest& iso, Hash options) {
// data
size_t nrows = options.get<size_t, Symbol>("nrows");
size_t ncols_numeric = options.get<size_t, Symbol>("ncols_numeric");
size_t ncols_categ = options.get<size_t, Symbol>("ncols_categ");
@@ -258,22 +280,22 @@
for (size_t i = 0; i < outlier_scores.size(); i++) {
ret.push(outlier_scores[i]);
}
return ret;
})
- .define_singleton_method(
+ .define_singleton_function(
"serialize_ext_isoforest",
- *[](ExtIsoForest& iso, String path) {
+ [](ExtIsoForest& iso, String path) {
#ifdef _MSC_VER
// TODO convert to wchar_t
throw std::runtime_error("Not supported on Windows yet");
#else
serialize_ext_isoforest(iso, path.c_str());
#endif
})
- .define_singleton_method(
+ .define_singleton_function(
"deserialize_ext_isoforest",
- *[](String path) {
+ [](String path) {
ExtIsoForest iso;
#ifdef _MSC_VER
// TODO convert to wchar_t
throw std::runtime_error("Not supported on Windows yet");