ArrayList<TIntHashSet> layer = new ArrayList<TIntHashSet>();
TIntHashSet[] tmpQ = new TIntHashSet[totalSizes];
// DLList[vars.length+1];
for (i = 0; i <= n; i++) {
layer.add(new TIntHashSet());// = new DLList(nbNodes);
}
//forward pass, construct all paths described by the automaton for word of length nbVars.
layer.get(0).add(pi.getInitialState());
TIntHashSet nexts = new TIntHashSet();
for (i = 0; i < n; i++) {
int UB = vs[i].getUB();
for (j = vs[i].getLB(); j <= UB; j = vs[i].nextValue(j)) {
layerIter = layer.get(i).iterator();//getIterator();
while (layerIter.hasNext()) {
k = layerIter.next();
nexts.clear();
pi.delta(k, j, nexts);
TIntIterator it = nexts.iterator();
for (; it.hasNext(); ) {
int succ = it.next();
layer.get(i + 1).add(succ);
}
if (!nexts.isEmpty()) {
int idx = starts[i] + j - offsets[i];
if (tmpQ[idx] == null)
tmpQ[idx] = new TIntHashSet();
tmpQ[idx].add(k);
}
}
}
}
//removing reachable non accepting states
layerIter = layer.get(n).iterator();
while (layerIter.hasNext()) {
k = layerIter.next();
if (!pi.isFinal(k)) {
layerIter.remove();
}
}
//backward pass, removing arcs that does not lead to an accepting state
int nbNodes = pi.getNbStates();
BitSet mark = new BitSet(nbNodes);
Node[] in = new Node[pi.getNbStates() * (n + 1)];
Node tink = new Node(pi.getNbStates() + 1, n + 1, nid++);
graph.addVertex(tink);
for (i = n - 1; i >= 0; i--) {
mark.clear(0, nbNodes);
int UB = vs[i].getUB();
for (j = vs[i].getLB(); j <= UB; j = vs[i].nextValue(j)) {
int idx = starts[i] + j - offsets[i];
TIntHashSet l = tmpQ[idx];
if (l != null) {
qijIter = l.iterator();
while (qijIter.hasNext()) {
k = qijIter.next();
nexts.clear();
pi.delta(k, j, nexts);
if (nexts.size() > 1)
System.err.println("STOP");
boolean added = false;
for (TIntIterator it = nexts.iterator(); it.hasNext(); ) {
int qn = it.next();
if (layer.get(i + 1).contains(qn)) {
added = true;
Node a = in[i * pi.getNbStates() + k];
if (a == null) {
a = new Node(k, i, nid++);
in[i * pi.getNbStates() + k] = a;
graph.addVertex(a);
}
Node b = in[(i + 1) * pi.getNbStates() + qn];
if (b == null) {
b = new Node(qn, i + 1, nid++);
in[(i + 1) * pi.getNbStates() + qn] = b;
graph.addVertex(b);
}
Arc arc = new Arc(a, b, j, aid++);
graph.addEdge(a, b, arc);
tmp.get(idx).add(arc);
mark.set(k);
}
}
if (!added)
qijIter.remove();
}
}
}
layerIter = layer.get(i).iterator();
// If no more arcs go out of a given state in the layer, then we remove the state from that layer
while (layerIter.hasNext())
if (!mark.get(layerIter.next()))
layerIter.remove();
}
TIntHashSet th = new TIntHashSet();
int[][] intLayer = new int[n + 2][];
for (k = 0; k < pi.getNbStates(); k++) {
Node o = in[n * pi.getNbStates() + k];
{
if (o != null) {
Arc a = new Arc(o, tink, 0, aid++);
graph.addEdge(o, tink, a);
}
}
}
for (i = 0; i <= n; i++) {
th.clear();
for (k = 0; k < pi.getNbStates(); k++) {
Node o = in[i * pi.getNbStates() + k];
if (o != null) {
th.add(o.id);
}
}
intLayer[i] = th.toArray();
}
intLayer[n + 1] = new int[]{tink.id};
if (intLayer[0].length > 0) {
IEnvironment environment = solver.getEnvironment();