//
/** 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>>();
public void add(Topology<K> t, V v) { put(t, v); }
public void addAll(Topology<K> k, Iterable<V> vi) { putAll(k, vi); }
- public void putAll(Topology<K> k, Iterable<V> vi) { for(V v : vi) put(k, v); }
+ public void putAll(Topology<K> k, Iterable<V> vi) { if (vi!=null) for(V v : vi) put(k, v); }
public void put(Topology<K> t, V v) {
for(Topology<K> ht : h.keySet()) {
if (t.disjoint(ht)) continue;
- if (t.equals(ht)) {
+ if (t.containsAll(ht) && ht.containsAll(t)) {
h.get(ht).add(v);
return;
}
TopologicalBag<K,V> ret = new TopologicalBag<K,V>();
for(Topology<K> ht : h.keySet()) {
if (ht.disjoint(t)) continue;
- ret.putAll(ht.intersect(t), h.get(ht));
+ Iterable<V> it = h.get(ht);
+ Topology<K> tk = ht.intersect(t);
+ ret.putAll(tk, it);
}
return ret;
}
return true;
}
- public boolean contains(K k) { return get(k).iterator().hasNext(); }
+ public boolean contains(K k) { return has(k); }
public boolean contains(K k, V v) {
for(Topology<K> t : h.keySet())
return false;
}
+ public boolean has(K k) {
+ for(Topology<K> t : h.keySet())
+ if (t.contains(k) && !h.get(t).isEmpty())
+ return true;
+ return false;
+ }
+
private HashMap<K,Iterable<V>> cache = new HashMap<K,Iterable<V>>();
public Iterable<V> getAll(Topology<K> k) { return h.get(k); }
+
+ public <B,C> void invoke(K k, Invokable<V,B,C> ivbc, B b, C c) {
+ for(Topology<K> t : h.keySet())
+ if (t.contains(k))
+ for(V v : h.get(t))
+ ivbc.invoke(v, b, c);
+ }
+
public Iterable<V> get(K k) {
Iterable<V> c = cache.get(k);
if (c != null) return c;