Debian Codegraph @

src/dr/evomodel/speciation/CalibrationLineagesIterator.java

raw symbols SCIP
1 /*
2 * CalibrationLineagesIterator.java
3 *
4 * Copyright (c) 2002-2015 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.speciation;
27
28 /**
29 * Created by IntelliJ IDEA.
30 * User: joseph
31 * Date: 22/06/11
32 * Time: 12:39 PM
33 * To change this template use File | Settings | File Templates.
34 */
35 public class CalibrationLineagesIterator {
36 final int[][] taxaPartialOrder;
37 final int[] cladesFreeLins;
38 private final LinsIterator[] iters;
39 private int nCurIters;
40 private int[][] vals;
41 private int nFreeLineages;
42 private final int[] maximalClades;
43
44 CalibrationLineagesIterator(int[][] clades, int[][] taxaPartialOrder, boolean[] maximal, int externalNodeCount) {
45 cladesFreeLins = new int[clades.length];
46 for(int k = 0; k < cladesFreeLins.length; ++k) {
47 cladesFreeLins[k] = clades[k].length;
48 for( int l : taxaPartialOrder[k] ) {
49 cladesFreeLins[k] -= clades[l].length;
50 }
51 assert cladesFreeLins[k] >= 0;
52 }
53
54 this.taxaPartialOrder = taxaPartialOrder;
55 iters = new LinsIterator[clades.length+1];
56 vals = new int[iters.length][];
57
58 int nMax = 0;
59 for(boolean b : maximal) {
60 nMax += b ? 1 : 0;
61 }
62 maximalClades = new int[nMax];
63 nFreeLineages = externalNodeCount;
64
65 nMax = 0;
66 for(int m = 0; m < maximal.length; ++m) {
67 if( maximal[m] ) {
68 maximalClades[nMax] = m;
69 ++nMax;
70 nFreeLineages -= clades[m].length;
71 }
72 }
73
74 assert nFreeLineages >= 0;
75 }
76
77 int setup(int[] ranks) {
78 int n = cladesFreeLins.length;
79
80 nCurIters = 0;
81
82 for(int k = 0; k < n; ++k) {
83 setOneIterator(ranks, taxaPartialOrder[k], cladesFreeLins[k], ranks[k]);
84 }
85
86 if( nFreeLineages > 0 ) {
87 setOneIterator(ranks, maximalClades, nFreeLineages, n+1);
88 }
89
90 for(int k = 0; k < nCurIters-1; ++k) {
91 vals[k] = iters[k].next();
92 }
93
94 return nCurIters;
95 }
96
97 private void setOneIterator(int[] ranks, int[] joinerClades, int nl, int rank) {
98 int nSubs = joinerClades.length;
99
100 LinsIterator itr = null;
101 if( nSubs == 0 ) {
102 itr = new LinsIterator(nl, rank, null);
103 } else /*if( nl > 0 || nSubs > 2 ) */ {
104 final int[] s = new int[nSubs];
105 for(int i = 0; i < nSubs; ++i) {
106 s[i] = ranks[joinerClades[i]];
107 }
108 itr = new LinsIterator(nl, rank, s);
109 }
110
111 if( itr != null ) {
112 // sorted according to rank
113 iters[itr.rank-1] = itr;
114 itr.startIter();
115 ++nCurIters;
116 }
117 }
118
119 int[][] next()
120 {
121 int[] l = iters[nCurIters-1].next();
122
123 if( l != null ) {
124 vals[nCurIters-1] = l;
125 return vals;
126 }
127
128 int i = nCurIters-2;
129 for( ; i >= 0; --i) {
130 if( (vals[i] = iters[i].next()) != null) {
131 break;
132 }
133 }
134
135 if( i < 0 ) {
136 return null;
137 }
138
139 ++i;
140
141 for( ; i < nCurIters; ++i) {
142 iters[i].startIter();
143 vals[i] = iters[i].next();
144 }
145
146 return vals;
147 }
148
149 public int[][] allJoiners() {
150 int[][] joiners = new int[nCurIters][];
151
152 for(int i = 0; i < nCurIters; ++i) {
153 joiners[i] = iters[i].ljoins();
154 }
155 return joiners;
156 }
157
158 public int nStart(int i) {
159 return iters[i].nStart;
160 }
161
162 class LinsIterator {
163
164 private final int rank;
165 private final int nStart;
166 private final int[] joiners;
167 private final int[] aStart;
168 private final int[] lins;
169 private int lastJoinger;
170 private boolean stopIter;
171
172 LinsIterator(int ns, int r, int[] jnr) {
173 rank = r;
174 nStart = ns;
175 joiners = new int [r];
176
177 lastJoinger = -1;
178
179 // 2 for start+end, rank-1 intermediate levels
180 aStart = new int [2 + rank-1];
181 lins = new int [2 + rank-1];
182
183 for(int k = 0; k < rank; ++k) {
184 joiners[k] = 0;
185 }
186
187 if( jnr != null ) {
188 for (int j : jnr) {
189 joiners[j] = 1;
190 if (lastJoinger < j) {
191 lastJoinger = j;
192 }
193 }
194 }
195 aStart[0] = ns;
196
197 if( lastJoinger <= 0 ) {
198 for(int i = 1; i < rank+1; ++i) {
199 aStart[i] = 2;
200 }
201 if( rank > 1) {
202 // first iteration increments this
203 aStart[rank-1] -= 1;
204 }
205 } else {
206 //assert(rank > 1);
207
208 if( nStart > 0 ) {
209 int i = 1;
210 for(; i < lastJoinger+1; ++i) {
211 aStart[i] = 1;
212 }
213 for(; i < rank+1; ++i) {
214 aStart[i] = 2;
215 }
216 } else {
217 int mj = jnr[0];
218 for(int k = 0; k < jnr.length; ++k) {
219 mj = Math.min(mj, jnr[k]);
220 }
221 int i = 1;
222 for(; i < mj+1; ++i) {
223 aStart[i] = 0;
224 }
225 for(; i < lastJoinger+1; ++i) {
226 aStart[i] = 1;
227 }
228 for(; i < rank+1; ++i) {
229 aStart[i] = 2;
230 }
231 }
232 // first iteration increments this
233 aStart[rank-1] -= 1;
234 }
235
236 }
237
238 void startIter() {
239 for(int i = 0; i < rank+1; ++i) {
240 lins[i] = aStart[i];
241 }
242 stopIter = false;
243 }
244
245 final int[] next()
246 {
247 int i = rank - 1;
248 if( lastJoinger <= 0 ) {
249 while( i >= 1 && lins[i] == lins[i-1]) {
250 --i;
251 }
252 if( i == 0 ) {
253 if( rank == 1 ) {
254 if( !stopIter ) {
255 stopIter = true;
256 return lins;
257 }
258 }
259 return null;
260 }
261 lins[i] += 1;
262 ++i;
263 while( i < rank ) {
264 lins[i] = 2;
265 ++i;
266 }
267 } else {
268
269 while( i >= 1 && lins[i] == lins[i-1] + joiners[i-1] ) {
270 --i;
271 }
272 if( i == 0 ) {
273 return null;
274 }
275 lins[i] += 1;
276 i++;
277 while( i < rank ) {
278 lins[i] = (i <= lastJoinger) ? 1 : 2;
279 i++;
280 }
281 }
282 return lins;
283 }
284
285 final int[] ljoins() {
286 return joiners;
287 }
288 }
289 }
290
291