Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 1978x 1978x 1978x 1978x 1978x 1978x 1978x 1978x 986x 427x 427x 559x 559x 1978x 20x 20x 1978x 18x 1978x 4x 4x 1970x 108x 26x 26x 108x 924x 1978x 4x 4x 1978x 60x 60x 1306x 810x 10x 10x 1978x 10x 50x 3x 3x 1978x 22x 22x 1978x 34x 34x 1978x 11x 11x 770x 770x 770x 1515x 457x 457x 19x 19x 457x 751x 991x 658x 658x 751x 751x 751x 751x 1978x 1978x 1978x 1978x 1978x 1978x 1978x 1978x 1978x 1978x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x | /** * @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. */ /* eslint-disable no-invalid-this */ 'use strict'; // MODULES // var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); var isCollection = require( '@stdlib/assert/is-collection' ); var isNonNegativeIntegerArray = require( '@stdlib/assert/is-nonnegative-integer-array' ).primitives; var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; var isIntegerArray = require( '@stdlib/assert/is-integer-array' ).primitives; var isFunction = require( '@stdlib/assert/is-function' ); var isOrder = require( '@stdlib/ndarray/base/assert/is-order' ); var isDataType = require( '@stdlib/ndarray/base/assert/is-data-type' ); var isBufferLengthCompatible = require( '@stdlib/ndarray/base/assert/is-buffer-length-compatible' ); var numel = require( '@stdlib/ndarray/base/numel' ); var parent = require( '@stdlib/ndarray/base/ctor' ); // eslint-disable-line stdlib/no-redeclare var defaults = require( '@stdlib/ndarray/defaults' ); var inherit = require( '@stdlib/utils/inherit' ); var format = require( '@stdlib/string/format' ); var iget = require( './iget.js' ); var iset = require( './iset.js' ); var get = require( './get.js' ); var set = require( './set.js' ); var copy = require( './copy_array.js' ); var validate = require( './validate.js' ); // VARIABLES // /* * See the following references: * * - https://stackoverflow.com/questions/22747068/is-there-a-max-number-of-arguments-javascript-functions-can-accept * - https://bugs.webkit.org/show_bug.cgi?id=80797 * - https://github.com/numpy/numpy/issues/5744 * * Note that the maximum number of function arguments can vary from engine to engine. Here, we choose something of a lowest common denominator which may **not** be valid everywhere. */ var MAX_DIMS = 32767|0; var INDEX_MODE = defaults.get( 'index_mode' ); var READONLY = false; // MAIN // /** * ndarray constructor. * * @constructor * @param {string} dtype - data type * @param {Collection} buffer - data buffer * @param {NonNegativeIntegerArray} shape - array shape * @param {IntegerArray} strides - array strides * @param {NonNegativeInteger} offset - index offset * @param {string} order - specifies whether an array is row-major (C-style) or column-major (Fortran-style) * @param {Options} [options] - function options * @param {string} [options.mode="throw"] - specifies how to handle indices which exceed array dimensions * @param {StringArray} [options.submode=["throw"]] - specifies how to handle subscripts which exceed array dimensions on a per dimension basis * @param {boolean} [options.readonly=false] - boolean indicating whether an array should be read-only * @throws {TypeError} `dtype` argument must be a supported ndarray data type * @throws {TypeError} `buffer` argument must be an array-like object, typed-array-like, or a Buffer * @throws {TypeError} `buffer` argument `get` and `set` properties must be functions * @throws {TypeError} `shape` argument must be an array-like object containing nonnegative integers * @throws {Error} `shape` argument length must equal the number of dimensions * @throws {TypeError} `strides` argument must be an array-like object containing integers * @throws {Error} `strides` argument length must equal the number of dimensions (except for zero-dimensional arrays; in which case, the `strides` argument length must be equal to `1`) * @throws {Error} for zero-dimensional ndarrays, the `strides` argument must contain a single element equal to `0` * @throws {TypeError} `offset` argument must be a nonnegative integer * @throws {TypeError} `order` argument must be a supported ndarray order * @throws {Error} `buffer` argument must be compatible with specified meta data * @throws {TypeError} options argument must be an object * @throws {TypeError} must provide valid options * @throws {RangeError} too many dimensions * @returns {ndarray} ndarray instance * * @example * var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; * var shape = [ 3, 2 ]; * var strides = [ 2, 1 ]; * var offset = 0; * * var out = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); */ function ndarray( dtype, buffer, shape, strides, offset, order, options ) { var ndims; var opts; var err; var sh; var st; if ( !(this instanceof ndarray) ) { if ( arguments.length < 7 ) { return new ndarray( dtype, buffer, shape, strides, offset, order ); } return new ndarray( dtype, buffer, shape, strides, offset, order, options ); // eslint-disable-line max-len } if ( !isDataType( dtype ) ) { throw new TypeError( format( 'invalid argument. First argument must be a supported ndarray data type. Value: `%s`.', dtype ) ); } if ( !isCollection( buffer ) ) { throw new TypeError( format( 'invalid argument. Second argument must be an array-like object, typed-array-like, or a Buffer. Value: `%s`.', buffer ) ); } else if ( buffer.get && buffer.set && ( !isFunction( buffer.get ) || !isFunction( buffer.set ) ) ) { // eslint-disable-line max-len throw new TypeError( format( 'invalid argument. Second argument `get` and `set` properties must be functions. Value: `%s`.', buffer ) ); } if ( !isNonNegativeIntegerArray( shape ) ) { if ( !isCollection( shape) || shape.length > 0 ) { throw new TypeError( format( 'invalid argument. Third argument must be an array-like object containing nonnegative integers. Value: `%s`.', shape ) ); } } ndims = shape.length; if ( ndims > MAX_DIMS ) { throw new RangeError( format( 'invalid argument. Number of dimensions must not exceed %u due to stack limits. Value: `%u`.', MAX_DIMS, ndims ) ); } if ( !isIntegerArray( strides ) ) { throw new TypeError( format( 'invalid argument. Fourth argument must be an array-like object containing integers. Value: `%s`.', strides ) ); } if ( ndims > 0 ) { if ( strides.length !== ndims ) { throw new RangeError( format( 'invalid argument. Fourth argument length must match the number of dimensions. Expected number of dimensions: `%u`. Strides length: `%u`.', ndims, strides.length ) ); } } else if ( strides.length !== 1 ) { throw new RangeError( 'invalid argument. Fourth argument length must be equal to 1 when creating a zero-dimensional ndarray.' ); } else if ( strides[ 0 ] !== 0 ) { throw new RangeError( format( 'invalid argument. Fourth argument must contain a single element equal to 0. Value: `%d`.', strides[ 0 ] ) ); } if ( !isNonNegativeInteger( offset ) ) { throw new TypeError( format( 'invalid argument. Fifth argument must be a nonnegative integer. Value: `%s`.', offset ) ); } if ( !isOrder( order ) ) { throw new TypeError( format( 'invalid argument. Sixth argument must be a supported order. Value: `%s`.', order ) ); } if ( ndims > 0 && !isBufferLengthCompatible( buffer.length, shape, strides, offset ) && numel( shape ) > 0 ) { // eslint-disable-line max-len throw new Error( 'invalid arguments. Input buffer is incompatible with the specified meta data. Ensure that the offset is valid with regard to the strides array and that the buffer has enough elements to satisfy the desired array shape.' ); } opts = {}; opts.mode = INDEX_MODE; opts.readonly = READONLY; if ( arguments.length > 6 ) { err = validate( opts, options ); if ( err ) { throw err; } } this._mode = opts.mode; if ( opts.submode === void 0 ) { opts.submode = [ this._mode ]; } this._submode = opts.submode; // Copy `shape` and `strides` to prevent external mutation: sh = copy( shape, ndims ); st = copy( strides, ndims || 1 ); // Call the parent constructor: parent.call( this, dtype, buffer, sh, st, offset, order ); this._flags.READONLY = opts.readonly; return this; /* eslint-enable no-invalid-this */ } // Inherit from the parent constructor: inherit( ndarray, parent ); /** * Constructor name. * * @name name * @memberof ndarray * @type {string} * @default 'ndarray' * * @example * var str = ndarray.name; * // returns 'ndarray' */ setReadOnly( ndarray, 'name', 'ndarray' ); /** * Returns an array element. * * ## Notes * * - The number of indices should **equal** the number of dimensions. Accordingly, for zero-dimensional arrays, no indices should be provided. * * @name get * @memberof ndarray.prototype * @type {Function} * @param {...integer} [idx] - indices * @returns {*} array element * * @example * var buffer = [ 1, 2, 3, 4, 5, 6 ]; * var shape = [ 3, 2 ]; * var strides = [ 2, 1 ]; * var offset = 0; * * var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); * * var v = x.get( 1, 1 ); * // returns 4 */ setReadOnly( ndarray.prototype, 'get', get ); /** * Returns an array element located at a specified linear index. * * ## Notes * * - For zero-dimensional arrays, the input argument is ignored and, for clarity, should not be provided. * * @name iget * @memberof ndarray.prototype * @type {Function} * @param {integer} [idx] - linear index * @returns {*} array element * * @example * var buffer = [ 1, 2, 3, 4, 5, 6 ]; * var shape = [ 3, 2 ]; * var strides = [ 2, 1 ]; * var offset = 0; * * var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); * * var v = x.iget( 3 ); * // returns 4 */ setReadOnly( ndarray.prototype, 'iget', iget ); /** * Sets an array element. * * ## Notes * * - The number of indices should **equal** the number of dimensions. Accordingly, for zero-dimensional arrays, no indices should be provided. * * @name set * @memberof ndarray.prototype * @type {Function} * @param {...integer} [idx] - indices * @param {*} v - value to set * @returns {ndarray} ndarray instance * * @example * var buffer = [ 1, 2, 3, 4, 5, 6 ]; * var shape = [ 3, 2 ]; * var strides = [ 2, 1 ]; * var offset = 0; * * var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); * * var v = x.get( 1, 1 ); * // returns 4 * * x.set( 1, 1, 10 ); * * var b = x.data; * // returns [ 1, 2, 3, 10, 5, 6 ] * * v = x.get( 1, 1 ); * // returns 10 */ setReadOnly( ndarray.prototype, 'set', set ); /** * Sets an array element located at a specified linear index. * * ## Notes * * - For zero-dimensional arrays, the first, and only, argument should be the value to set. * * @name iset * @memberof ndarray.prototype * @type {Function} * @param {integer} [idx] - linear index * @param {*} v - value to set * @returns {ndarray} ndarray instance * * @example * var buffer = [ 1, 2, 3, 4, 5, 6 ]; * var shape = [ 3, 2 ]; * var strides = [ 2, 1 ]; * var offset = 0; * * var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); * * var v = x.iget( 3 ); * // returns 4 * * x.iset( 3, 10 ); * * var b = x.data; * // returns [ 1, 2, 3, 10, 5, 6 ] * * v = x.iget( 3 ); * // returns 10 */ setReadOnly( ndarray.prototype, 'iset', iset ); // EXPORTS // module.exports = ndarray; |