Debian Codegraph @

src/dr/evomodel/treedatalikelihood/BufferIndexHelper.java

raw symbols SCIP
1 /*
2 * BufferIndexHelper.java
3 *
4 * Copyright (c) 2002-2016 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evomodel.treedatalikelihood;
27
28 import java.io.Serializable;
29 import java.util.*;
30
31 /**
32 * BufferIndexHelper - helper for double buffering of intermediate computation at nodes.
33 *
34 * @author Andrew Rambaut
35 * @author Marc Suchard
36 * @version $Id$
37 */
38 public class BufferIndexHelper implements Serializable {
39
40 /**
41 * @param maxIndexValue the number of possible input values for the index
42 * @param minIndexValue the minimum index value to have the mirrored buffers
43 */
44 public BufferIndexHelper(int maxIndexValue, int minIndexValue) {
45 this(maxIndexValue, minIndexValue, 0);
46 }
47
48 /**
49 * @param maxIndexValue the number of possible input values for the index
50 * @param minIndexValue the minimum index value to have the mirrored buffers
51 * @param bufferSetNumber provides a total offset of bufferSetNumber * bufferCount
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]){ // only flip once before reject / accept
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 }//END: class