//
/** a mapping from topologies over <tt>K</tt> to <i>sets of</i> values of type <tt>V</tt> */
-public class TopologicalBag<K,V> implements MapBag<Topology<K>,V> {
+public class TopologicalBag<K,V> implements MapBag<Topology<K>,V>, VisitableMap<K,V> {
// CRUCIAL INVARIANT: keys in this hashmap MUST be disjoint or the universe will implode
private final HashMap<Topology<K>,HashSet<V>> h = new HashMap<Topology<K>,HashSet<V>>();
return ret;
}
}
+
+ public VisitableMap<K,V> optimize() {
+ ArrayList<Long> min_ = new ArrayList<Long>();
+ ArrayList<Long> max_ = new ArrayList<Long>();
+ ArrayList<Object[]> v_ = new ArrayList<Object[]>();
+ for(Topology<K> t : h.keySet()) {
+ ArrayList<V> al = new ArrayList<V>();
+ for(V vv : h.get(t)) al.add(vv);
+ Object[] vs = new Object[al.size()];
+ al.toArray(vs);
+ IntegerTopology it = (IntegerTopology)t;
+ for(Range r : it.getRanges()) {
+ min_.add(r.isMinNegInf() ? Long.MIN_VALUE : r.getMin());
+ max_.add(r.isMaxPosInf() ? Long.MAX_VALUE : r.getMax());
+ v_.add(vs);
+ }
+ }
+ final int size = v_.size();
+ final long[] min = new long[size]; for(int i=0; i<min.length; i++) min[i]=min_.get(i);
+ final long[] max = new long[size]; for(int i=0; i<max.length; i++) max[i]=max_.get(i);
+ final Object[][] v = new Object[size][]; v_.toArray(v);
+ return new VisitableMap<K,V>() {
+ public boolean contains(K k) {
+ IntegerMappable im = (IntegerMappable)k;
+ int asint = im.toInt();
+ for(int i=0; i<size; i++)
+ if (min[i] <= asint && max[i] >= asint && v[i].length > 0)
+ return true;
+ return false;
+ }
+ public <B,C> void invoke(K k, Invokable<V,B,C> ivbc, B b, C c) {
+ IntegerMappable im = (IntegerMappable)k;
+ int asint = im.toInt();
+ for(int i=0; i<size; i++) {
+ if (min[i] <= asint && max[i] >= asint) {
+ Object[] arr = v[i];
+ for(int j=0; j<arr.length; j++)
+ ivbc.invoke((V)arr[j], b, c);
+ }
+ }
+ }
+ };
+ }
+
private final Iterable<V> emptyIterator = new EmptyIterator<V>();
}