time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

main.js (2202B)


      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 bufferCtors = require( './../../../base/buffer-ctors' );
     24 var allocUnsafe = require( '@stdlib/buffer/alloc-unsafe' );
     25 var zeros = require( './zeros.js' );
     26 
     27 
     28 // FUNCTIONS //
     29 
     30 /**
     31 * Returns a zero-filled generic array.
     32 *
     33 * @private
     34 * @param {NonNegativeInteger} size - buffer size
     35 * @returns {Array} zero-filled generic array
     36 */
     37 function generic( size ) {
     38 	var buf;
     39 	var i;
     40 
     41 	buf = [];
     42 	for ( i = 0; i < size; i++ ) {
     43 		buf.push( 0 );
     44 	}
     45 	return buf;
     46 }
     47 
     48 /**
     49 * Returns a zero-filled binary buffer.
     50 *
     51 * @private
     52 * @param {NonNegativeInteger} size - buffer size
     53 * @returns {Buffer} zero-filled binary buffer
     54 */
     55 function binary( size ) {
     56 	return zeros( allocUnsafe( size ) );
     57 }
     58 
     59 /**
     60 * Returns a zero-filled typed array.
     61 *
     62 * @private
     63 * @param {string} dtype - data type
     64 * @param {NonNegativeInteger} size - buffer size
     65 * @returns {(TypedArray|null)} zero-filled typed array
     66 */
     67 function typedarray( dtype, size ) {
     68 	var ctor = bufferCtors( dtype );
     69 	if ( ctor ) {
     70 		return new ctor( size );
     71 	}
     72 	return null;
     73 }
     74 
     75 
     76 // MAIN //
     77 
     78 /**
     79 * Returns a zero-filled contiguous linear ndarray data buffer.
     80 *
     81 * @param {string} dtype - data type
     82 * @param {NonNegativeInteger} size - buffer size
     83 * @returns {(Array|TypedArray|Buffer|null)} data buffer
     84 *
     85 * @example
     86 * var buf = buffer( 'float64', 3 );
     87 * // returns <Float64Array>[ 0.0, 0.0, 0.0 ]
     88 */
     89 function buffer( dtype, size ) {
     90 	if ( dtype === 'generic' ) {
     91 		return generic( size );
     92 	}
     93 	if ( dtype === 'binary' ) {
     94 		return binary( size );
     95 	}
     96 	return typedarray( dtype, size );
     97 }
     98 
     99 
    100 // EXPORTS //
    101 
    102 module.exports = buffer;