Skip to content

Commit

Permalink
feat(inclose5): make inclose5 C++ implementation work
Browse files Browse the repository at this point in the history
  • Loading branch information
kalitine committed Sep 20, 2020
1 parent 6c0b689 commit 1055d47
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
9 changes: 8 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
"target_name": "inclose5",
"cflags!": [ "-fno-exceptions" ],
"cflags_cc!": [ "-fno-exceptions" ],
"sources": [ "src/index.cc" ],
"sources": [
"src/index.cc",
"lib/inclose5/BicsUtils.cpp",
"lib/inclose5/globalsv.cpp",
"lib/inclose5/inclose.cpp",
"lib/inclose5/stdafx.cpp",
"lib/inclose5/inclose5.cpp"
],
"include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")"
],
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"lib": "lib"
},
"scripts": {
"compile": "node-gyp configure & node-gyp build",
"precompile": "rm -rf build",
"compile": "node-gyp configure && node-gyp build",
"pretest": "npm run compile",
"test": "node test/index.test.js"
},
"repository": {
Expand Down
58 changes: 53 additions & 5 deletions src/index.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,61 @@
#include <napi.h>
#include <stdio.h>
#include "../lib/inclose5/stdafx.h"
#include "../lib/inclose5/globalsv.h"
#include "../lib/inclose5/inclose.h"
#include "../lib/inclose5/BicsUtils.h"

Napi::String Method(const Napi::CallbackInfo& info) {
static void ArrayConsumer(dataset_t &matrix, const uint8_t* array, size_t length, row_t row_size, col_t col_size ) {

//Allocating memory
matrix = new data_t*[row_size];
for (row_t i = 0; i < row_size; ++i)
matrix[i] = new data_t[col_size];

row_t i = 0;
row_t j = 0;
for (size_t index = 0; index < length; index++) {
if (j == col_size) {
j = 0;
i++;
}
if (i < row_size) {
matrix[i][j] = array[index] == 1 ? true : false;
j++;
}
}
}

static Napi::Value Run(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
return Napi::String::New(env, "world");

if (info.Length() < 3) {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
return env.Null();
}

if (!info[0].IsArrayBuffer() || !info[1].IsNumber() || !info[2].IsNumber()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}


row_t row_size = info[1].As<Napi::Number>().Int32Value();
col_t col_size = info[2].As<Napi::Number>().Int32Value();
Napi::ArrayBuffer buf = info[0].As<Napi::ArrayBuffer>();
dataset_t matrix;
ArrayConsumer(matrix, reinterpret_cast<uint8_t*>(buf.Data()),
buf.ByteLength() / sizeof(uint8_t), row_size, col_size);

double duration = runInClose(matrix, row_size, col_size, 2, 1);
Napi::Number durationN = Napi::Number::New(env, duration);

return durationN;
}

Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "inclose5"),
Napi::Function::New(env, Method));
static Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "run"), Napi::Function::New(env, Run));
return exports;
}

Expand Down
6 changes: 4 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const addon = require('../src/index.js')
const inclose5 = require('../src/index.js')

console.log(addon.inclose5())
const dataset = new Uint8Array([0,0,0,1,1,1,0,0,0,1,1,1,0,1,1,0,0,0,0,1,1,0,0,0])
const duration = inclose5.run(dataset.buffer, 4, 6)
console.log(`Algorithm run time = ${duration} seconds`)

0 comments on commit 1055d47

Please sign in to comment.