time-to-botec

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

unzip.js (2827B)


      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 isInteger = require( '@stdlib/assert/is-integer' );
     24 var isArray = require( '@stdlib/assert/is-array' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Unzips a zipped array (i.e., a nested array of tuples).
     31 *
     32 * @param {Array} arr - zipped array
     33 * @param {Array} [idx] - array of indices specifying which tuple elements to unzip
     34 * @throws {TypeError} first argument must be a zipped array
     35 * @throws {TypeError} second argument must be an array of integer indices
     36 * @throws {Error} indices must be on the interval from zero to the tuple length
     37 * @returns {Array} array of unzipped arrays
     38 *
     39 * @example
     40 * var arr = [ [ 1, 'a', 3 ], [ 2, 'b', 4 ] ];
     41 *
     42 * var out = unzip( arr );
     43 * // returns [ [ 1, 2 ], [ 'a', 'b' ], [ 3, 4 ] ]
     44 *
     45 * @example
     46 * var arr = [ [ 1, 'a', 3 ], [ 2, 'b', 4 ] ];
     47 *
     48 * var out = unzip( arr, [ 0, 2 ] );
     49 * // returns [ [ 1, 2 ], [ 3, 4 ] ]
     50 */
     51 function unzip( arr, idx ) {
     52 	var numVals;
     53 	var len;
     54 	var out;
     55 	var tmp;
     56 	var i;
     57 	var j;
     58 	var k;
     59 
     60 	if ( !isArray( arr ) ) {
     61 		throw new TypeError( 'invalid argument. Must provide a zipped array.' );
     62 	}
     63 	len = arr.length;
     64 	for ( i = 0; i < len; i++ ) {
     65 		if ( !isArray( arr[i] ) ) {
     66 			throw new TypeError( 'invalid argument. Array must only contain arrays.' );
     67 		}
     68 	}
     69 	// Assume that the first tuple is representative of all tuples...
     70 	numVals = arr[ 0 ].length;
     71 	if ( arguments.length > 1 ) {
     72 		if ( !isArray( idx ) ) {
     73 			throw new TypeError( 'invalid argument. Indices must be specified as an array.' );
     74 		}
     75 		for ( i = 0; i < idx.length; i++ ) {
     76 			k = idx[ i ];
     77 			if ( !isInteger( k ) ) {
     78 				throw new TypeError( 'invalid argument. All indices must be integers.' );
     79 			}
     80 			if ( k < 0 || k > numVals ) {
     81 				throw new Error( 'invalid argument. Must provide valid indices; i.e., an index must be on the interval [0,len], where len is the tuple length.' );
     82 			}
     83 		}
     84 		numVals = idx.length;
     85 	} else {
     86 		idx = new Array( numVals );
     87 		for ( i = 0; i < numVals; i++ ) {
     88 			idx[ i ] = i;
     89 		}
     90 	}
     91 	out = new Array( numVals );
     92 	for ( j = 0; j < numVals; j++ ) {
     93 		tmp = new Array( len );
     94 		k = idx[ j ];
     95 		for ( i = 0; i < len; i++ ) {
     96 			tmp[ i ] = arr[ i ][ k ];
     97 		}
     98 		out[ j ] = tmp;
     99 	}
    100 	return out;
    101 }
    102 
    103 
    104 // EXPORTS //
    105 
    106 module.exports = unzip;