Debian Codegraph @

current/cardinality/BBLog.java

raw symbols SCIP
1 package cardinality;
2
3 import java.util.concurrent.atomic.AtomicLongArray;
4
5 import shared.Parser;
6 import shared.Tools;
7 import structures.LongList;
8
9 /**
10 * @author Brian Bushnell
11 * @date Feb 20, 2020
12 *
13 */
14 public final class BBLog extends CardinalityTracker {
15
16 /*--------------------------------------------------------------*/
17 /*---------------- Initialization ----------------*/
18 /*--------------------------------------------------------------*/
19
20 /** Create a LogLog with default parameters */
21 BBLog(){
22 this(2048, 31, -1, 0);
23 }
24
25 /** Create a LogLog with parsed parameters */
26 BBLog(Parser p){
27 super(p);
28 maxArrayA=(atomic ? new AtomicLongArray(buckets) : null);
29 maxArray=(atomic ? null : new long[buckets]);
30 counts=(trackCounts ? new int[buckets] : null);
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 BBLog(int buckets_, int k_, long seed, float minProb_){
41 super(buckets_, k_, seed, minProb_);
42 maxArrayA=(atomic ? new AtomicLongArray(buckets) : null);
43 maxArray=(atomic ? null : new long[buckets]);
44 counts=(trackCounts ? new int[buckets] : null);
45 }
46
47 /*--------------------------------------------------------------*/
48 /*---------------- Methods ----------------*/
49 /*--------------------------------------------------------------*/
50
51 @Override
52 public final long cardinality(){
53 double difSum=0;
54 double estLogSum=0;
55 int count=0;
56 LongList list=new LongList(buckets);
57 //assert(atomic);
58 if(atomic){
59 for(int i=0; i<maxArrayA.length(); i++){
60 long val=maxArrayA.get(i);
61 if(val>0){
62 // System.err.println("val="+val);
63 long dif=Long.MAX_VALUE-val;
64 difSum+=dif;
65 count++;
66 double est=2*(Long.MAX_VALUE/(double)dif)*SKIPMOD;
67 estLogSum+=Math.log(est);
68 list.add(dif);
69 }
70 }
71 }else{
72 for(int i=0; i<maxArray.length; i++){
73 long val=maxArray[i];
74 if(val>0){
75 long dif=Long.MAX_VALUE-val;
76 difSum+=dif;
77 count++;
78 double est=2*(Long.MAX_VALUE/(double)dif)*SKIPMOD;
79 estLogSum+=Math.log(est);
80 list.add(dif);
81 }
82 }
83 }
84 int div=count;//Could also be count be that causes problems
85 final double mean=difSum/Tools.max(div, 1);
86 final double estimatePerSet=2*(Long.MAX_VALUE/mean)*SKIPMOD;
87 final double total=estimatePerSet*div*((count+buckets)/(float)(buckets+buckets));
88
89 final double estSum=div*Math.exp(estLogSum/(Tools.max(div, 1)));
90 list.sort();
91 long median=list.median();
92 double medianEst=2*(Long.MAX_VALUE/(double)median)*SKIPMOD*div;
93
94 // new Exception().printStackTrace();
95
96 // System.err.println(maxArray);
97 // //Overall, it looks like "total" is the best, then "estSum", then "medianEst" is the worst, in terms of variance.
98 // System.err.println("difSum="+difSum+", count="+count+", mean="+mean+", est="+estimatePerSet+", total="+(long)total);
99 // System.err.println("estSum="+(long)estSum+", median="+median+", medianEst="+(long)medianEst);
100
101 long cardinality=(long)(total);
102 lastCardinality=cardinality;
103 return cardinality;
104 }
105
106 @Override
107 public int[] getCounts(){
108 return counts;
109 }
110
111 @Override
112 public final void add(CardinalityTracker log){
113 assert(log.getClass()==this.getClass());
114 add((BBLog)log);
115 }
116
117 public void add(BBLog log){
118 if(atomic && maxArrayA!=log.maxArrayA){
119 for(int i=0; i<buckets; i++){
120 maxArrayA.set(i, Tools.max(maxArrayA.get(i), log.maxArrayA.get(i)));
121 }
122 }else
123 if(maxArray!=log.maxArray){
124 if(counts==null){
125 for(int i=0; i<buckets; i++){
126 maxArray[i]=Tools.max(maxArray[i], log.maxArray[i]);
127 }
128 }else{
129 for(int i=0; i<buckets; i++){
130 final long a=maxArray[i], b=log.maxArray[i];
131 if(a==b){
132 counts[i]+=log.counts[i];
133 }else if(b>a){
134 maxArray[i]=b;
135 counts[i]=log.counts[i];
136 }
137 }
138 }
139 }
140 }
141
142 @Override
143 public void hashAndStore(final long number){
144 // if(number%SKIPMOD!=0){return;}
145 // final long key=hash(number, tables[((int)number)&numTablesMask]);
146 final long key=Tools.hash64shift(number);
147
148 // if(key<minKey){return;}
149 final int bucket=(int)(key&bucketMask);
150
151 if(atomic){
152 long x=maxArrayA.get(bucket);
153 while(key>x){
154 boolean b=maxArrayA.compareAndSet(bucket, x, key);
155 if(b){x=key;}
156 else{x=maxArrayA.get(bucket);}
157 }
158 }else{
159 if(trackCounts){
160 if(key>maxArray[bucket]){
161 maxArray[bucket]=key;
162 counts[bucket]=1;
163 }else if(key==maxArray[bucket]){
164 counts[bucket]++;
165 }
166 }else{
167 maxArray[bucket]=Tools.max(key, maxArray[bucket]);
168 }
169 }
170 }
171
172 @Override
173 public final float[] compensationFactorLogBucketsArray(){
174 return null;
175 }
176
177 /*--------------------------------------------------------------*/
178 /*---------------- Fields ----------------*/
179 /*--------------------------------------------------------------*/
180
181 /** Maintains state. These are the actual buckets. */
182 private final long[] maxArray;
183 /** Atomic version of maxArray. */
184 private final AtomicLongArray maxArrayA;
185 /** Counts associated with values in the buckets. */
186 private final int[] counts;
187
188 // private static long minKey=(long)(0.75f*Long.MAX_VALUE); //non-atomic 15% faster without this
189
190 }