Skip to content

Commit e8640d3

Browse files
committed
adding in support for offset indices
1 parent f88407a commit e8640d3

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ To use the library, you pass it an object with the following fields:
5353
+ `"scalar"`: A globally broadcasted scalar argument
5454
+ `"index"`: (Hidden) An array representing the current index of the element being processed. Initially [0,0,...] in the pre block and set to some undefined value in the post block.
5555
+ `"shape"`: (Hidden) An array representing the shape of the arrays being processed
56+
+ (Hidden) An object containing two properties reprsenting an offset pointer from an array argument.
57+
+ `offset` An array representing the relative offset of the object
58+
+ `array` The index of an array parameter
5659
* `pre`: A function to be executed before starting the loop
5760
* `body`: (Required) A function that gets applied to each element of the input arrays
5861
* `post`: Executed when loop completes

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"dependencies": {
1010
"cwise-parser": "~0.0.1",
11-
"cwise-compiler": "~0.0.0"
11+
"cwise-compiler": "~0.1.0"
1212
},
1313
"devDependencies": {
1414
"ndarray": "~1.0.1",

test/offset.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
var cwise = require("../cwise.js")
2+
, ndarray = require("ndarray")
3+
, test = require("tap").test
4+
5+
function DumbStorage(n) {
6+
this.data = new Int32Array(n)
7+
this.length = n
8+
}
9+
DumbStorage.prototype.get = function(i) { return this.data[i] }
10+
DumbStorage.prototype.set = function(i, v) { return this.data[i]=v }
11+
12+
13+
test("offset", function(t) {
14+
15+
var binary = cwise({
16+
args: ["array", "array", {offset:[1], array:1}, "scalar", "shape", "index"],
17+
body: function(a,b,c,t,s,idx) {
18+
t.equals(a, 0, "idx:"+idx+", shape:"+s)
19+
a = c + b + 1000
20+
}
21+
})
22+
23+
function testBinary1D(P, Q) {
24+
console.log(P.shape.toString(), Q.shape.toString())
25+
for(var i=0; i<P.shape[0]; ++i) {
26+
Q.set(i, i)
27+
P.set(i, 0)
28+
}
29+
Q.set(P.shape[0], P.shape[0])
30+
binary(P, Q, t)
31+
for(var i=0; i<P.shape[0]; ++i) {
32+
t.equals(P.get(i), 2*i+1001)
33+
}
34+
}
35+
36+
var A = ndarray(new Int32Array(128))
37+
var B = ndarray(new Int32Array(129))
38+
39+
testBinary1D(ndarray(new Int32Array(0)), ndarray(new Int32Array(1)))
40+
testBinary1D(ndarray(new Int32Array(1)), ndarray(new Int32Array(2)))
41+
testBinary1D(A, B)
42+
testBinary1D(A.lo(32), B)
43+
testBinary1D(A.step(-1), B)
44+
testBinary1D(A, B.step(-1))
45+
46+
A = ndarray(new DumbStorage(128))
47+
B = ndarray(new DumbStorage(129))
48+
testBinary1D(ndarray(new DumbStorage(0)), ndarray(new DumbStorage(1)))
49+
testBinary1D(ndarray(new DumbStorage(1)), ndarray(new DumbStorage(2)))
50+
testBinary1D(A, B)
51+
testBinary1D(A.lo(32), B)
52+
testBinary1D(A.step(-1), B)
53+
testBinary1D(A, B.step(-1))
54+
55+
t.end()
56+
})

0 commit comments

Comments
 (0)