encoder.js
function getBestBinFit( start, len, blocks ) {
	var b, s, e, end = start + len, found = false,
		c, last_c = 0, last_s = 0, last_i = -1, last_bin = null;

	// Find the biggest chunk that can fit on these data
	for (var bi=0, bl=blocks.length; bi<bl; ++bi) {
		b = blocks[bi]; found = false;
		for (var i=0, il=b.length; i<il; ++i) {
			s = b[i][0]; e = s + b[i][1];

			// Find the common region of block-scan frame
			if ( ((s >= start) && (s < end-1)) ||	// Start in bounds
				 ((e >= start) && (e < end-1)) ||	// End in bounds
				 ((s <= start) && (e >= end)) )		// 
			{

				// Check bounds
				if (s < start) s = start;
				if (s > end-2) continue;
				if (e > end) e = end;
				if (s === e) continue;

				// Test coverage
				c = e - s;
				if (c > last_c) {
					last_c = c;
					last_s = s;
					last_bin = b[i];
					found = true;
				}

			}

		}

		// Prefer priority to length across different blocks
		// on the first block (repeated)
		if ((bi === 0) && found)
			return last_bin;

	}

	// Update bounds
	if (last_bin) {
		last_bin = last_bin.slice();
		last_bin[0] = last_s;
		last_bin[1] = last_c;
	}

	// Return last bin
	return last_bin;
}
function getBestBinFit( start, len, blocks ) {
	var b, s, e, end = start + len, found = false,
		c, last_c = 0, last_s = 0, last_i = -1, last_bin = null;

	// Find the biggest chunk that can fit on these data
	for (var bi=0, bl=blocks.length; bi<bl; ++bi) {
		b = blocks[bi]; found = false;
		for (var i=0, il=b.length; i<il; ++i) {
			s = b[i][0]; e = s + b[i][1];

			// Find the common region of block-scan frame
			if ( ((s >= start) && (s < end-1)) ||	// Start in bounds
				 ((e >= start) && (e < end-1)) ||	// End in bounds
				 ((s <= start) && (e >= end)) )		// 
			{

				// Check bounds
				if (s < start) s = start;
				if (s > end-2) continue;
				if (e > end) e = end;
				if (s === e) continue;

				// Test coverage
				c = e - s;
				if (c > last_c) {
					last_c = c;
					last_s = s;
					last_bin = b[i];
					found = true;
				}

			}

		}

		// Prefer priority to length across different blocks
		// on the first block (repeated)
		if ((bi === 0) && found)
			return last_bin;

	}

	// Update bounds
	if (last_bin) {
		last_bin = last_bin.slice();
		last_bin[0] = last_s;
		last_bin[1] = last_c;
	}

	// Return last bin
	return last_bin;
}
function getBestBinFit ( start , len , blocks ) { var b , s , e , end = start + len , found = false , c , last_c = 0 , last_s = 0 , last_i = - 1 , last_bin = null ; // Find the biggest chunk that can fit on these data for ( var bi = 0 , bl = blocks . length ; bi < bl ; ++ bi ) { b = blocks [ bi ] ; found = false ; for ( var i = 0 , il = b . length ; i < il ; ++ i ) { s = b [ i ] [ 0 ] ; e = s + b [ i ] [ 1 ] ; // Find the common region of block-scan frame if ( ( ( s >= start ) && ( s < end - 1 ) ) || // Start in bounds ( ( e >= start ) && ( e < end - 1 ) ) || // End in bounds ( ( s <= start ) && ( e >= end ) ) ) //  { // Check bounds if ( s < start ) s = start ; if ( s > end - 2 ) continue ; if ( e > end ) e = end ; if ( s === e ) continue ; // Test coverage c = e - s ; if ( c > last_c ) { last_c = c ; last_s = s ; last_bin = b [ i ] ; found = true ; } } } // Prefer priority to length across different blocks // on the first block (repeated) if ( ( bi === 0 ) && found ) return last_bin ; } // Update bounds if ( last_bin ) { last_bin = last_bin . slice ( ) ; last_bin [ 0 ] = last_s ; last_bin [ 1 ] = last_c ; } // Return last bin return last_bin ; }
Pack the specified number of bins to the specified bounds
Pack the specified number of bins to the specified bounds