-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathAbstractClusterCollection.java
208 lines (172 loc) · 4.87 KB
/
AbstractClusterCollection.java
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package phylonet.coalescent;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
import phylonet.tree.model.sti.STITreeCluster;
import phylonet.tree.model.sti.STITreeCluster.Vertex;
public abstract class AbstractClusterCollection implements IClusterCollection, Cloneable {
protected ArrayList<Set<Vertex>> clusters;
protected int topClusterLength;
int totalcount = 0;
protected void initialize(int len) {
this.topClusterLength = len;
clusters = new ArrayList<Set<Vertex>>(len);
for (int i = 0; i <= len; i++) {
clusters.add(new HashSet<Vertex>());
}
}
@Override
public Vertex getTopVertex() {
Iterator<Vertex> it = clusters.get(topClusterLength).iterator();
if (! it.hasNext()) {
throw new NoSuchElementException();
}
return it.next();
}
@Override
public int getClusterCount() {
return totalcount;
}
@Override
public boolean addCluster(Vertex vertex, int size) {
boolean added = clusters.get(size).add(vertex);
if (added) {
totalcount++;
}
return added;
}
@Override
public boolean removeCluster(Vertex vertex, int size) {
boolean removed = clusters.get(size).remove(vertex);
if(removed){
totalcount--;
}
return removed;
}
@Override
public boolean contains(Vertex vertex) {
return clusters.get(vertex.getCluster().getClusterSize()).contains(vertex);
}
@Override
public IClusterCollection getContainedClusters(Vertex v) {
STITreeCluster cluster = v.getCluster();
int size = cluster.getClusterSize();
AbstractClusterCollection ret = newInstance(size);
addClusterToRet(v, size, ret);
for (int i = size - 1 ; i > 0; i--) {
Set<Vertex> sizeClusters = clusters.get(i);
if (sizeClusters == null) continue;
for (Vertex vertex : sizeClusters) {
if (cluster.containsCluster(vertex.getCluster())) {
addClusterToRet(vertex, i, ret);
}
}
}
return ret;
}
protected void addClusterToRet(Vertex vertex, int size, IClusterCollection ret) {
ret.addCluster(vertex, size);
}
@Override
public Iterable<VertexPair> getClusterResolutions() {
//System.out.println(topClusterLength+ " "+getTopVertex());
//TODO: return an iterator directly instead of building a collection.
ArrayList<VertexPair> ret = new ArrayList<VertexPair>();
/*Iterable<VertexPair> r= new Iterable<IClusterCollection.VertexPair>() {
@Override
public Iterator<VertexPair> iterator() {
return new Iterator<VertexPair>() {
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return false;
}
@Override
public VertexPair next() {
// TODO Auto-generated method stub
return null;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};*/
int clusterSize = topClusterLength;
Vertex v = this.getTopVertex();
for (int i = 1; i <= (clusterSize / 2); i++) {
Set<Vertex> left = this.clusters.get(i);
if (left == null || left.size() == 0) {
continue;
}
Set<Vertex> right = this.clusters.get(clusterSize - i);
if (right == null || right.size() == 0) {
continue;
}
for (Vertex smallV : left) {
for (Vertex bigv : right) {
if (!smallV.getCluster().isDisjoint(bigv.getCluster())) {
continue;
}
VertexPair bi = new VertexPair(
smallV, bigv, v);
ret.add(bi);
}
}
}
return ret;
}
public abstract AbstractClusterCollection newInstance(int size);
@Override
public Set<Vertex> getSubClusters(int size) {
return this.clusters.get(size);
}
@Override
public Iterable<Set<Vertex>> getSubClusters() {
return new Iterable<Set<Vertex>>() {
@Override
public Iterator<Set<Vertex>> iterator() {
return new Iterator<Set<Vertex>>() {
int i = topClusterLength - 1;
int next = topClusterLength ;
@Override
public boolean hasNext() {
if (next > i) {
next = i;
while (next>0 && (clusters.get(next) == null || clusters.get(next).size() == 0)) next--;
}
return next>0;
}
@Override
public Set<Vertex> next() {
if (! hasNext()) throw new NoSuchElementException();
i = next;
Set<Vertex> ret = clusters.get(i);
i--;
return ret;
}
@Override
public void remove() {
throw new ConcurrentModificationException();
}
};
}
};
}
public AbstractClusterCollection clone() throws CloneNotSupportedException {
AbstractClusterCollection clone = (AbstractClusterCollection) super.clone();
clone.clusters = new ArrayList<Set<Vertex>>();
for (Set<Vertex> vset : this.clusters) {
HashSet<Vertex> nset = new HashSet<STITreeCluster.Vertex>();
clone.clusters.add(nset);
for (Vertex v: vset) {
nset.add(v.getCluster().new Vertex());
}
}
return clone;
}
}