cdbe976ae06e969ea54954940a2c4f7dbf717cf7
[sbp.git] / src / edu / berkeley / sbp / util / TopologicalBag.java
1 package edu.berkeley.sbp.util;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import java.io.*;
5 import java.util.*;
6 import java.lang.reflect.*;
7 import java.lang.ref.*;
8
9 //
10 // This is a fairly crude implementation; if things need to be fast,
11 // you should implement a special-purpose class
12 //
13
14 /** a mapping from topologies over <tt>K</tt> to <i>sets of</i> values of type <tt>V</tt> */
15 public class TopologicalBag<K,V> implements MapBag<Topology<K>,V> {
16
17     // CRUCIAL INVARIANT: keys in this hashmap MUST be disjoint or the universe will implode
18     private final HashMap<Topology<K>,HashSet<V>> h = new HashMap<Topology<K>,HashSet<V>>();
19
20     public Iterator<Topology<K>> iterator() { return h.keySet().iterator(); }
21
22     public void addAll(TopologicalBag<K,V> tb) {
23         for(Topology<K> k : tb)
24             addAll(k, tb.getAll(k));
25     }
26
27     public void add(Topology<K> t, V v) { put(t, v); }
28     public void addAll(Topology<K> k, Iterable<V> vi) { putAll(k, vi); }
29
30     public void putAll(Topology<K> k, Iterable<V> vi) { if (vi!=null) for(V v : vi) put(k, v); }
31     public void put(Topology<K> t, V v) {
32         for(Topology<K> ht : h.keySet()) {
33             if (t.disjoint(ht)) continue;
34             if (t.containsAll(ht) && ht.containsAll(t)) {
35                 h.get(ht).add(v);
36                 return;
37             }
38             if (ht.containsAll(t)) {
39                 HashSet<V> v0 = h.get(ht);
40                 HashSet<V> v1 = new HashSet<V>();
41                 v1.addAll(v0);
42                 h.remove(ht);
43                 v1.add(v);
44                 h.put(ht.intersect(t), v1);
45                 h.put(ht.minus(t), v0);
46                 return;
47             }
48             put(t.intersect(ht), v);
49             put(t.minus(ht), v);
50             return;
51         }
52         HashSet<V> v1 = new HashSet<V>();
53         v1.add(v);
54         h.put(t, v1);
55     }
56
57
58     public TopologicalBag<K,V> subset(Topology<K> t) {
59         TopologicalBag<K,V> ret = new TopologicalBag<K,V>();
60         for(Topology<K> ht : h.keySet()) {
61             if (ht.disjoint(t)) continue;
62             Iterable<V> it = h.get(ht);
63             Topology<K> tk = ht.intersect(t);
64             ret.putAll(tk, it);
65         }
66         return ret;
67     }
68
69     public Map<Topology<K>,HashSet<V>> gett(Topology<K> t) {
70         HashMap<Topology<K>,HashSet<V>> ret = new HashMap<Topology<K>,HashSet<V>>();
71         for(Topology<K> ht : h.keySet()) {
72             if (ht.disjoint(t)) continue;
73             ret.put(ht.intersect(t), h.get(ht));
74         }
75         return ret;
76     }
77
78     public boolean empty(K k) {
79         for(Topology<K> t : h.keySet())
80             if (t.contains(k) && h.get(t).size() > 0)
81                 return false;
82         return true;
83     }
84
85     public boolean contains(K k) { return get(k).iterator().hasNext(); }
86
87     public boolean contains(K k, V v) {
88         for(Topology<K> t : h.keySet())
89             if (t.contains(k))
90                 return h.get(t).contains(v);
91         return false;
92     }
93
94     public boolean has(K k) {
95         for(Topology<K> t : h.keySet())
96             if (t.contains(k) && !h.get(t).isEmpty())
97                 return true;
98         return false;
99     }
100
101     private HashMap<K,Iterable<V>> cache = new HashMap<K,Iterable<V>>();
102     public Iterable<V> getAll(Topology<K> k) { return h.get(k); }
103     public Iterable<V> get(K k) {
104         Iterable<V> c = cache.get(k);
105         if (c != null) return c;
106         HashSet<V> ret = null;
107         HashSet<V> ret0 = null;
108         for(Topology<K> t : h.keySet())
109             if (t.contains(k)) {
110                 if (ret==null) {
111                     ret0 = ret = h.get(t);
112                 } else {
113                     if (ret0 != null) {
114                         ret = new HashSet<V>();
115                         ret.addAll(ret0);
116                         ret0 = null;
117                     }
118                     ret.addAll(h.get(t));
119                 }
120             }
121         if (ret==null) {
122             cache.put(k, emptyIterator);
123             return emptyIterator;
124         } else {
125             cache.put(k, new FastSet<V>(ret));
126             return ret;
127         }
128     }
129     private final Iterable<V> emptyIterator = new EmptyIterator<V>();
130 }