My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package annas.graph;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

/**
* Base graph class
*
* @author Sam Wilson
*
* @param <N>
* Node type
* @param <A>
* Arc type
*/
public abstract class Graph<N, A extends ArcInterface<N>> implements
GraphInterface<N, A> {

/**
* Holds a list of all nodes in this graph
*/
protected Map<N, MultiHashMap<N, A>> nodeMap;

/**
* Factory for creating arcs
*/
protected ArcFactory<N, A> factory;

/**
* GraphObserver @see GraphObserser
*/
protected GraphObserver observer = null;

/**
* Keeps the version number of the graph. The version of the graph is
* initially 0, each call which modifies the graph increments the value by
* 1.
*/
protected int version;

/**
* Default Constructor
*/
public Graph() {
super();
this.nodeMap = new Hashtable<N, MultiHashMap<N, A>>();
this.factory = (ArcFactory<N, A>) new DefaultArcFactory<N>();
this.version = 0;
}

public Graph(ArcFactory<N, A> arcfactory) {
super();
this.nodeMap = new Hashtable<N, MultiHashMap<N, A>>();
this.factory = arcfactory;
this.version = 0;
}

/**
* Constructor used to set the observer from creation
*
* @param observer
*/
public Graph(GraphObserver observer) {
super();
this.nodeMap = new Hashtable<N, MultiHashMap<N, A>>();
this.factory = (ArcFactory<N, A>) new DefaultArcFactory<N>();
this.observer = observer;
this.version = 0;
}

public GraphObserver getObserver() {
return observer;
}

public void setObserver(GraphObserver observer) {
this.observer = observer;
}

@Override
public boolean addArc(N tail, N head, WeightedInterface wi) {
if (this.nodeMap.containsKey(tail) && this.nodeMap.containsKey(head)) {
this.nodeMap.get(tail).put(head,
this.factory.create(tail, head, wi));
Object[] h = { tail, head, wi };
this.raiseEvent(new GraphEvent(Event.Arc_Added, h));
return true;
}
return false;
}

protected boolean addArc(ArcInterface a) {
if (this.nodeMap.containsKey(a.getTail())
&& this.nodeMap.containsKey(a.getHead())) {
this.nodeMap.get(a.getTail()).put(a.getHead(), a);
Object[] h = { a.getTail(), a.getHead(), a.getWeightedInterface() };
this.raiseEvent(new GraphEvent(Event.Arc_Added, h));
return true;
}
return false;
}

@Override
public boolean addNode(N node) {
if (!this.nodeMap.containsKey(node)) {
this.nodeMap.put(node, new MultiHashMap<N, A>());
Object[] h = { node };
this.raiseEvent(new GraphEvent(Event.Node_Added, h));
return true;
}
return false;

}

public boolean addNode(N[] nodes) {
for (int i = 0; i < nodes.length; i++) {
if (!this.addNode(nodes[i])) {
return false;
}
}
return true;
}

@Override
public boolean contains(N node) {
return this.nodeMap.containsKey(node);
}

@Override
public ArrayList<A> getArc(N tail) {
MultiHashMap<N, A> h = this.nodeMap.get(tail);
ArrayList<A> arcs = new ArrayList<A>();
Set<N> s = (Set<N>) h.keySet();
for (N n : (N[]) s.toArray()) {
arcs.addAll(h.getCollection(n));
}

return arcs;
}

@Override
public ArrayList<A> getArc(N tail, N head) {
ArrayList<A> h = (ArrayList<A>) this.nodeMap.get(tail).getCollection(
head);
return (h != null) ? h : new ArrayList<A>();
}

public ArrayList<A> getArcs() {
ArrayList<A> arcs = new ArrayList(this.getNuArcs());

for (N node : this.nodeMap.keySet()) {
arcs.addAll(this.getArc(node));
}
return arcs;
}

@Override
public ArcFactory<N, A> getArcFactory() {
return this.factory;
}

@Override
public ArrayList<N> getNodeMap() {
return (ArrayList<N>) new ArrayList(this.nodeMap.keySet());
}

@Override
public boolean removeArc(N tail, A arc) {
if (this.nodeMap.containsKey(tail)) {
boolean h = ((ArrayList<A>) this.nodeMap.get(tail).get(
arc.getHead())).remove(arc);
Object[] j = { tail, arc };
this.raiseEvent(new GraphEvent(Event.Arc_Removed, j));
return h;
}
return false;
}

@Override
public boolean removeArc(N tail, N head) {
boolean retval = true;
if (this.contains(tail) && this.contains(head)) {
ArrayList<A> arcs = new ArrayList<A>(this.nodeMap.get(tail)
.getCollection(head));
for (A f : arcs)
retval = retval && this.removeArc(tail, f);
}

return retval;
}

@Override
public boolean removeArc(N tail) {
boolean retval = true;
if (this.contains(tail)) {
this.nodeMap.get(tail).clear();
retval = this.nodeMap.get(tail).isEmpty();
Object[] h = { tail };
this.raiseEvent(new GraphEvent(Event.Arc_Reset, h));
}

return retval;
}

@Override
public void resetArcs() {
ArrayList<N> nodes = this.getNodeMap();
for (N j : nodes) {
this.nodeMap.get(j).clear();
Object[] h = {};
this.raiseEvent(new GraphEvent(Event.Arc_Reset, h));
}

}

@Override
public boolean removeNode(N node) {
ArrayList<N> m = new ArrayList<N>(this.nodeMap.keySet());
boolean retval = true;
for (N mhm : m) {
this.nodeMap.get(mhm).remove(node);
retval = retval & !this.nodeMap.get(mhm).containsKey(node);
}
this.nodeMap.remove(node);
Object[] j = { node };
this.raiseEvent(new GraphEvent(Event.Node_Removed, j));
return retval;

}

public int getVersion() {
return this.version;
}

public int getNuArcs() {
ArrayList<MultiHashMap<N, A>> adj = new ArrayList<MultiHashMap<N, A>>(
this.nodeMap.values());
int count = 0;
for (MultiHashMap<N, A> h : adj) {
count += h.totalSize();
}
return count;
}

public int getNuNodes() {
return this.nodeMap.keySet().size();
}

protected void raiseEvent(GraphEvent e) {
this.version++;
if (this.observer != null) {
this.observer.update(e);
}
}

}

Change log

r299 by samwilson001 on Nov 25, 2011   Diff
Added addNode(N[]) method
Go to: 
Project members, sign in to write a code review

Older revisions

r297 by samwilson001 on Sep 2, 2011   Diff
[No log message]
r296 by samwilson001 on Aug 4, 2011   Diff
Added helper function for subclasses
of Graph
r281 by samwilson001 on Oct 20, 2010   Diff
minor changes PMD suggested
All revisions of this file

File info

Size: 5732 bytes, 257 lines
Powered by Google Project Hosting