-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy_view.js
167 lines (146 loc) · 4.42 KB
/
copy_view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// MODULES //
var arraylike2object = require( '@stdlib/array-base-arraylike2object' );
var castReturn = require( '@stdlib/complex-base-cast-return' );
var complexCtors = require( '@stdlib/complex-ctors' );
var bufferCtors = require( '@stdlib/ndarray-base-buffer-ctors' );
var allocUnsafe = require( '@stdlib/buffer-alloc-unsafe' );
var ndarray = require( '@stdlib/ndarray-base-ctor' );
var getDType = require( '@stdlib/ndarray-dtype' );
var getShape = require( '@stdlib/ndarray-shape' );
var getStrides = require( '@stdlib/ndarray-strides' );
var getOffset = require( '@stdlib/ndarray-offset' );
var getOrder = require( '@stdlib/ndarray-order' );
var getData = require( '@stdlib/ndarray-data-buffer' );
// FUNCTIONS //
/**
* Copies a "generic" ndarray view.
*
* @private
* @param {ndarray} arr - input ndarray
* @returns {Array} output data buffer
*/
function generic( arr ) {
var len;
var out;
var i;
len = arr.length;
out = [];
for ( i = 0; i < len; i++ ) {
out.push( arr.iget( i ) ); // as output buffer is generic, should work with both real- and complex-valued ndarrays
}
return out;
}
/**
* Copies a "binary" ndarray view.
*
* @private
* @param {ndarray} arr - input ndarray
* @returns {Array} output data buffer
*/
function binary( arr ) {
var len;
var out;
var i;
len = arr.length;
out = allocUnsafe( len );
for ( i = 0; i < len; i++ ) {
out[ i ] = arr.iget( i ); // we're assuming that we're doing something sensible here (e.g., not trying to cast a complex-valued ndarray to a "binary" ndarray or a double-precision floating-point ndarray to binary, etc)
}
return out;
}
/**
* Copies a "typed" ndarray view.
*
* @private
* @param {ndarray} arr - input ndarray
* @param {string} dtype - data type
* @returns {Array} output data buffer
*/
function typed( arr, dtype ) {
var ctor;
var len;
var out;
var set;
var fcn;
var o;
var i;
ctor = bufferCtors( dtype );
len = arr.length;
out = new ctor( len );
// If the output data buffer is a complex number array, we need to use accessors...
o = arraylike2object( out );
if ( o.accessorProtocol ) {
set = o.accessors[ 1 ];
fcn = castReturn( wrapper, 1, complexCtors( dtype ) );
for ( i = 0; i < len; i++ ) {
set( out, i, fcn( i ) ); // we're assuming that we're doing something sensible here (e.g., not trying to cast arbitrary objects to complex numbers, etc)
}
} else {
for ( i = 0; i < len; i++ ) {
out[ i ] = arr.iget( i ); // we're assuming that we're doing something sensible here (e.g., not trying to cast an ndarray containing generic objects to a double-precision floating-point array or a complex-valued ndarray to a real-valued ndarray, etc)
}
}
return out;
/**
* Returns the ndarray element specified by a provided linear index.
*
* @private
* @param {NonNegativeInteger} i - linear index
* @returns {*} value
*/
function wrapper( i ) {
return arr.iget( i );
}
}
// MAIN //
/**
* Copies an ndarray view to a data buffer.
*
* @private
* @param {ndarray} arr - input ndarray
* @param {string} dtype - data type
* @returns {(Array|TypedArray|Buffer)} output data buffer
*
* @example
* var ndarray = require( '@stdlib/ndarray-ctor' );
*
* var buffer = [ 1.0, 2.0, 3.0 ];
* var shape = [ 3 ];
* var strides = [ -1 ];
* var vec = ndarray( 'generic', buffer, shape, strides, 2, 'row-major' );
*
* var b = copyView( vec, 'float64' );
* // returns <Float64Array>[ 3.0, 2.0, 1.0 ]
*/
function copyView( arr, dtype ) {
var x;
// Create a new "base" view, thus ensuring we have an `.iget` method and associated meta data...
x = new ndarray( getDType( arr ), getData( arr ), getShape( arr ), getStrides( arr ), getOffset( arr ), getOrder( arr ) ); // eslint-disable-line max-len
if ( dtype === 'generic' ) {
return generic( x );
}
if ( dtype === 'binary' ) {
return binary( x );
}
return typed( x, dtype );
}
// EXPORTS //
module.exports = copyView;