1
1
var iota = require ( "iota-array" )
2
2
var isBuffer = require ( "is-buffer" )
3
3
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
-
20
4
var hasTypedArrays = ( ( typeof Float64Array ) !== "undefined" )
21
5
22
6
function compare1st ( a , b ) {
@@ -103,62 +87,26 @@ return function construct_"+className+"(a,b,c,d){return new "+className+"(a,d)}"
103
87
var indices = iota ( dimension )
104
88
var args = indices . map ( function ( i ) { return "i" + i } )
105
89
var index_str = "this.offset+" + indices . map ( function ( i ) {
106
- return "this._stride " + i + "*i" + i
90
+ return "this.stride[ " + i + "] *i" + i
107
91
} ) . join ( "+" )
108
- code . push ( "function " + className + "(a," +
109
- indices . map ( function ( i ) {
92
+ var shapeArg = indices . map ( function ( i ) {
110
93
return "b" + i
111
- } ) . join ( "," ) + "," +
112
- indices . map ( function ( i ) {
94
+ } ) . join ( "," )
95
+ var strideArg = indices . map ( function ( i ) {
113
96
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}" ,
122
103
"var proto=" + className + ".prototype" ,
123
104
"proto.dtype='" + dtype + "'" ,
124
105
"proto.dimension=" + dimension )
125
106
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
-
159
107
//view.size:
160
108
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 ( "*" ) ,
162
110
"}})" )
163
111
164
112
//view.order:
@@ -169,10 +117,10 @@ return "+indices.map(function(i) { return "this._shape"+i }).join("*"),
169
117
if ( dimension < 4 ) {
170
118
code . push ( "function " + className + "_order(){" )
171
119
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]}})" )
173
121
} else if ( dimension === 3 ) {
174
122
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] );\
176
124
if(s0>s1){\
177
125
if(s1>s2){\
178
126
return [2,1,0];\
@@ -218,15 +166,15 @@ return [0,2,1];\
218
166
//view.hi():
219
167
code . push ( "proto.hi=function " + className + "_hi(" + args . join ( "," ) + "){return new " + className + "(this.data," +
220
168
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 ( "" )
222
170
} ) . join ( "," ) + "," +
223
171
indices . map ( function ( i ) {
224
- return "this._stride " + i
172
+ return "this.stride[ " + i + "]"
225
173
} ) . join ( "," ) + ",this.offset)}" )
226
174
227
175
//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 + "]" } )
230
178
code . push ( "proto.lo=function " + className + "_lo(" + args . join ( "," ) + "){var b=this.offset,d=0," + a_vars . join ( "," ) + "," + c_vars . join ( "," ) )
231
179
for ( var i = 0 ; i < dimension ; ++ i ) {
232
180
code . push (
@@ -246,10 +194,10 @@ a"+i+"-=d}")
246
194
//view.step():
247
195
code . push ( "proto.step=function " + className + "_step(" + args . join ( "," ) + "){var " +
248
196
indices . map ( function ( i ) {
249
- return "a" + i + "=this._shape " + i
197
+ return "a" + i + "=this.shape[ " + i + "]"
250
198
} ) . join ( "," ) + "," +
251
199
indices . map ( function ( i ) {
252
- return "b" + i + "=this._stride " + i
200
+ return "b" + i + "=this.stride[ " + i + "]"
253
201
} ) . join ( "," ) + ",c=this.offset,d=0,ceil=Math.ceil" )
254
202
for ( var i = 0 ; i < dimension ; ++ i ) {
255
203
code . push (
@@ -286,7 +234,7 @@ b"+i+"*=d\
286
234
//view.pick():
287
235
code . push ( "proto.pick=function " + className + "_pick(" + args + "){var a=[],b=[],c=this.offset" )
288
236
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 + "] )}" )
290
238
}
291
239
code . push ( "var ctor=CTOR_LIST[a.length+1];return ctor(this.data,a,b,c)}" )
292
240
0 commit comments