copy_ndarray.js (3457B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2021 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // FUNCTIONS // 22 23 /** 24 * Returns an ndarray buffer element. 25 * 26 * @private 27 * @param {Collection} buf - data buffer 28 * @param {NonNegativeInteger} idx - element index 29 * @returns {*} element 30 */ 31 function getIndexed( buf, idx ) { 32 return buf[ idx ]; 33 } 34 35 /** 36 * Returns an ndarray buffer element using an accessor method. 37 * 38 * @private 39 * @param {Collection} buf - data buffer 40 * @param {NonNegativeInteger} idx - element index 41 * @returns {*} element 42 */ 43 function getAccessor( buf, idx ) { 44 return buf.get( idx ); 45 } 46 47 /** 48 * Sets an ndarray buffer element. 49 * 50 * @private 51 * @param {Collection} buf - data buffer 52 * @param {NonNegativeInteger} idx - element index 53 * @param {*} value - value to set 54 */ 55 function setIndexed( buf, idx, value ) { 56 buf[ idx ] = value; 57 } 58 59 /** 60 * Sets an ndarray buffer element using an accessor method. 61 * 62 * @private 63 * @param {Collection} buf - data buffer 64 * @param {NonNegativeInteger} idx - element index 65 * @param {*} value - value to set 66 */ 67 function setAccessor( buf, idx, value ) { 68 buf.set( value, idx ); 69 } 70 71 72 // MAIN // 73 74 /** 75 * Copies ndarray meta-data to an object likely to have the same "hidden" shape. 76 * 77 * ## Notes 78 * 79 * - This function is intended as a potential performance optimization. In V8, for example, even if two objects share common properties, if those properties were added in different orders or if one object has additional properties not shared by the other object, then those objects will have different "hidden" classes. If a function is provided many objects having different "shapes", some JavaScript VMs (e.g., V8) will consider the function "megamorphic" and fail to perform various runtime optimizations. Accordingly, the intent of this function is to standardize the "shape" of the object holding ndarray meta data to ensure that loop iteration functions are provided consistent argument "shapes". 80 * 81 * @private 82 * @param {Object} x - object containing ndarray meta data 83 * @param {string} x.dtype - data type 84 * @param {Collection} x.data - data buffer 85 * @param {NonNegativeIntegerArray} x.shape - dimensions 86 * @param {IntegerArray} x.strides - stride lengths 87 * @param {NonNegativeInteger} x.offset - index offset 88 * @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style) 89 * @returns {Object} object containing ndarray meta data 90 */ 91 function copy( x ) { 92 var xbuf; 93 var bool; 94 95 xbuf = x.data; 96 bool = Boolean( xbuf.get && xbuf.set ); // Note: intentional weak check, as don't explicitly check for functions for (perhaps marginally) better performance. 97 return { 98 'dtype': x.dtype, 99 'data': xbuf, 100 'shape': x.shape, 101 'strides': x.strides, 102 'offset': x.offset, 103 'order': x.order, 104 'accessors': bool, 105 'getter': ( bool ) ? getAccessor : getIndexed, 106 'setter': ( bool ) ? setAccessor : setIndexed 107 }; 108 } 109 110 111 // EXPORTS // 112 113 module.exports = copy;