Skip to content

Commit 07ae02d

Browse files
authored
Merge pull request #1 from plotly/inline-cwise-functions
Inline function constructors of ndarray-pack module
2 parents 11c6fcf + 4a51cd5 commit 07ae02d

File tree

5 files changed

+93
-24
lines changed

5 files changed

+93
-24
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ logs
1212
results
1313

1414
npm-debug.log
15+
package-lock.json
1516
node_modules/*

build.js

-16
This file was deleted.

doConvert.js

+87-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,87 @@
1-
module.exports=require('cwise-compiler')({"args":["array","scalar","index"],"pre":{"body":"{}","args":[],"thisVars":[],"localVars":[]},"body":{"body":"{\nvar _inline_1_v=_inline_1_arg1_,_inline_1_i\nfor(_inline_1_i=0;_inline_1_i<_inline_1_arg2_.length-1;++_inline_1_i) {\n_inline_1_v=_inline_1_v[_inline_1_arg2_[_inline_1_i]]\n}\n_inline_1_arg0_=_inline_1_v[_inline_1_arg2_[_inline_1_arg2_.length-1]]\n}","args":[{"name":"_inline_1_arg0_","lvalue":true,"rvalue":false,"count":1},{"name":"_inline_1_arg1_","lvalue":false,"rvalue":true,"count":1},{"name":"_inline_1_arg2_","lvalue":false,"rvalue":true,"count":4}],"thisVars":[],"localVars":["_inline_1_i","_inline_1_v"]},"post":{"body":"{}","args":[],"thisVars":[],"localVars":[]},"funcName":"convert","blockSize":64})
1+
"use strict"
2+
3+
function CwiseOp() {
4+
return function (SS, a0, t0, p0, Y0) {
5+
var s0 = SS[0],
6+
s1 = SS[1],
7+
s2 = SS[2],
8+
t0p0 = t0[0],
9+
t0p1 = t0[1],
10+
t0p2 = t0[2],
11+
index = [0, 0, 0];
12+
p0 |= 0;
13+
var i0 = 0,
14+
i1 = 0,
15+
i2 = 0,
16+
d0s0 = t0p2,
17+
d0s1 = t0p1 - s2 * t0p2,
18+
d0s2 = t0p0 - s1 * t0p1;
19+
for (i2 = 0; i2 < s0; ++i2) {
20+
for (i1 = 0; i1 < s1; ++i1) {
21+
for (i0 = 0; i0 < s2; ++i0) {
22+
{
23+
var _inline_1_v = Y0,
24+
_inline_1_i;
25+
for (
26+
_inline_1_i = 0;
27+
_inline_1_i < index.length - 1;
28+
++_inline_1_i
29+
) {
30+
_inline_1_v = _inline_1_v[index[_inline_1_i]];
31+
}
32+
a0[p0] = _inline_1_v[index[index.length - 1]];
33+
}
34+
p0 += d0s0;
35+
++index[2];
36+
}
37+
p0 += d0s1;
38+
index[2] -= s2;
39+
++index[1];
40+
}
41+
p0 += d0s2;
42+
index[1] -= s1;
43+
++index[0];
44+
}
45+
};
46+
}
47+
48+
//Generates a cwise operator
49+
function generateCWiseOp() {
50+
return CwiseOp()
51+
}
52+
53+
var compile = generateCWiseOp
54+
55+
function thunk(compile) {
56+
var CACHED = {};
57+
return function convert_cwise_thunk(array0, scalar1) {
58+
var t0 = array0.dtype,
59+
r0 = array0.order,
60+
type = [t0, r0.join()].join(),
61+
proc = CACHED[type];
62+
if (!proc) {
63+
CACHED[type] = proc = compile([t0, r0]);
64+
}
65+
return proc(
66+
array0.shape.slice(0),
67+
array0.data,
68+
array0.stride,
69+
array0.offset | 0,
70+
scalar1
71+
);
72+
};
73+
}
74+
75+
function createThunk(proc) {
76+
return thunk(compile.bind(undefined, proc))
77+
}
78+
79+
function compileCwise(user_args) {
80+
return createThunk({
81+
funcName: user_args.funcName
82+
})
83+
}
84+
85+
module.exports = compileCwise({
86+
funcName: "convert"
87+
});

package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77
"test": "test"
88
},
99
"dependencies": {
10-
"cwise-compiler": "^1.1.2",
1110
"ndarray": "^1.0.13"
1211
},
1312
"devDependencies": {
1413
"tap": "~0.4.2",
1514
"cwise-bake": "0.0.0"
1615
},
1716
"scripts": {
18-
"test": "tap test/*.js",
19-
"build": "node build.js > doConvert.js"
17+
"test": "tap test/*.js"
2018
},
2119
"repository": {
2220
"type": "git",

test/test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
require("tap").test("ndarray-pack", function(t) {
44

5-
var x = [[1, 0, 1],
5+
var x = [[[1, 0, 1],
66
[0, 1, 1],
77
[0, 0, 1],
8-
[1, 0, 0]]
8+
[1, 0, 0]]]
99

1010
var y = require("../convert.js")(x)
1111

1212
for(var i=0; i<4; ++i) {
1313
for(var j=0; j<3; ++j) {
14-
t.equals(y.get(i,j), x[i][j])
14+
t.equals(y.get(0,i,j), x[0][i][j])
1515
}
1616
}
17-
17+
1818
var x = [[[1, 2]], [[3,4]], [[5,6]]]
1919
var y = require("../convert.js")(x)
2020

0 commit comments

Comments
 (0)