time-to-botec

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

tabulate.js (2260B)


      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 isCollection = require( '@stdlib/assert/is-collection' );
     24 var indexOf = require( './../../index-of' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Generates a frequency table.
     31 *
     32 * ## Notes
     33 *
     34 * -   The output is an array of arrays. Each sub-array corresponds to a unique value in the input collection and is structured as follows:
     35 *
     36 *     -   0: unique value
     37 *     -   1: value count
     38 *     -   2: frequency percentage
     39 *
     40 *
     41 * @param {Collection} collection - input collection
     42 * @throws {TypeError} first argument must be a collection
     43 * @returns {(Array<Array>|Array)} frequency table
     44 *
     45 * @example
     46 * var arr = [ 'beep', 'boop', 'foo', 'beep' ];
     47 *
     48 * var out = tabulate( arr );
     49 * // returns [ [ 'beep', 2, 0.5 ], [ 'boop', 1, 0.25 ], [ 'foo', 1, 0.25 ] ]
     50 */
     51 function tabulate( collection ) {
     52 	var count;
     53 	var tmp;
     54 	var len;
     55 	var out;
     56 	var v;
     57 	var i;
     58 	var j;
     59 	if ( !isCollection( collection ) ) {
     60 		throw new TypeError( 'invalid argument. First argument must be a collection. Value: `'+collection+'`.' );
     61 	}
     62 	count = 0;
     63 	tmp = [];
     64 	out = [];
     65 
     66 	// For each collection element, determine if we've seen the element before. If not, cache a reference which points to its location in the output array; otherwise, update the running count.
     67 	len = collection.length;
     68 	for ( i = 0; i < len; i++ ) {
     69 		v = collection[ i ];
     70 		count += 1;
     71 		j = indexOf( tmp, v );
     72 		if ( j === -1 ) {
     73 			tmp.push( v );
     74 			out.push( [ v, 1, 0 ] );
     75 		} else {
     76 			out[ j ][ 1 ] += 1;
     77 		}
     78 	}
     79 	// Compute percentages...
     80 	len = out.length;
     81 	for ( i = 0; i < len; i++ ) {
     82 		out[ i ][ 2 ] = out[ i ][ 1 ] / count;
     83 	}
     84 	return out;
     85 }
     86 
     87 
     88 // EXPORTS //
     89 
     90 module.exports = tabulate;