-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiMap.java
executable file
·118 lines (114 loc) · 3.95 KB
/
MultiMap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**************************************************************************
* @author Mahdi Eslamimehr
* @version 0.1
***************************************************************************/
package ContractGen;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.Iterator;
import java.util.Map;
public class MultiMap {
private HashMap _hash;
public MultiMap() {
_hash = new HashMap();
}
public MultiMap (MultiMap m) {
_hash = new HashMap();
_hash.putAll(m._hash);
}
public Set entrySet() {
return _hash.entrySet();
}
/****************************************************
* put(x, y): puts y into an existing set of values
* for the key 'x'
**************************************************/
public void put (Object key, Object value) {
HashSet values = null;
if (_hash.containsKey(key))
values = (HashSet) _hash.get(key);
if (values==null) values = new HashSet();
if (value!=null) values.add(value);
_hash.put(key,values);
}
/*****************************************************
* putMap(o): Copies the entire multimap o into this.
***************************************************/
public void putMap(MultiMap o) {
_hash.putAll(o._hash);
}
/*****************************************************
* merge(MultiMap o): Merges using mergeMap().
* For all keys in 'this', add all values for corr.
* keys in o to the respective keys.
***************************************************/
public void merge(MultiMap o) {
for (Iterator it = o._hash.keySet().iterator(); it.hasNext(); ) {
Object hisKey = it.next();
Object hisVals = o._hash.get(hisKey);
mergeMap(hisKey, hisVals);
}
}
/******************************************************
* Returns the set of all values mapped to given key.
****************************************************/
public HashSet get(Object key) {
return (HashSet) _hash.get(key);
}
/********************************************************
* Add all values from newValues into the set of values
* associated with key.
******************************************************/
public void mergeMap (Object key, Object newValues) {
// System.out.println("Called mergeMap");
HashSet values = null;
if (_hash.containsKey(key)) {
values = (HashSet) _hash.get(key);
if (values==null) values = new HashSet();
} else values = new HashSet();
if (newValues!=null) values.addAll((HashSet) newValues);
_hash.put (key, values);
}
/********************************************************
* Add new mapping [key, values] to multi-map.
******************************************************/
public void putAll(Object key, Object values) {
_hash.put(key, values);
}
public boolean equals(Object o) {
MultiMap m = (MultiMap) o;
return (_hash.equals(m._hash));
}
public int hashCode() {
return _hash.hashCode();
}
public Set keySet() {
return (Set) _hash.keySet();
}
public boolean containsKey(Object x) {
return _hash.containsKey(x);
}
public int size() {
return _hash.size();
}
public boolean existsEdge(Object n1, Object n2) {
return (containsKey(n1) && get(n1).contains(n2));
}
public boolean isEmpty() {
return _hash.isEmpty();
}
public void clear() {
_hash.clear();
}
public String toString() {
return _hash.toString();
}
public void remove(Object key, Object value) {
if (!_hash.containsKey(key)) return;
HashSet vals = (HashSet) _hash.get(key);
vals.remove(value);
if (vals.isEmpty()) _hash.remove(key);
else putAll(key, vals);
}
}