package structures; import shared.Tools; /** * Holds counts of numbers for histograms. * Small numbers are stored as counts in an array. * Large numbers are stored individually in a LongList. * @author Brian Bushnell * */ public class SuperLongList { /*--------------------------------------------------------------*/ /*---------------- Initialization ----------------*/ /*--------------------------------------------------------------*/ public SuperLongList(){ this(100000); } /** * @param limit_ Length of array used to store small numbers. */ public SuperLongList(int limit_){ limit=limit_; array=new long[limit]; list=new LongList(); } /*--------------------------------------------------------------*/ /*---------------- Getters ----------------*/ /*--------------------------------------------------------------*/ public long[] array(){return array;} public LongList list(){return list;} public void addTo(long[] ca){ final int max=ca.length-1; { for(int i=0; i=0); if(x>0){increment(i, x);} } } /** Add or increment this key. * May result in duplicate copies appearing, which is fine. */ public void add(long x){ if(x=0) : "SLL does not support decrements."; if(x=thresh){return i;} } long prev=-1; for(int i=0; i=prev) : "Needs to be sorted ascending."; currentSum+=x; currentCount++; if(currentCount>=thresh){return x;} prev=x; } assert(false) : percentile+", "+count+", "+sum; return 0; } /** Returns value such that percentile of sum of values are below that value */ public long percentileValueBySum(double percentile){ // assert(sorted); long thresh=(long)(sum*percentile); long currentSum=0; long currentCount=0; for(int i=0; i=thresh){return i;} } long prev=-1; for(int i=0; i=prev) : "Needs to be sorted ascending."; currentSum+=x; currentCount++; if(currentSum>=thresh){return x;} prev=x; } assert(false) : percentile+", "+count+", "+sum; return 0; } /** Returns the sum of the lower percentile of values */ public long percentileSumByCount(double percentile){ // assert(sorted); long thresh=(long)(count*percentile); long currentSum=0; long currentCount=0; for(int i=0; i=thresh){ currentSum-=(x*i); currentCount-=i; while(currentCount=prev) : "Needs to be sorted ascending."; currentSum+=x; currentCount++; if(currentCount>=thresh){return currentSum;} prev=x; } assert(false) : percentile+", "+count+", "+sum; return 0; } /** Returns the number of lower values needed to sum to this percentile of the total sum */ public long percentileCountBySum(double percentile){ // assert(sorted); long thresh=(long)(sum*percentile); long currentSum=0; long currentCount=0; for(int i=0; i=thresh){ currentSum-=(x*i); currentCount-=i; while(currentSum=prev) : "Needs to be sorted ascending."; currentSum+=x; currentCount++; if(currentSum>=thresh){return currentCount;} prev=x; } assert(false) : percentile+", "+count+", "+sum; return 0; } //Slow, avoid using public long max(){ if(list.size>0){return list.max();} for(int i=array.length-1; i>=0; i--){ if(array[i]>0){return i;} } return 0; } public double mean(){ return sum/Tools.max(1.0, count); } public long median(){ return percentileValueByCount(0.5); } public long mode(){ long maxCount=0; long maxValue=0; for(int i=0; imaxCount){ maxCount=x; maxValue=i; } } long prev=-1; long currentCount=0; for(int i=0; imaxCount){ maxCount=currentCount; maxValue=x; } }else{ assert(x>prev) : "Needs to be sorted ascending."; prev=x; currentCount=1; } } return maxValue; } /*--------------------------------------------------------------*/ /*---------------- toString ----------------*/ /*--------------------------------------------------------------*/ @Override public String toString(){ ByteBuilder bb=new ByteBuilder(); bb.append('['); String comma=""; for(int i=0; i