-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphMutex.cpp
More file actions
205 lines (156 loc) · 4.57 KB
/
GraphMutex.cpp
File metadata and controls
205 lines (156 loc) · 4.57 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
//
// Created by naraujo on 20/08/17.
//
#include "GraphMutex.hpp"
//Construtor
GraphMutex::GraphMutex() {
}
//Insert
bool GraphMutex::insertNode() {
std::lock_guard<std::mutex>structureGuard(structureMutex);
//Creates Edges for New Node
std::vector<int> newNodeEdges;
for (int i = 0; i < nodes.size(); i++){
edges[i].push_back(0);
newNodeEdges.push_back(0);
}
newNodeEdges.push_back(1);
//Insert Node
nodes.push_back(1);
edges.push_back(newNodeEdges);
nodesMutex.push_back(mutex_wrapper());
return true;
}
bool GraphMutex::insertEdge(int n1, int n2) {
structureMutex.lock();
//Safety Checks
if (n1 > edges.size() || n2 > edges[n1].size() || n1 < 0 || n2 < 0 || n1 == n2) {
structureMutex.unlock();
return false;
}
structureMutex.unlock();
std::lock_guard<std::mutex>n1Guard(nodesMutex[n1]);
std::lock_guard<std::mutex>n2Guard(nodesMutex[n2]);
//Checks if already exists
if (edges[n1][n2] > 0)
return false;
else {
edges[n1][n2] = 1;
return true;
}
}
//Delete
bool GraphMutex::deleteNode(int n) {
structureMutex.lock();
if (n > nodes.size() || n < 0) {
structureMutex.unlock();
return false;
}
structureMutex.unlock();
std::lock_guard<std::mutex>structureGuard(structureMutex);
std::lock_guard<std::mutex>nodeGuard(nodesMutex[n]);
//Delete Edges From Other Nodes
for (int i = 0; i < edges.size(); i++){
edges[i].erase(edges[i].begin() + n);
}
//Delete Whole Vector From Vector
edges.erase(edges.begin() + n);
//Delete Node
nodes.erase(nodes.begin() + n);
//Mutex
nodesMutex.pop_back();
return true;
}
bool GraphMutex::deleteEdge(int n1, int n2) {
//Safety Checks
structureMutex.lock();
if (n1 > edges.size() || n2 > edges[n1].size() || n1 < 0 || n2 < 0 || n1 == n2) {
structureMutex.unlock();
return false;
}
structureMutex.unlock();
std::lock_guard<std::mutex>n1Guard(nodesMutex[n1]);
std::lock_guard<std::mutex>n2Guard(nodesMutex[n2]);
if (edges[n1][n2] < 1)
return false;
edges[n1][n2] = 0;
return true;
}
//Costs
bool GraphMutex::increaseEdge(int n1, int n2) {
//Safety Checks
structureMutex.lock();
if (n1 > edges.size() || n2 > edges[n1].size() || n1 < 0 || n2 < 0 || n1 == n2) {
structureMutex.unlock();
return false;
}
structureMutex.unlock();
std::lock_guard<std::mutex>n1Guard(nodesMutex[n1]);
std::lock_guard<std::mutex>n2Guard(nodesMutex[n2]);
//Cant insert
if (edges[n1][n2] < 1)
return false;
else
edges[n1][n2]++;
return true;
}
bool GraphMutex::decreaseEdge(int n1, int n2) {
//Safety Checks
structureMutex.lock();
if (n1 > edges.size() || n2 > edges[n1].size() || n1 < 0 || n2 < 0 || n1 == n2) {
structureMutex.unlock();
return false;
}
structureMutex.unlock();
std::lock_guard<std::mutex>n1Guard(nodesMutex[n1]);
std::lock_guard<std::mutex>n2Guard(nodesMutex[n2]);
//Cant delete
if (edges[n1][n2] <= 1)
return false;
edges[n1][n2]--;
return true;
}
//Getters
bool GraphMutex::getEdge(int n1, int n2, int& out){
//Safety Checks
structureMutex.lock();
if (n1 > edges.size() || n2 > edges[n1].size() || n1 < 0 || n2 < 0) {
structureMutex.unlock();
return false;
}
structureMutex.unlock();
std::lock_guard<std::mutex>n1Guard(nodesMutex[n1]);
std::lock_guard<std::mutex>n2Guard(nodesMutex[n2]);
out = edges[n1][n2];
return true;
}
bool GraphMutex::getNode(int n, int& out){
structureMutex.lock();
if (n > nodes.size() || n < 0) {
structureMutex.unlock();
return false;
}
structureMutex.unlock();
std::lock_guard<std::mutex>nodeGuard(nodesMutex[n]);
out = nodes[n];
return true;
}
void GraphMutex::printEdges() {
std::lock_guard<std::mutex> structureGuard(structureMutex);
std::cout << "Printing Edges..." << std::endl;
std::cout << "-------------- START --------------" << std::endl;
for (int i = 0; i < edges.size(); i++){
nodesMutex[i].lock();
std::cout << "| ";
for (int j = 0; j < edges[i].size(); j++) {
if (i != j)
nodesMutex[j].lock();
std::cout << "(" << edges[i][j] << ") | ";
if (i != j)
nodesMutex[j].unlock();
}
std::cout << std::endl;
nodesMutex[i].unlock();
}
std::cout << "--------------- END ---------------" << std::endl;
}