Repository navigation
Expand file tree
/
Copy pathedge.cpp
More file actions
132 lines (102 loc) · 3.25 KB
/
Copy pathedge.cpp
File metadata and controls
132 lines (102 loc) · 3.25 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
#include <iostream>
#include <string>
#include <regex>
#include "edge.h"
using namespace std;
namespace AboutGraphs
{
Edge *const Edge::EMPTY_EDGE = new Edge();
const regex Edge::PATTERN_TO_VALIDATE_AN_EDGE = regex("[(|{]\\s*\\w+\\s*,(\\s*\\w+\\s*,)?\\s*\\w+\\s*[)|}]");
bool Edge::is_edge_directed()
{
return is_directed;
}
bool Edge::is_edge_pondered()
{
return is_pondered;
}
string &Edge::get_string_representation()
{
return string_representation;
}
int Edge::get_edge_value()
{
return value;
}
string &Edge::get_first_vertice()
{
return first_vertice;
}
string &Edge::get_second_vertice()
{
return second_vertice;
}
Edge *Edge::from_string(string &edge_representation)
{
if (regex_search(edge_representation, PATTERN_TO_VALIDATE_AN_EDGE) == false)
{
return EMPTY_EDGE;
}
Edge *edge = new Edge();
edge->string_representation = edge_representation;
if (!edge->is_edge_enclosed_correctly())
{
return EMPTY_EDGE;
}
edge->fill_properties_of_the_edge();
return edge;
}
bool Edge::is_edge_enclosed_correctly()
{
char opening_char = string_representation[0];
char closing_char = string_representation[string_representation.length() - 1];
return ((opening_char == '(' && closing_char == ')') || (opening_char == '{' && closing_char == '}'));
}
void Edge::fill_properties_of_the_edge()
{
int first_comma_index = string_representation.find_first_of(',');
int second_comma_index;
first_vertice = string_representation.substr(1, first_comma_index - 1);
if (count(string_representation.begin(), string_representation.end(), ',') == 2)
{
is_pondered = true;
second_comma_index = string_representation.find_last_of(',');
second_vertice = string_representation.substr(first_comma_index + 1, second_comma_index - first_comma_index - 1);
value = stoi(string_representation.substr(second_comma_index + 1, string_representation.length() - second_comma_index - 2));
}
else
{
is_pondered = false;
second_vertice = string_representation.substr(first_comma_index + 1, string_representation.length() - first_comma_index - 2);
}
do_inplace_trim(first_vertice);
do_inplace_trim(second_vertice);
if (string_representation[0] == '(')
is_directed = true;
else
is_directed = false;
}
void Edge::do_inplace_trim(string &s)
{
do_inplace_left_trim(s);
do_inplace_right_trim(s);
}
void Edge::do_inplace_left_trim(string &s)
{
int index = 0;
while (s[index] == ' ')
{
index++;
}
s.erase(0, index);
}
void Edge::do_inplace_right_trim(string &s)
{
int index = s.length() - 1;
while (s[index] == ' ')
{
index--;
}
s.erase(index + 1, s.length() - index);
}
}