Skip to content

Commit 007d21a

Browse files
committed
merge old 1.0.16 branch in
1 parent 2530a1c commit 007d21a

File tree

1 file changed

+20
-72
lines changed

1 file changed

+20
-72
lines changed

ndarray.js

+20-72
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,6 @@
11
var iota = require("iota-array")
22
var isBuffer = require("is-buffer")
33

4-
var arrayMethods = [
5-
"concat",
6-
"join",
7-
"slice",
8-
"toString",
9-
"indexOf",
10-
"lastIndexOf",
11-
"forEach",
12-
"every",
13-
"some",
14-
"filter",
15-
"map",
16-
"reduce",
17-
"reduceRight"
18-
]
19-
204
var hasTypedArrays = ((typeof Float64Array) !== "undefined")
215

226
function compare1st(a, b) {
@@ -103,62 +87,26 @@ return function construct_"+className+"(a,b,c,d){return new "+className+"(a,d)}"
10387
var indices = iota(dimension)
10488
var args = indices.map(function(i) { return "i"+i })
10589
var index_str = "this.offset+" + indices.map(function(i) {
106-
return "this._stride" + i + "*i" + i
90+
return "this.stride[" + i + "]*i" + i
10791
}).join("+")
108-
code.push("function "+className+"(a,"+
109-
indices.map(function(i) {
92+
var shapeArg = indices.map(function(i) {
11093
return "b"+i
111-
}).join(",") + "," +
112-
indices.map(function(i) {
94+
}).join(",")
95+
var strideArg = indices.map(function(i) {
11396
return "c"+i
114-
}).join(",") + ",d){this.data=a")
115-
for(var i=0; i<dimension; ++i) {
116-
code.push("this._shape"+i+"=b"+i+"|0")
117-
}
118-
for(var i=0; i<dimension; ++i) {
119-
code.push("this._stride"+i+"=c"+i+"|0")
120-
}
121-
code.push("this.offset=d|0}",
97+
}).join(",")
98+
code.push(
99+
"function "+className+"(a," + shapeArg + "," + strideArg + ",d){this.data=a",
100+
"this.shape=[" + shapeArg + "]",
101+
"this.stride=[" + strideArg + "]",
102+
"this.offset=d|0}",
122103
"var proto="+className+".prototype",
123104
"proto.dtype='"+dtype+"'",
124105
"proto.dimension="+dimension)
125106

126-
//view.stride and view.shape
127-
var strideClassName = "VStride" + dimension + "d" + dtype
128-
var shapeClassName = "VShape" + dimension + "d" + dtype
129-
var props = {"stride":strideClassName, "shape":shapeClassName}
130-
for(var prop in props) {
131-
var arrayName = props[prop]
132-
code.push(
133-
"function " + arrayName + "(v) {this._v=v} var aproto=" + arrayName + ".prototype",
134-
"aproto.length="+dimension)
135-
136-
var array_elements = []
137-
for(var i=0; i<dimension; ++i) {
138-
array_elements.push(["this._v._", prop, i].join(""))
139-
}
140-
code.push(
141-
"aproto.toJSON=function " + arrayName + "_toJSON(){return [" + array_elements.join(",") + "]}",
142-
"aproto.valueOf=aproto.toString=function " + arrayName + "_toString(){return [" + array_elements.join(",") + "].join()}")
143-
144-
for(var i=0; i<dimension; ++i) {
145-
code.push("Object.defineProperty(aproto,"+i+",{get:function(){return this._v._"+prop+i+"},set:function(v){return this._v._"+prop+i+"=v|0},enumerable:true})")
146-
}
147-
for(var i=0; i<arrayMethods.length; ++i) {
148-
if(arrayMethods[i] in Array.prototype) {
149-
code.push("aproto."+arrayMethods[i]+"=Array.prototype."+arrayMethods[i])
150-
}
151-
}
152-
code.push(["Object.defineProperty(proto,'",prop,"',{get:function ", arrayName, "_get(){return new ", arrayName, "(this)},set: function ", arrayName, "_set(v){"].join(""))
153-
for(var i=0; i<dimension; ++i) {
154-
code.push("this._"+prop+i+"=v["+i+"]|0")
155-
}
156-
code.push("return v}})")
157-
}
158-
159107
//view.size:
160108
code.push("Object.defineProperty(proto,'size',{get:function "+className+"_size(){\
161-
return "+indices.map(function(i) { return "this._shape"+i }).join("*"),
109+
return "+indices.map(function(i) { return "this.shape["+i+"]" }).join("*"),
162110
"}})")
163111

164112
//view.order:
@@ -169,10 +117,10 @@ return "+indices.map(function(i) { return "this._shape"+i }).join("*"),
169117
if(dimension < 4) {
170118
code.push("function "+className+"_order(){")
171119
if(dimension === 2) {
172-
code.push("return (Math.abs(this._stride0)>Math.abs(this._stride1))?[1,0]:[0,1]}})")
120+
code.push("return (Math.abs(this.stride[0])>Math.abs(this.stride[1]))?[1,0]:[0,1]}})")
173121
} else if(dimension === 3) {
174122
code.push(
175-
"var s0=Math.abs(this._stride0),s1=Math.abs(this._stride1),s2=Math.abs(this._stride2);\
123+
"var s0=Math.abs(this.stride[0]),s1=Math.abs(this.stride[1]),s2=Math.abs(this.stride[2]);\
176124
if(s0>s1){\
177125
if(s1>s2){\
178126
return [2,1,0];\
@@ -218,15 +166,15 @@ return [0,2,1];\
218166
//view.hi():
219167
code.push("proto.hi=function "+className+"_hi("+args.join(",")+"){return new "+className+"(this.data,"+
220168
indices.map(function(i) {
221-
return ["(typeof i",i,"!=='number'||i",i,"<0)?this._shape", i, ":i", i,"|0"].join("")
169+
return ["(typeof i",i,"!=='number'||i",i,"<0)?this.shape[", i, "]:i", i,"|0"].join("")
222170
}).join(",")+","+
223171
indices.map(function(i) {
224-
return "this._stride"+i
172+
return "this.stride["+i + "]"
225173
}).join(",")+",this.offset)}")
226174

227175
//view.lo():
228-
var a_vars = indices.map(function(i) { return "a"+i+"=this._shape"+i })
229-
var c_vars = indices.map(function(i) { return "c"+i+"=this._stride"+i })
176+
var a_vars = indices.map(function(i) { return "a"+i+"=this.shape["+i+"]" })
177+
var c_vars = indices.map(function(i) { return "c"+i+"=this.stride["+i+"]" })
230178
code.push("proto.lo=function "+className+"_lo("+args.join(",")+"){var b=this.offset,d=0,"+a_vars.join(",")+","+c_vars.join(","))
231179
for(var i=0; i<dimension; ++i) {
232180
code.push(
@@ -246,10 +194,10 @@ a"+i+"-=d}")
246194
//view.step():
247195
code.push("proto.step=function "+className+"_step("+args.join(",")+"){var "+
248196
indices.map(function(i) {
249-
return "a"+i+"=this._shape"+i
197+
return "a"+i+"=this.shape["+i+"]"
250198
}).join(",")+","+
251199
indices.map(function(i) {
252-
return "b"+i+"=this._stride"+i
200+
return "b"+i+"=this.stride["+i+"]"
253201
}).join(",")+",c=this.offset,d=0,ceil=Math.ceil")
254202
for(var i=0; i<dimension; ++i) {
255203
code.push(
@@ -286,7 +234,7 @@ b"+i+"*=d\
286234
//view.pick():
287235
code.push("proto.pick=function "+className+"_pick("+args+"){var a=[],b=[],c=this.offset")
288236
for(var i=0; i<dimension; ++i) {
289-
code.push("if(typeof i"+i+"==='number'&&i"+i+">=0){c=(c+this._stride"+i+"*i"+i+")|0}else{a.push(this._shape"+i+");b.push(this._stride"+i+")}")
237+
code.push("if(typeof i"+i+"==='number'&&i"+i+">=0){c=(c+this.stride["+i+"]*i"+i+")|0}else{a.push(this.shape["+i+"]);b.push(this.stride["+i+"])}")
290238
}
291239
code.push("var ctor=CTOR_LIST[a.length+1];return ctor(this.data,a,b,c)}")
292240

0 commit comments

Comments
 (0)