@Override public void addReaction(final Reaction<T> r) { tree.add(r); times.add(r.getTau()); final int index = tree.size() - 1; indexes.put(r, index); updateEffectively(r, index); }
private boolean up(final Reaction<T> r, final int i) { int index = i; int parentIndex = getParent(index); final Time newTime = r.getTau(); if (parentIndex == -1) { return false; } else { Reaction<T> parent = tree.get(parentIndex); if (newTime.compareTo(times.get(parentIndex)) >= 0) { return false; } else { do { swap(index, r, parentIndex, parent); index = parentIndex; parentIndex = getParent(index); if (parentIndex == -1) { return true; } parent = tree.get(parentIndex); } while (newTime.compareTo(times.get(parentIndex)) < 0); return true; } } }
/** * Builds a simulation for a given environment. By default it uses a * DependencyGraph and an IndexedPriorityQueue internally. If you want to * use your own implementations of {@link DependencyGraph} and * {@link ReactionManager} interfaces, don't use this constructor. * * @param e * the environment at the initial time * @param maxSteps * the maximum number of steps to do * @param t * the maximum time to reach */ public Engine(final Environment<T> e, final long maxSteps, final Time t) { L.trace("Engine created"); env = e; env.setSimulation(this); dg = new MapBasedDependencyGraph<T>(env, handlers); ipq = new ArrayIndexedPriorityQueue<>(); this.steps = maxSteps; this.finalTime = t; }
private void down(final Reaction<T> r, final int i) { int index = i; final Time newTime = r.getTau(); do { int minIndex = 2 * index + 1; if (minIndex > tree.size() - 1) { return; } Time minTime = times.get(minIndex); Reaction<T> min = tree.get(minIndex); final int right = minIndex + 1; if (right < tree.size()) { final Time rr = times.get(right); if (rr.compareTo(minTime) < 0) { min = tree.get(right); minIndex = right; minTime = rr; } } if (newTime.compareTo(minTime) > 0) { swap(index, r, minIndex, min); index = minIndex; } else { return; } } while (true); }
@Override public void updateReaction(final Reaction<T> r) { final int index = indexes.get(r); if (index != indexes.getNoEntryValue()) { times.set(index, r.getTau()); updateEffectively(r, index); } }
@Override public void removeReaction(final Reaction<T> r) { final int index = indexes.get(r); final int last = tree.size() - 1; if (index == last) { tree.remove(index); indexes.remove(r); times.remove(index); } else { final Reaction<T> swapped = tree.get(last); indexes.put(swapped, index); tree.set(index, swapped); times.set(index, swapped.getTau()); tree.remove(last); times.remove(last); indexes.remove(r); updateEffectively(swapped, index); } }