| 1 |
package structures; |
| 2 |
import java.util.concurrent.atomic.AtomicIntegerArray; |
| 3 |
|
| 4 |
import shared.KillSwitch; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
public class CoverageArray3A extends CoverageArray { |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
private static final long serialVersionUID = 98483952072098494L; |
| 18 |
|
| 19 |
public static void main(String[] args){ |
| 20 |
|
| 21 |
} |
| 22 |
|
| 23 |
public CoverageArray3A(int chrom, int len){ |
| 24 |
super(chrom, len); |
| 25 |
array=KillSwitch.allocAtomicInt(len); |
| 26 |
minIndex=0; |
| 27 |
maxIndex=len-1; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
@Override |
| 34 |
public void increment(int loc){ |
| 35 |
increment(loc, 1); |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public void increment(int loc, int amt) { |
| 40 |
int val=array.addAndGet(loc, (int)amt); |
| 41 |
|
| 42 |
if(val<0 && amt>0){ |
| 43 |
if(!OVERFLOWED){ |
| 44 |
System.err.println("Note: Coverage capped at "+Integer.MAX_VALUE); |
| 45 |
OVERFLOWED=true; |
| 46 |
} |
| 47 |
array.set(loc, Integer.MAX_VALUE); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
public void incrementRangeSynchronized(int min, int max, int amt) { |
| 53 |
incrementRange(min, max, amt); |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
public void incrementRange(int min, int max, int amt){ |
| 58 |
if(min<0){min=0;} |
| 59 |
if(max>maxIndex){max=maxIndex;} |
| 60 |
boolean over=false; |
| 61 |
for(int loc=min; loc<=max; loc++){ |
| 62 |
int val=array.addAndGet(loc, (int)amt); |
| 63 |
if(val<0 && amt>0){ |
| 64 |
over=true; |
| 65 |
array.set(loc, Integer.MAX_VALUE); |
| 66 |
} |
| 67 |
} |
| 68 |
if(over && !OVERFLOWED){ |
| 69 |
synchronized(CoverageArray3A.class){ |
| 70 |
if(!OVERFLOWED){ |
| 71 |
System.err.println("Note: Coverage capped at "+Integer.MAX_VALUE); |
| 72 |
OVERFLOWED=true; |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public void set(int loc, int val){ |
| 80 |
|
| 81 |
array.set(loc, val); |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public int get(int loc){ |
| 86 |
return loc<0 || loc>=array.length() ? 0 : array.get(loc); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public void resize(int newlen){ |
| 91 |
throw new RuntimeException("Resize: Unsupported."); |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public String toString(){ |
| 96 |
StringBuilder sb=new StringBuilder(); |
| 97 |
sb.append('['); |
| 98 |
for(int i=0; i<=length(); i++){ |
| 99 |
if(i>0){sb.append(", ");} |
| 100 |
sb.append((int)array.get(i)); |
| 101 |
} |
| 102 |
sb.append(']'); |
| 103 |
return sb.toString(); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public int[] toArray() { |
| 108 |
int[] array2=new int[length()]; |
| 109 |
for(int i=0; i<array2.length; i++) { |
| 110 |
array2[i]=get(i); |
| 111 |
} |
| 112 |
return array2; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
public final AtomicIntegerArray array; |
| 117 |
|
| 118 |
|
| 119 |
@Override |
| 120 |
public int arrayLength(){return array.length();} |
| 121 |
|
| 122 |
private static boolean OVERFLOWED=false; |
| 123 |
|
| 124 |
} |