Debian Codegraph @

current/cardinality/LogLog.java

raw symbols SCIP
1 package cardinality;
2
3 import java.util.concurrent.atomic.AtomicIntegerArray;
4
5 import shared.Parser;
6 import shared.Tools;
7 import structures.LongList;
8
9 /**
10 * @author Brian Bushnell
11 * @date Sep 30, 2015
12 *
13 */
14 public final class LogLog extends CardinalityTracker {
15
16 /*--------------------------------------------------------------*/
17 /*---------------- Initialization ----------------*/
18 /*--------------------------------------------------------------*/
19
20 /** Create a LogLog with default parameters */
21 LogLog(){
22 this(2048, 31, -1, 0);
23 }
24
25 /** Create a LogLog with parsed parameters */
26 LogLog(Parser p){
27 super(p);
28 //assert(atomic);
29 maxArrayA=(atomic ? new AtomicIntegerArray(buckets) : null);
30 maxArray=(atomic ? null : new int[buckets]);
31 }
32
33 /**
34 * Create a LogLog with specified parameters
35 * @param buckets_ Number of buckets (counters)
36 * @param k_ Kmer length
37 * @param seed Random number generator seed; -1 for a random seed
38 * @param minProb_ Ignore kmers with under this probability of being correct
39 */
40 LogLog(int buckets_, int k_, long seed, float minProb_){
41 super(buckets_, k_, seed, minProb_);
42 //assert(atomic);
43 maxArrayA=(atomic ? new AtomicIntegerArray(buckets) : null);
44 maxArray=(atomic ? null : new int[buckets]);
45 }
46
47 /*--------------------------------------------------------------*/
48 /*---------------- Methods ----------------*/
49 /*--------------------------------------------------------------*/
50
51 //Restores floating point to integer
52 private long restore(int score){
53 int leading=score;
54 long mantissa=1;
55 int shift=64-leading-1;
56 long original=mantissa<<shift;
57 return original;
58 }
59
60 @Override
61 public final long cardinality(){
62 double difSum=0;
63 double estLogSum=0;
64 int count=0;
65 LongList list=new LongList(buckets);
66 //assert(atomic);
67 if(atomic){
68 for(int i=0; i<maxArrayA.length(); i++){
69 int max=maxArrayA.get(i);
70 long val=restore(max);
71 if(max>0 && val>0){
72 // long val=restore(max);
73 // System.err.println("val="+val);
74 final long dif=val;
75 difSum+=dif;
76 count++;
77 double est=2*(Long.MAX_VALUE/(double)dif)*SKIPMOD;
78 estLogSum+=Math.log(est);
79 list.add(dif);
80 }
81 }
82 }else{
83 for(int i=0; i<maxArray.length; i++){
84 int max=maxArray[i];
85 long val=restore(max);
86 if(max>0 && val>0){
87 // long val=restore(max);
88 final long dif=val;
89 difSum+=dif;
90 count++;
91 double est=2*(Long.MAX_VALUE/(double)dif)*SKIPMOD;
92 estLogSum+=Math.log(est);
93 list.add(dif);
94 }
95 }
96 }
97 final int div=count;//Could also be count be that causes problems
98 final double mean=difSum/Tools.max(div, 1);
99 list.sort();
100 final long median=list.median();
101 final double mwa=list.medianWeightedAverage();
102
103 //What to use as the value from the counters
104 final double proxy=mean;
105
106 // assert(false) : mean+", "+median+", "+difSum+", "+list;
107
108 final double estimatePerSet=2*(Long.MAX_VALUE/proxy)*SKIPMOD;
109 final double conversionFactor=0.7213428177;
110 final double total=conversionFactor*estimatePerSet*div*((count+buckets)/(float)(buckets+buckets));
111
112 // final double estSum=div*Math.exp(estLogSum/(Tools.max(div, 1)));
113 // double medianEst=2*(Long.MAX_VALUE/(double)median)*SKIPMOD*div;
114
115 // new Exception().printStackTrace();
116
117 // System.err.println(maxArray);
118 //// Overall, it looks like "total" is the best, then "estSum", then "medianEst" is the worst, in terms of variance.
119 // System.err.println("difSum="+difSum+", count="+count+", mean="+mean+", est="+estimatePerSet+", total="+(long)total);
120 // System.err.println("estSum="+(long)estSum+", median="+median+", medianEst="+(long)medianEst);
121
122 long cardinality=(long)(total);
123 lastCardinality=cardinality;
124 return cardinality;
125 }
126
127 // @Override
128 // public final long cardinality(){
129 // long sum=0;
130 // //assert(atomic);
131 // if(atomic){
132 // for(int i=0; i<maxArray.length(); i++){
133 // sum+=maxArray.get(i);
134 // }
135 // }else{
136 // for(int i=0; i<maxArray2.length; i++){
137 // sum+=maxArray2[i];
138 // }
139 // }
140 // double mean=sum/(double)buckets;
141 // long cardinality=(long)((((Math.pow(2, mean)-1)*buckets*SKIPMOD))/1.258275);
142 // lastCardinality=cardinality;
143 // return cardinality;
144 // }
145
146 public final long cardinalityH(){
147 double sum=0;
148 for(int i=0; i<maxArrayA.length(); i++){
149 int x=Tools.max(1, maxArrayA.get(i));
150 sum+=1.0/x;
151 }
152 double mean=buckets/sum;
153 return (long)((Math.pow(2, mean)*buckets*SKIPMOD));
154 }
155
156 @Override
157 public final void add(CardinalityTracker log){
158 assert(log.getClass()==this.getClass());
159 add((LogLog)log);
160 }
161
162 public void add(LogLog log){
163 if(atomic && maxArrayA!=log.maxArrayA){
164 for(int i=0; i<buckets; i++){
165 maxArrayA.set(i, Tools.max(maxArrayA.get(i), log.maxArrayA.get(i)));
166 }
167 }else if(maxArray!=log.maxArray){
168 for(int i=0; i<buckets; i++){
169 maxArray[i]=Tools.max(maxArray[i], log.maxArray[i]);
170 }
171 }
172 }
173
174 @Override
175 public void hashAndStore(final long number){
176 // if(number%SKIPMOD!=0){return;} //Slows down moderately
177 long key=number;
178
179 // key=hash(key, tables[((int)number)&numTablesMask]);
180
181 key=Tools.hash64shift(key);
182 // if(key<0 || key>maxHashedValue){return;}//Slows things down by 50% lot, mysteriously
183 int leading=Long.numberOfLeadingZeros(key);
184
185 // counts[leading]++;
186
187 // if(leading<3){return;}//Slows things down slightly
188 // final int bucket=(int)((number&Integer.MAX_VALUE)%buckets);
189 final int bucket=(int)(key&bucketMask);
190
191 if(atomic){
192 int x=maxArrayA.get(bucket);
193 while(leading>x){
194 boolean b=maxArrayA.compareAndSet(bucket, x, leading);
195 if(b){x=leading;}
196 else{x=maxArrayA.get(bucket);}
197 }
198 }else{
199 maxArray[bucket]=Tools.max(leading, maxArray[bucket]);
200 }
201 }
202
203 @Override
204 public final float[] compensationFactorLogBucketsArray(){
205 return compensationFactorLogBucketsArray;
206 }
207
208 /*--------------------------------------------------------------*/
209 /*---------------- Fields ----------------*/
210 /*--------------------------------------------------------------*/
211
212 /** Maintains state. These are the actual buckets. */
213 private final int[] maxArray;
214 /** Atomic version of maxArray. */
215 private final AtomicIntegerArray maxArrayA;
216
217 private static final float[] compensationFactorLogBucketsArray={
218 0.053699781f, 0.49556874f, 0.742263622f, 0.861204899f, 0.926038294f,
219 0.967001269f, 0.982949748f, 0.992495155f, 0.996185775f, 0.998077246f
220 };
221
222 }