This repository has been archived by the owner on Oct 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GrapheLArcs.java
116 lines (97 loc) · 3.35 KB
/
GrapheLArcs.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
package graphe.implems;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import graphe.core.*;
public class GrapheLArcs implements IGraphe {
private List<Arc> arcs;
private static final String NULL_STR = "";
private static final int NO_VALUATION = -1;
public GrapheLArcs() {
this.arcs = new ArrayList<>();
}
public GrapheLArcs(String input) {
this();
peupler(input);
}
@Override
public List<String> getSommets() {
List<String> nodes = new ArrayList<>();
for (Arc arc : this.arcs) {
if (NULL_STR.equals(arc.getDestination()) && arc.getValuation() == NO_VALUATION ) {
nodes.add(arc.getSource());
}
}
Collections.sort(nodes);
return nodes;
}
@Override
public List<String> getSucc(String sommet) {
List<String> outgoingArcs = new ArrayList<>();
for (Arc arc : this.arcs) {
if (sommet.equals(arc.getSource()) && arc.getValuation() != NO_VALUATION) {
outgoingArcs.add(arc.getDestination());
}
}
return outgoingArcs;
}
@Override
public int getValuation(String src, String dest) {
for (Arc arc: arcs) {
if (src.equals(arc.getSource()) && dest.equals(arc.getDestination()))
return arc.getValuation();
}
return -1;
}
@Override
public boolean contientSommet(String sommet) {
for(Arc a : this.arcs){
if (a.getSource().equals(sommet) || a.getDestination().equals(sommet)) {
return true;
}
}
return false;
}
@Override
public boolean contientArc(String src, String dest) {
for(Arc a : this.arcs){
if(a.getSource().equals(src) && a.getDestination().equals(dest)){
return true;
}
}
return false;
}
private boolean contains(String source, String destination) {
for (Arc arc: arcs) {
if (source.equals(arc.getSource()) && destination.equals(arc.getDestination()))
return true;
}
return false;
}
@Override
public void ajouterSommet(String noeud) {
if (!contains(noeud, NULL_STR))
this.arcs.add(new Arc(noeud, NULL_STR, NO_VALUATION));
}
@Override
public void ajouterArc(String source, String destination, Integer valeur) throws IllegalArgumentException {
if (valeur < 0) throw new IllegalArgumentException("Valuation négative.");
ajouterSommet(source);
ajouterSommet(destination);
if (contains(source, destination)) throw new IllegalArgumentException("Arc déjà présent.");
else this.arcs.add(new Arc(source, destination, valeur));
}
@Override
public void oterSommet(String noeud) {
arcs.removeIf(arc -> noeud.equals(arc.getSource()) || noeud.equals(arc.getDestination()));
}
@Override
public void oterArc(String source, String destination) throws IllegalArgumentException {
if (!contains(source, destination)) throw new IllegalArgumentException("Cet arc n'existe pas.");
arcs.removeIf(arc -> source.equals(arc.getSource()) && destination.equals(arc.getDestination()));
}
@Override
public String toString() {
return this.toAString();
}
}