main.js (1841B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 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 // MODULES // 22 23 var minmax = require( './../../../../base/minmax-view-buffer-index' ); 24 25 26 // MAIN // 27 28 /** 29 * Returns a boolean indicating if a buffer length is compatible with provided ndarray meta data. 30 * 31 * @param {NonNegativeInteger} len - buffer length 32 * @param {NonNegativeIntegerArray} shape - array shape 33 * @param {IntegerArray} strides - stride array 34 * @param {NonNegativeInteger} offset - index offset 35 * @returns {boolean} boolean indicating if a buffer length is compatible 36 * 37 * @example 38 * var shape = [ 2, 2 ]; 39 * var strides = [ 2, 1 ]; 40 * var offset = 0; 41 * 42 * var bool = isBufferLengthCompatible( 4, shape, strides, offset ); 43 * // returns true 44 * 45 * @example 46 * var shape = [ 2, 2 ]; 47 * var strides = [ 2, 1 ]; 48 * var offset = 2; 49 * 50 * var bool = isBufferLengthCompatible( 4, shape, strides, offset ); 51 * // returns false 52 */ 53 function isBufferLengthCompatible( len, shape, strides, offset ) { 54 // Determine the minimum and maximum linear indices which are accessible by the array view: 55 var buf = minmax( shape, strides, offset ); 56 57 // If the indices are "inbounds", then the buffer length is compatible: 58 return ( buf[ 0 ] >= 0 && buf[ 1 ] < len ); 59 } 60 61 62 // EXPORTS // 63 64 module.exports = isBufferLengthCompatible;