Skip to content

Commit 344fd1d

Browse files
committed
gfor.js invented
1 parent a4808fe commit 344fd1d

File tree

6 files changed

+103
-10
lines changed

6 files changed

+103
-10
lines changed

lib/es6/complex.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131

3232
"use strict";
3333

34-
let lodash = require("lodash");
34+
let _ = require("lodash");
3535
let assert = require("better-assert");
3636

3737
function Complex(real, imag) {

lib/es6/dim4.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131

3232
"use strict";
3333

34-
let lodash = require("lodash");
34+
let _ = require("lodash");
3535
let assert = require("better-assert");
3636

3737
function Dim4(dim0, dim1, dim2, dim3) {

lib/es6/ext.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ function synchronify(fire, f) {
3838
return function () {
3939
var err;
4040
var res;
41+
var done = false;
4142
let cb = function (e, r) {
4243
err = e;
4344
res = r;
4445
done = true;
4546
};
4647

47-
let done = false;
4848
let args = _.toArray(arguments).concat(cb);
4949

5050
f.apply(this, args);
@@ -54,7 +54,7 @@ function synchronify(fire, f) {
5454
if (err) throw err;
5555

5656
return res;
57-
}
57+
};
5858
}
5959

6060
function installAsyncAndSync(fire, obj, name) {
@@ -121,7 +121,8 @@ function Ext(self) {
121121
finally {
122122
this.setDevice(current);
123123
}
124-
}
124+
},
125+
gfor: require("./makeGfor")(self)
125126
});
126127
}
127128

lib/es6/makeGfor.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Copyright (c) 2014-2015, ArrayFire
3+
Copyright (c) 2015 Gábor Mező aka unbornchikken ([email protected])
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without modification,
7+
are permitted provided that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
* Redistributions in binary form must reproduce the above copyright notice, this
13+
list of conditions and the following disclaimer in the documentation and/or
14+
other materials provided with the distribution.
15+
16+
* Neither the name of the ArrayFire nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
24+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
"use strict";
33+
34+
let _ = require("lodash");
35+
let assert = require("better-assert");
36+
let Seq = require("./seq");
37+
38+
function makeGfor(af) {
39+
return function gfor() {
40+
let fPos = -1;
41+
for(let i = 0; i < arguments.length; i++) {
42+
if (_.isFunction(arguments[i])) {
43+
fPos = i;
44+
break;
45+
}
46+
}
47+
if (fPos === -1) {
48+
throw new Error("Body function argument expected.");
49+
}
50+
if (fPos === 0) {
51+
throw new Error("Seq arguments expected.");
52+
}
53+
let thisArg = arguments[fPos];
54+
if (arguments.length > fPos + 1 && _.isObject(fPos + 1)) {
55+
thisArg = arguments[fPos + 1];
56+
}
57+
let seq;
58+
if (fPos === 1) {
59+
seq = new Seq(arguments[0]);
60+
}
61+
else if (fPos === 2) {
62+
seq = new Seq(arguments[0], arguments[1]);
63+
}
64+
else {
65+
throw new Error("Invalid number of Seq arguments.");
66+
}
67+
seq.isGFor = true;
68+
af._gforToggle();
69+
try {
70+
arguments[fPos].call(thisArg, seq);
71+
}
72+
finally {
73+
af._gforToggle();
74+
}
75+
}
76+
}
77+
78+
module.exports = makeGfor;

lib/es6/seq.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,26 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131

3232
"use strict";
3333

34-
let lodash = require("lodash");
34+
let _ = require("lodash");
3535
let assert = require("better-assert");
3636

37-
function Seq(begin, end, step, isGFor) {
37+
function Seq(begin, end, step) {
3838
assert(_.isNumber(begin));
39-
assert(_.isNumber(end));
39+
if (_.isUndefined(end)) {
40+
begin = 0;
41+
end = begin;
42+
}
43+
else {
44+
assert(_.isNumber(end));
45+
}
4046
step = step || 1;
4147
assert(_.isNumber(step));
42-
isGFor = !!isGFor;
4348

4449
this.begin = begin;
4550
this.end = end;
4651
this.step = step;
47-
this.isGFor = isGFor;
52+
53+
this.isGFor = false;
4854
}
4955

5056
module.exports = Seq;

src/fire.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ NAN_METHOD(_DoEvents)
5555
NanReturnUndefined();
5656
}
5757

58+
NAN_METHOD(_GforToggle)
59+
{
60+
NanScope();
61+
af::gforToggle();
62+
NanReturnUndefined();
63+
}
64+
5865
void Init(v8::Handle<v8::Object> exports)
5966
{
6067
Symbols::Init();
@@ -73,4 +80,5 @@ void Init(v8::Handle<v8::Object> exports)
7380

7481
// Helpers:
7582
exports->Set(NanNew("_doEvents"), NanNew<FunctionTemplate>(_DoEvents)->GetFunction());
83+
exports->Set(NanNew("_gforToggle"), NanNew<FunctionTemplate>(_GforToggle)->GetFunction());
7684
}

0 commit comments

Comments
 (0)