- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
Latest commit
227 lines (191 loc) · 6.81 KB
/
Copy pathNode.java
File metadata and controls
227 lines (191 loc) · 6.81 KB
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
importjava.util.Set;
importjava.util.HashSet;
publicclassNodeimplementsComparable<Node> {
privatestaticintcpt = 0;
privatefinalintid;
privateLinkabledata;
privateNodenext;
publicNode(Linkabledata) {
this.id = cpt++;
this.data = data;
}
publicLinkablegetData() {
returnthis.data;
}
publicNodegetNext() {
returnthis.next;
}
publicvoidsetNext(Nodenext) {
this.next = next;
}
/**
* Goes through each linked Node to count how many nodes there are.
* @return int for the number of nodes
*/
publicintsize() {
Nodecurrent = this;
intsize = 0;
while (current != null) {
current = current.getNext();
size++;
}
returnsize;
}
/**
* Goes to the last Node.
* @return Node representing the last linked Node, the one that has no next.
*/
publicNodegetLast() {
Nodecurrent = this;
while (current.next != null) {
current = current.next;
}
returncurrent;
}
/**
* Makes a copy of the current Node with its following Nodes
* @return Node that is independent is its source.
*/
publicNodecopy(Nodestop) {
Nodecopy = newNode(null);
NodecurrentOnCopy = copy;
NodecurrentOnThis = this;
intstopId = stop.id;
while (currentOnThis != null && stopId != currentOnThis.id) {
currentOnCopy.next = newNode(currentOnThis.getData());
currentOnCopy = currentOnCopy.next;
currentOnThis = currentOnThis.next;
}
currentOnCopy.next = newNode(currentOnThis.getData());
returncopy.next;
}
/**
* Makes the concatenation of the specified Node and the current one.
* @param another the Node to append
* @return Node starting with the parameter Node and followed by the current Node
*/
publicNodeconcat(Nodeanother) {
if (another != null) {
NodetoJoin = another.getLast();
if (toJoin.getData().equals(this.getData())) {
toJoin.next = this.next;
} else {
toJoin.next = this;
}
returnanother;
} else {
returnthis;
}
}
/**
* Determines which components are crossed by following this linked Node
* @return Set<String> containing all crossed components
*/
publicSet<String> whichComponentsCrossed() {
Set<String> crossedComponents = newHashSet<>();
Set<String> intersection;
Nodecurrent = this;
while (current.next != null) {
intersection = Node.intersect(current.getData().getComponents(), current.next.getData().getComponents());
if (!intersection.isEmpty()) {
crossedComponents.addAll(intersection);
}
current = current.next;
}
returncrossedComponents;
}
/**
* Determine whether the given Node can replace the specified segment of Nodes
* without changing the crossed components
* @param another replacement
* @param to end Node of the current linked Node
* @param origin the entire linked Node of the current Node
* @param all crossed components
* @return true replaceable
* @return false not replaceable
*/
publicbooleanisReplaceableInComponentsBy(Nodeanother, Nodeto, Nodeorigin, Set<String> all) {
Set<String> fusion;
// Determine the components before this;
NodesaveNextThis = this.next;
this.next = null;
fusion = origin.whichComponentsCrossed();
this.next = saveNextThis;
// Adding the components of another
fusion.addAll(another.whichComponentsCrossed());
// Adding components after
fusion.addAll(to.whichComponentsCrossed());
returnfusion.containsAll(all);
}
/**
* Replaces a segment of the current linked Node from a start with a specified one.
* @param stop last Node of the replaced segment
* @param with linked Nodes replacer
*/
publicvoidreplaceUntil(Nodestop, Nodewith) {
this.next = (this.equals(with)) ? with.next : with; // Skipping the first node in case of an equality
Nodelast = with.getLast();
last.next = (last.equals(stop)) ? stop.next : stop; // Skipping the last node of the new path in case of equality
}
/**
* Intersection of 2 Sets
* @param first Set to intersect
* @param Set to intersect
* @param the intersection as a Set without modifying the parameters.
*/
publicstaticSet<String> intersect(Set<String> first, Set<String> second) {
Set<String> intersection = newHashSet<>(first);
intersection.retainAll(second);
returnintersection;
}
/**
* Determines which components are crossed by following this linked Node
* @return Set<String> containing all crossed components
*/
publicSet<String> getCrossedComponents() {
Set<String> crossedComponents = newHashSet<>();
Set<String> intersection;
Nodecurrent = this;
while (current.next != null) {
intersection = Node.intersect(current.data.getComponents(),current.next.data.getComponents());
if (intersection.size() >= 1) {
crossedComponents.addAll(intersection);
}
current = current.next;
}
returncrossedComponents;
}
publicbooleanequals(Nodeanother) {
return (this.getData().equals(another.getData()));
}
@Override
publicbooleanequals(Objectanother) {
if (!this.getClass().equals(another.getClass())) {
returnfalse;
}
NodeanotherNode = (Node)another;
return (this.equals(anotherNode));
}
@Override
publicinthashCode() {
returnthis.getData().hashCode();
}
@Override
publicintcompareTo(Nodeanother) {
return (this.size() - another.size());
}
@Override
publicStringtoString() { // Print reversed chain
StringBuilderstrb = newStringBuilder();
if (this.next == null) {
return ("\n\tGroupe(s) : " + this.getData().getComponents() + "\t : " + this.getData().toString() + "\n\t");
} else {
strb.append(this.next);
// Groupe = Composantes connexes
strb.append("Groupe(s) : " + this.data.getComponents());
strb.append("\t : " +this.getData());
strb.append("\n\t");
returnstrb.toString();
}
}
}