encoder.js
function encodeArray_Numeric( encoder, data, n_type ) {
	var na, keep = true, v, lv, same = true;

	// Separate small array case
	if (data.length < 256) {

		// If the numeric type is unknown, try to find actual type
		if (n_type === NUMTYPE.NUMERIC) {

			// Perform fast numeric analysis (skip delta)
			na = analyzeNumericArray( data, false );

			// Check if this is not a numeric array (getTypedArrayType can fail
			// in Array cases since it's optimised for speed, not accuracy)
			if (na === null) {
				keep = false;
			} else {
				n_type = na.type;
				same = na.same;
			}

		} else {

			// Test for same
			lv = data[0];
			for (var i=1, len=data.length; i<len; ++i) {
				v = data[i]; if (v !== lv) {
					same = false; break;
				} lv = v;
			}

		}

		// If for any reason this array is not numeric, don't
		// try to encode it
		if (keep) {

			if (same) {
				// If all values are the same, prefer repeated instead of short
				// console.log(">ARR>",data.length,"itms as REPEATED");
				encodeArray_NUM_REPEATED( encoder, data, n_type );
			} else {
				// Encode small numeric array
				// console.log(">ARR>",data.length,"itms as SHORT");
				encodeArray_NUM_SHORT( encoder, data, n_type );
			}

		} else {

			// Pass it to primitive encoder
			encodeArray_Primitive( encoder, data );

		}

	} else {

		// Perform full numeric type analysis and encode numeric array
		na = analyzeNumericArray( data, true );

		// Check if this is not a numeric array (getTypedArrayType can fail
		// in Array cases since it's optimised for speed, not accuracy)
		if (na !== null) {

			// Define generic type
			if (n_type === NUMTYPE.NUMERIC) {
				n_type = na.type;
			}

			// If all values are the same, encode using same numeric encoding
			if (na.same) {
				// console.log(">ARR>",data.length,"itms as REPEATED");
				encodeArray_NUM_REPEATED( encoder, data, n_type );
				return;
			}

			// If we have more than <thresshold> items with the same
			// value, break into chunked array
			if (na.psame >= encoder.optimize.repeat_break_thresshold) {
				encodeArray_Primitive( encoder, data );
				return;
			}

			// Perform detailed analysis for downscaling and delta-encoding
			var n_dws = getDownscaleType( n_type, na );
			// console.log(">>[DWS]>> n_type="+_NUMTYPE[n_type]+", analysis=",na,":",_NUMTYPE[n_dws]);

			// Get sizes of different encoding approaches
			var b_raw = sizeOfType( n_type ),
				b_dws = sizeOfType( n_dws ),
				b_delta = sizeOfType( na.delta_type ),
				b_min = Math.min(b_raw, b_dws, b_delta);

			// Pick best, according to speed preference
			if (b_min === b_raw) {
				// Encode raw
				// console.log(">ARR>",data.length,"itms as RAW (type="+_NUMTYPE[n_type]+")");
				encodeArray_NUM_RAW( encoder, data, n_type );
			} else if (b_min === b_dws) {
				// Encode downscaled
				// console.log(">ARR>",data.length,"itms as DOWNSCALED");
				encodeArray_NUM_DWS( encoder, data, n_type, n_dws );
			} else if (b_min === b_delta) {
				// Encode delta
				if ( encoder.optimize.enable_delta ) {
					// console.log(">ARR>",data.length,"itms as DELTA");
					if (na.dmode == 1) {
						encodeArray_NUM_DELTA_INT( encoder, data, n_type, na.delta_type );
						return;
					} else if ((na.dmode == 2) && encoder.optimize.float_int_downscale) {
						encodeArray_NUM_DELTA_FLOAT( encoder, data, n_type, na.delta_type, na.mean, na.fscale );
						return;
					}
				}
				// Fallback case -> Raw encode
				// console.log(">ARR>",data.length,"itms as RAW (type="+_NUMTYPE[n_type]+") (Fallback)");
				encodeArray_NUM_RAW( encoder, data, n_type );
			}

		} else {

			// Pass it to primitive encoder
			encodeArray_Primitive( encoder, data );

		}

	}

}
function encodeArray_Numeric( encoder, data, n_type ) {
	var na, keep = true, v, lv, same = true;

	// Separate small array case
	if (data.length < 256) {

		// If the numeric type is unknown, try to find actual type
		if (n_type === NUMTYPE.NUMERIC) {

			// Perform fast numeric analysis (skip delta)
			na = analyzeNumericArray( data, false );

			// Check if this is not a numeric array (getTypedArrayType can fail
			// in Array cases since it's optimised for speed, not accuracy)
			if (na === null) {
				keep = false;
			} else {
				n_type = na.type;
				same = na.same;
			}

		} else {

			// Test for same
			lv = data[0];
			for (var i=1, len=data.length; i<len; ++i) {
				v = data[i]; if (v !== lv) {
					same = false; break;
				} lv = v;
			}

		}

		// If for any reason this array is not numeric, don't
		// try to encode it
		if (keep) {

			if (same) {
				// If all values are the same, prefer repeated instead of short
				// console.log(">ARR>",data.length,"itms as REPEATED");
				encodeArray_NUM_REPEATED( encoder, data, n_type );
			} else {
				// Encode small numeric array
				// console.log(">ARR>",data.length,"itms as SHORT");
				encodeArray_NUM_SHORT( encoder, data, n_type );
			}

		} else {

			// Pass it to primitive encoder
			encodeArray_Primitive( encoder, data );

		}

	} else {

		// Perform full numeric type analysis and encode numeric array
		na = analyzeNumericArray( data, true );

		// Check if this is not a numeric array (getTypedArrayType can fail
		// in Array cases since it's optimised for speed, not accuracy)
		if (na !== null) {

			// Define generic type
			if (n_type === NUMTYPE.NUMERIC) {
				n_type = na.type;
			}

			// If all values are the same, encode using same numeric encoding
			if (na.same) {
				// console.log(">ARR>",data.length,"itms as REPEATED");
				encodeArray_NUM_REPEATED( encoder, data, n_type );
				return;
			}

			// If we have more than <thresshold> items with the same
			// value, break into chunked array
			if (na.psame >= encoder.optimize.repeat_break_thresshold) {
				encodeArray_Primitive( encoder, data );
				return;
			}

			// Perform detailed analysis for downscaling and delta-encoding
			var n_dws = getDownscaleType( n_type, na );
			// console.log(">>[DWS]>> n_type="+_NUMTYPE[n_type]+", analysis=",na,":",_NUMTYPE[n_dws]);

			// Get sizes of different encoding approaches
			var b_raw = sizeOfType( n_type ),
				b_dws = sizeOfType( n_dws ),
				b_delta = sizeOfType( na.delta_type ),
				b_min = Math.min(b_raw, b_dws, b_delta);

			// Pick best, according to speed preference
			if (b_min === b_raw) {
				// Encode raw
				// console.log(">ARR>",data.length,"itms as RAW (type="+_NUMTYPE[n_type]+")");
				encodeArray_NUM_RAW( encoder, data, n_type );
			} else if (b_min === b_dws) {
				// Encode downscaled
				// console.log(">ARR>",data.length,"itms as DOWNSCALED");
				encodeArray_NUM_DWS( encoder, data, n_type, n_dws );
			} else if (b_min === b_delta) {
				// Encode delta
				if ( encoder.optimize.enable_delta ) {
					// console.log(">ARR>",data.length,"itms as DELTA");
					if (na.dmode == 1) {
						encodeArray_NUM_DELTA_INT( encoder, data, n_type, na.delta_type );
						return;
					} else if ((na.dmode == 2) && encoder.optimize.float_int_downscale) {
						encodeArray_NUM_DELTA_FLOAT( encoder, data, n_type, na.delta_type, na.mean, na.fscale );
						return;
					}
				}
				// Fallback case -> Raw encode
				// console.log(">ARR>",data.length,"itms as RAW (type="+_NUMTYPE[n_type]+") (Fallback)");
				encodeArray_NUM_RAW( encoder, data, n_type );
			}

		} else {

			// Pass it to primitive encoder
			encodeArray_Primitive( encoder, data );

		}

	}

}
function encodeArray_Numeric ( encoder , data , n_type ) { var na , keep = true , v , lv , same = true ; // Separate small array case if ( data . length < 256 ) { // If the numeric type is unknown, try to find actual type if ( n_type === NUMTYPE . NUMERIC ) { // Perform fast numeric analysis (skip delta) na = analyzeNumericArray ( data , false ) ; // Check if this is not a numeric array (getTypedArrayType can fail // in Array cases since it's optimised for speed, not accuracy) if ( na === null ) { keep = false ; } else { n_type = na . type ; same = na . same ; } } else { // Test for same lv = data [ 0 ] ; for ( var i = 1 , len = data . length ; i < len ; ++ i ) { v = data [ i ] ; if ( v !== lv ) { same = false ; break ; } lv = v ; } } // If for any reason this array is not numeric, don't // try to encode it if ( keep ) { if ( same ) { // If all values are the same, prefer repeated instead of short // console.log(">ARR>",data.length,"itms as REPEATED"); encodeArray_NUM_REPEATED ( encoder , data , n_type ) ; } else { // Encode small numeric array // console.log(">ARR>",data.length,"itms as SHORT"); encodeArray_NUM_SHORT ( encoder , data , n_type ) ; } } else { // Pass it to primitive encoder encodeArray_Primitive ( encoder , data ) ; } } else { // Perform full numeric type analysis and encode numeric array na = analyzeNumericArray ( data , true ) ; // Check if this is not a numeric array (getTypedArrayType can fail // in Array cases since it's optimised for speed, not accuracy) if ( na !== null ) { // Define generic type if ( n_type === NUMTYPE . NUMERIC ) { n_type = na . type ; } // If all values are the same, encode using same numeric encoding if ( na . same ) { // console.log(">ARR>",data.length,"itms as REPEATED"); encodeArray_NUM_REPEATED ( encoder , data , n_type ) ; return ; } // If we have more than <thresshold> items with the same // value, break into chunked array if ( na . psame >= encoder . optimize . repeat_break_thresshold ) { encodeArray_Primitive ( encoder , data ) ; return ; } // Perform detailed analysis for downscaling and delta-encoding var n_dws = getDownscaleType ( n_type , na ) ; // console.log(">>[DWS]>> n_type="+_NUMTYPE[n_type]+", analysis=",na,":",_NUMTYPE[n_dws]); // Get sizes of different encoding approaches var b_raw = sizeOfType ( n_type ) , b_dws = sizeOfType ( n_dws ) , b_delta = sizeOfType ( na . delta_type ) , b_min = Math . min ( b_raw , b_dws , b_delta ) ; // Pick best, according to speed preference if ( b_min === b_raw ) { // Encode raw // console.log(">ARR>",data.length,"itms as RAW (type="+_NUMTYPE[n_type]+")"); encodeArray_NUM_RAW ( encoder , data , n_type ) ; } else if ( b_min === b_dws ) { // Encode downscaled // console.log(">ARR>",data.length,"itms as DOWNSCALED"); encodeArray_NUM_DWS ( encoder , data , n_type , n_dws ) ; } else if ( b_min === b_delta ) { // Encode delta if ( encoder . optimize . enable_delta ) { // console.log(">ARR>",data.length,"itms as DELTA"); if ( na . dmode == 1 ) { encodeArray_NUM_DELTA_INT ( encoder , data , n_type , na . delta_type ) ; return ; } else if ( ( na . dmode == 2 ) && encoder . optimize . float_int_downscale ) { encodeArray_NUM_DELTA_FLOAT ( encoder , data , n_type , na . delta_type , na . mean , na . fscale ) ; return ; } } // Fallback case -> Raw encode // console.log(">ARR>",data.length,"itms as RAW (type="+_NUMTYPE[n_type]+") (Fallback)"); encodeArray_NUM_RAW ( encoder , data , n_type ) ; } } else { // Pass it to primitive encoder encodeArray_Primitive ( encoder , data ) ; } } }
Encode an array that is already classified as numeric
Encode an array that is already classified as numeric