| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.evomodel.treedatalikelihood; |
| 27 |
|
| 28 |
import java.io.Serializable; |
| 29 |
import java.util.*; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class BufferIndexHelper implements Serializable { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public BufferIndexHelper(int maxIndexValue, int minIndexValue) { |
| 45 |
this(maxIndexValue, minIndexValue, 0); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public BufferIndexHelper(int maxIndexValue, int minIndexValue, int bufferSetNumber) { |
| 54 |
this.maxIndexValue = maxIndexValue; |
| 55 |
this.minIndexValue = minIndexValue; |
| 56 |
|
| 57 |
doubleBufferCount = maxIndexValue - minIndexValue; |
| 58 |
indexOffsets = new int[doubleBufferCount]; |
| 59 |
storedIndexOffsets = new int[doubleBufferCount]; |
| 60 |
indexOffsetsFlipped = new boolean[doubleBufferCount]; |
| 61 |
|
| 62 |
this.constantOffset = bufferSetNumber * getBufferCount(); |
| 63 |
} |
| 64 |
|
| 65 |
public int getBufferCount() { |
| 66 |
return 2 * doubleBufferCount + minIndexValue; |
| 67 |
} |
| 68 |
|
| 69 |
public void flipOffset(int i) { |
| 70 |
assert(i >= minIndexValue) : "shouldn't be trying to flip the first 'static' indices"; |
| 71 |
|
| 72 |
if (!indexOffsetsFlipped[i - minIndexValue]){ |
| 73 |
indexOffsets[i - minIndexValue] = doubleBufferCount - indexOffsets[i - minIndexValue]; |
| 74 |
indexOffsetsFlipped[i - minIndexValue] = true; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
public int getOffsetIndex(int i) { |
| 79 |
if (i < minIndexValue) { |
| 80 |
return i + constantOffset; |
| 81 |
} |
| 82 |
return indexOffsets[i - minIndexValue] + i + constantOffset; |
| 83 |
} |
| 84 |
|
| 85 |
public void getIndices(int[] outIndices) { |
| 86 |
for (int i = 0; i < maxIndexValue; i++) { |
| 87 |
outIndices[i] = getOffsetIndex(i); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
public void storeState() { |
| 92 |
Arrays.fill(indexOffsetsFlipped, false); |
| 93 |
System.arraycopy(indexOffsets, 0, storedIndexOffsets, 0, indexOffsets.length); |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
public void restoreState() { |
| 98 |
int[] tmp = storedIndexOffsets; |
| 99 |
storedIndexOffsets = indexOffsets; |
| 100 |
indexOffsets = tmp; |
| 101 |
Arrays.fill(indexOffsetsFlipped, false); |
| 102 |
} |
| 103 |
|
| 104 |
private final int maxIndexValue; |
| 105 |
private final int minIndexValue; |
| 106 |
private final int constantOffset; |
| 107 |
private final int doubleBufferCount; |
| 108 |
private final boolean[] indexOffsetsFlipped; |
| 109 |
|
| 110 |
private int[] indexOffsets; |
| 111 |
private int[] storedIndexOffsets; |
| 112 |
|
| 113 |
} |