Debian Codegraph @

src/dr/evolution/tree/RankedForest.java

raw symbols SCIP
1 /*
2 * RankedForest.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.evolution.tree;
27
28 import java.util.*;
29
30 /**
31 * @author Alexei Drummond
32 */
33 public interface RankedForest {
34
35 public List<RankedNode> getNodes();
36
37 public int getSize();
38
39 public int rank();
40
41 public boolean compatibleRank(List<BitSet> constraints);
42
43 public Set<BitSet> clades();
44
45 public int constraintsSatisfied();
46
47 public boolean isClear();
48
49
50 public class Default implements RankedForest {
51
52 List<RankedNode> nodes;
53 boolean clear;
54
55 public Default(int size, boolean clear) {
56 nodes = new ArrayList<RankedNode>(size);
57 for (int i = size - 1; i >= 0; i--) {
58 nodes.add(new RankedNode(i, size));
59 }
60 this.clear = clear;
61 }
62
63 public int getSize() {
64 return nodes.get(0).n;
65 }
66
67 public int rank() {
68 return nodes.get(0).rank;
69 }
70
71 public boolean compatibleRank(List<BitSet> constraints) {
72 return true;
73 }
74
75 public Set<BitSet> clades() {
76 return Collections.emptySet();
77
78 }
79
80 public int constraintsSatisfied() {
81 return 0;
82 }
83
84 public boolean isClear() {
85 return clear;
86 }
87
88 public List<RankedNode> getNodes() {
89 return nodes;
90 }
91 }
92
93 public class Parent implements RankedForest {
94
95 RankedForest child;
96 RankedNode parent;
97 List<RankedNode> nodes;
98 HashSet<BitSet> clades;
99 int constraintsSatisfied = 0;
100 boolean clear;
101
102 public Parent(RankedForest child, RankedNode parent, List<BitSet> constraints) {
103 this.child = child;
104 this.parent = parent;
105 nodes = new ArrayList<RankedNode>();
106 nodes.add(parent);
107 nodes.addAll(child.getNodes());
108 nodes.remove(parent.child1);
109 nodes.remove(parent.child2);
110
111 clades = new HashSet<BitSet>();
112 clades.addAll(child.clades());
113 clades.add(parent.cladeBits);
114
115 if (!clear) {
116 constraintsSatisfied = child.constraintsSatisfied();
117 //System.out.println(parent.cladeBits);
118 if (constraints.contains(parent.cladeBits)) {
119 //System.out.println("Found " + parent.cladeBits + " in constraints!");
120 constraintsSatisfied += 1;
121 }
122 clear = (constraintsSatisfied == constraints.size());
123 }
124
125 //System.out.println("making parent wih rank " + parent.rank);
126 }
127
128 public List<RankedNode> getNodes() {
129 return nodes;
130 }
131
132 public int getSize() {
133 return nodes.get(0).n;
134 }
135
136 public int rank() {
137 return nodes.get(0).rank;
138 }
139
140 public boolean compatibleRank(List<BitSet> constraints) {
141
142 if (constraints.size() == 1) return true;
143
144 int rank = 0;
145 for (BitSet constraint : constraints) {
146 int newRank = getRank(constraint);
147 if (newRank < rank) return false;
148 rank = newRank;
149 }
150 if (rank != Integer.MAX_VALUE) clear = true;
151 return true;
152 }
153
154 public Set<BitSet> clades() {
155 return clades;
156 }
157
158 public int constraintsSatisfied() {
159 return constraintsSatisfied;
160 }
161
162 public boolean isClear() {
163 return clear;
164 }
165
166 private int getRank(BitSet constraint) {
167 //if (constraint.cardinality() > parent.rank) return Integer.MAX_VALUE;
168
169 if (parent.cladeBits.equals(constraint)) {
170 return parent.rank;
171 } else {
172 if (child instanceof Parent) {
173 return ((Parent) child).getRank(constraint);
174 }
175 return Integer.MAX_VALUE;
176 }
177 }
178
179 //public String toString() {}
180 }
181 }