comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/bbmap-39.01-1/current/bloom/KCountArray3.java @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
comparison
equal deleted inserted replaced
67:0e9998148a16 68:5028fdace37b
1 package bloom;
2
3 /**
4 * @author Brian Bushnell
5 * @date Aug 17, 2012
6 *
7 */
8 public class KCountArray3 extends KCountArray {
9
10 /**
11 *
12 */
13 private static final long serialVersionUID = -5466091642729698944L;
14
15 public KCountArray3(long cells_, int bits_){
16 super(cells_, bits_);
17 long words=cells/cellsPerWord;
18 int wordsPerArray=(int)(words/numArrays);
19 matrix=new int[numArrays][wordsPerArray];
20 }
21
22 @Override
23 public int read(long key){
24 if(verbose){System.err.println("Reading "+key);}
25 // System.out.println("key="+key);
26 int arrayNum=(int)(key&arrayMask);
27 // System.out.println("array="+arrayNum);
28 key>>>=arrayBits;
29 // System.out.println("key2="+key);
30 int[] array=matrix[arrayNum];
31 int index=(int)(key>>>indexShift);
32 // System.out.println("index="+index);
33 int word=array[index];
34 // System.out.println("word="+Integer.toHexString(word));
35 int cellShift=(int)(cellBits*key);
36 // System.out.println("cellShift="+cellShift);
37 int value=(int)((word>>>cellShift)&valueMask);
38 if(verbose){System.err.println("Read "+value);}
39 return value;
40 }
41
42 @Override
43 public void write(long key, int value){
44 if(verbose){System.err.println("Writing "+key+", "+value);}
45 int arrayNum=(int)(key&arrayMask);
46 key>>>=arrayBits;
47 int[] array=matrix[arrayNum];
48 int index=(int)(key>>>indexShift);
49 int word=array[index];
50 int cellShift=(int)(cellBits*key);
51 word=(value<<cellShift)|(word&~((valueMask)<<cellShift));
52 array[index]=word;
53 }
54
55 // static int count138=0;
56 @Override
57 public void increment(long key, int incr){
58 if(verbose){System.err.println("*** Incrementing "+key);}
59 // if(key==138){
60 // assert(count138==0) : count138;
61 // count138++;
62 // }
63 int arrayNum=(int)(key&arrayMask);
64 key>>>=arrayBits;
65 int[] array=matrix[arrayNum];
66 int index=(int)(key>>>indexShift);
67 int word=array[index];
68 int cellShift=(int)(cellBits*key);
69 int value=((word>>>cellShift)&valueMask);
70 if(value==0 && incr>0){cellsUsed++;}
71 else if(incr<0 && value+incr==0){cellsUsed--;}
72 value=min(value+incr, maxValue);
73 word=(value<<cellShift)|(word&~((valueMask)<<cellShift));
74 array[index]=word;
75 if(verbose){System.err.println("Returning "+value);}
76 //return (int)value;
77 }
78
79 /** Returns unincremented value */
80 @Override
81 public int incrementAndReturnUnincremented(long key, int incr){
82 if(verbose){System.err.println("Incrementing2 "+key);}
83 int arrayNum=(int)(key&arrayMask);
84 key>>>=arrayBits;
85 int[] array=matrix[arrayNum];
86 int index=(int)(key>>>indexShift);
87 int word=array[index];
88 int cellShift=(int)(cellBits*key);
89 final int value=((word>>>cellShift)&valueMask);
90 final int value2=min(value+incr, maxValue);
91 word=(value2<<cellShift)|(word&~((valueMask)<<cellShift));
92 array[index]=word;
93 if(verbose){System.err.println("Returning "+value);}
94 return value;
95 }
96
97 @Override
98 public long[] transformToFrequency(){
99 return transformToFrequency(matrix);
100 }
101
102 @Override
103 public String toContentsString(){
104 StringBuilder sb=new StringBuilder();
105 sb.append("[");
106 String comma="";
107 for(int[] array : matrix){
108 for(int i=0; i<array.length; i++){
109 int word=array[i];
110 for(int j=0; j<cellsPerWord; j++){
111 int x=word&valueMask;
112 sb.append(comma);
113 sb.append(x);
114 word>>>=cellBits;
115 comma=", ";
116 }
117 }
118 }
119 sb.append("]");
120 return sb.toString();
121 }
122
123 @Override
124 public double usedFraction(){return cellsUsed/(double)cells;}
125
126 @Override
127 public double usedFraction(int mindepth){return cellsUsed(mindepth)/(double)cells;}
128
129 @Override
130 public long cellsUsed(int mindepth){
131 long count=0;
132 for(int[] array : matrix){
133 if(array!=null){
134 for(int word : array){
135 while(word>0){
136 int x=word&valueMask;
137 if(x>=mindepth){count++;}
138 word>>>=cellBits;
139 }
140 }
141 }
142 }
143 return count;
144 }
145
146 @Override
147 long hash(long x, int y) {
148 assert(false) : "Unsupported.";
149 return x;
150 }
151
152 private long cellsUsed;
153 private final int[][] matrix;
154
155 }