package it.cnr.isti.vir.util.bytes;

import it.cnr.isti.vir.features.ILongBinaryValues;
import it.cnr.isti.vir.util.RandomOperations;

import java.util.Collection;

public class LongBinaryUtil {
	
	public static long[] getMean(Collection coll) {
		long[][] temp = new long[coll.size()][];
		int i=0;
		for ( Object curr : coll ) {
			temp[i++] = ((ILongBinaryValues) curr).getElements();
		}
		return getMean( temp );
	}
	
	public static long[] getMeanFromLongs(Collection<long[]> coll) {
		long[][] temp = new long[coll.size()][];
		coll.toArray(temp);
		return getMean( temp );
	}
	
	public static long[] getMean(long[][] coll) {
		
		if ( coll.length == 0 ) return null;
		
		int nlongs = coll[0].length;
		int[] bitsSum = new int[nlongs*Long.SIZE];
		
		for ( long[] currVec : coll ) {
			//long[] currVec = it.next();
			int iBitSum = 0;
			for ( int iLong=0; iLong<currVec.length; iLong++ ) {
				// for each bit
				long mask = 1; 
				for ( int i=0; i<Long.SIZE; i++) {
					if ( (currVec[iLong] & mask) != 0 ) bitsSum[iBitSum]++;
					iBitSum++;
					mask = mask << 1;
				}
			}
		}
		
		long[] newValues = new long[nlongs];
		
		int threshold = coll.length / 2;
		long oneLong = 1;
		for ( int i=0; i<bitsSum.length; i++ ) {
			if ( bitsSum[i] > threshold
					||
				 (	bitsSum[i] == threshold
				 	&&
				 	RandomOperations.getBoolean() )
				 	) {
				newValues[i/Long.SIZE] ^= (oneLong << i%Long.SIZE );
			}
		}
		return newValues;
	}
}

@@ -1307,24 +1307,26 @@
  threshold%0D%0A
+//
 %09%09%09%09%09%7C%7C%0D%0A%09%09%09
@@ -1322,16 +1322,18 @@
 %09%09%09%09%7C%7C%0D%0A
+//
 %09%09%09%09 (%09b
@@ -1360,16 +1360,18 @@
 ld%0D%0A
+//
 %09%09%09%09 %09&&
 %0D%0A%09%09
@@ -1366,23 +1366,16 @@
 %09%09%09%09 %09&&
-%0D%0A%09%09%09%09 
 %09RandomO
@@ -1396,17 +1396,26 @@
 oolean()
- 
+%0D%0A//%09%09%09%09 %09
 )%0D%0A%09%09%09%09 

In evaluating the "mean", 0.5 is associated with 0 instead of randomly association