| 1 |
package structures; |
| 2 |
|
| 3 |
import shared.Tools; |
| 4 |
|
| 5 |
public class MultiBitSet extends AbstractBitSet { |
| 6 |
|
| 7 |
public static void main(String[] args){ |
| 8 |
MultiBitSet mbs=new MultiBitSet(20); |
| 9 |
System.err.println(mbs); |
| 10 |
mbs.increment(5, 2); |
| 11 |
System.err.println(mbs); |
| 12 |
mbs.increment(5, 1); |
| 13 |
System.err.println(mbs); |
| 14 |
mbs.increment(1, 1); |
| 15 |
System.err.println(mbs); |
| 16 |
mbs.increment(2, 1); |
| 17 |
System.err.println(mbs); |
| 18 |
mbs.increment(7, 3); |
| 19 |
System.err.println(mbs); |
| 20 |
mbs.increment(8, 4); |
| 21 |
System.err.println(mbs); |
| 22 |
mbs.increment(15, 2); |
| 23 |
System.err.println(mbs); |
| 24 |
mbs.increment(15, 2); |
| 25 |
System.err.println(mbs); |
| 26 |
mbs.increment(5, 2); |
| 27 |
System.err.println(mbs); |
| 28 |
} |
| 29 |
|
| 30 |
MultiBitSet(long capacity_){ |
| 31 |
setCapacity(capacity_, 0); |
| 32 |
} |
| 33 |
|
| 34 |
MultiBitSet(long capacity_, int extra){ |
| 35 |
setCapacity(capacity_, extra); |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public void addToCell(final int cell, final int other){ |
| 40 |
if(other==0){return;} |
| 41 |
int a=array[cell], b=other, c=0; |
| 42 |
for(int i=0; i<elementsPerCell; i++){ |
| 43 |
int av=a&elementMask; |
| 44 |
int bv=b&elementMask; |
| 45 |
int sum=Tools.min(av+bv, elementMask); |
| 46 |
int mask=sum<<(bits*i); |
| 47 |
c|=mask; |
| 48 |
a>>>=bits; |
| 49 |
b>>>=bits; |
| 50 |
} |
| 51 |
array[cell]=c; |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public void setToMax(final int cell, final int other){ |
| 56 |
if(other==0){return;} |
| 57 |
int a=array[cell], b=other, c=0; |
| 58 |
for(int i=0; i<elementsPerCell; i++){ |
| 59 |
int av=a&elementMask; |
| 60 |
int bv=b&elementMask; |
| 61 |
int max=Tools.max(av, bv); |
| 62 |
int mask=max<<(bits*i); |
| 63 |
c|=mask; |
| 64 |
a>>>=bits; |
| 65 |
b>>>=bits; |
| 66 |
} |
| 67 |
array[cell]=c; |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public void increment(int x, int amt){ |
| 72 |
assert(amt>0); |
| 73 |
assert(x>=0 && x<=capacity); |
| 74 |
final int cell=x/elementsPerCell; |
| 75 |
final int element=x&modulus; |
| 76 |
|
| 77 |
final int shift=bits*element; |
| 78 |
|
| 79 |
final int mask=elementMask<<shift; |
| 80 |
|
| 81 |
final int contents=array[cell]; |
| 82 |
|
| 83 |
final int currentValue=(contents&mask)>>>shift; |
| 84 |
|
| 85 |
final int newValue=Tools.min(currentValue+amt, elementMask); |
| 86 |
|
| 87 |
if(newValue==currentValue){return;} |
| 88 |
final int update=((contents&~mask)|(newValue<<shift)); |
| 89 |
|
| 90 |
array[cell]=update; |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public int getCount(int x){ |
| 95 |
assert(x>=0 && x<=capacity); |
| 96 |
final int cell=x/elementsPerCell; |
| 97 |
final int element=x&modulus; |
| 98 |
final int shift=bits*element; |
| 99 |
final int contents=array[cell]; |
| 100 |
final int currentValue=(contents>>>shift)&elementMask; |
| 101 |
return currentValue; |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
public void clear(){ |
| 106 |
for(int i=0; i<length; i++){ |
| 107 |
array[i]=0; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public long cardinality(){ |
| 113 |
long sum=0; |
| 114 |
for(int i=0; i<length; i++){ |
| 115 |
int value=array[i]; |
| 116 |
while(value!=0){ |
| 117 |
if((value&elementMask)!=0){sum++;} |
| 118 |
value>>>=bits; |
| 119 |
} |
| 120 |
} |
| 121 |
return sum; |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public void setCapacity(long capacity_, int extra){ |
| 126 |
capacity=capacity_; |
| 127 |
length=(int)((capacity+elementsPerCell-1)/elementsPerCell); |
| 128 |
|
| 129 |
if(maxCapacity<capacity){ |
| 130 |
maxLength=length+extra; |
| 131 |
maxCapacity=length*elementsPerCell; |
| 132 |
array=new int[maxLength]; |
| 133 |
} |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
@Override |
| 138 |
public long capacity(){return capacity;} |
| 139 |
|
| 140 |
@Override |
| 141 |
public int length(){return length;} |
| 142 |
|
| 143 |
@Override |
| 144 |
public final int bits(){return bits;} |
| 145 |
|
| 146 |
public int[] array(){return array;} |
| 147 |
|
| 148 |
private long maxCapacity=0; |
| 149 |
private long capacity=0; |
| 150 |
private int maxLength=0; |
| 151 |
private int length=0; |
| 152 |
private int[] array; |
| 153 |
|
| 154 |
public static final int bits=2; |
| 155 |
public static final int elementMask=~((-1)<<bits); |
| 156 |
public static final int elementsPerCell=32/bits; |
| 157 |
public static final int modulus=elementsPerCell-1; |
| 158 |
|
| 159 |
} |