-
Notifications
You must be signed in to change notification settings - Fork 0
/
Structures.h
299 lines (256 loc) · 7.34 KB
/
Structures.h
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#ifndef STRUCTURES
#define STRUCTURES
#include <map>
#include <vector>
#include <utility>
#include <iostream>
#include <cstdlib>
#include <climits>
#include "LinearAssignment/StructureAP.h"
typedef int edgeCost;
/*
dTuple:: each four bits are equal to the index of a vertex,
we can represent as much as 8 vertices (then dimensions) by word.
*/
typedef std::vector<std::vector<edgeCost> > matriz;
typedef std::vector<int> dTuple;
//typedef unsigned long long int dataTypeHyperEdge;//for a maximum of 8 dimensions of size 8 by each
class HyperGraph{
typedef unsigned long long int dataTypeHyperEdge;//for a maximum of 8 dimensions of size 8 by each
private:
std::map<dataTypeHyperEdge, edgeCost> hyperGraph;
std::vector<int> dimensions;
std::vector<unsigned long long int> offset;
public:
HyperGraph(){
}
~HyperGraph(){
std::cout << "entra aqui y destruye\n";
hyperGraph.clear();
dimensions.clear();
offset.clear();
}
unsigned long long int getOffset(int dim){
unsigned long long int powerOfTwo = 1, offset = 0;
for(; powerOfTwo < dim; powerOfTwo<<=1, offset++);
return offset;
}
void getOffsetVector(std::vector<int> dimensions){
for(unsigned int dim = 0; dim < dimensions.size(); dim++)
offset.push_back(getOffset(dimensions[dim]));
return;
}
void setDimensions(const std::vector<int> &dimensions){
this->dimensions = dimensions;
this->getOffsetVector(dimensions);
return;
}
std::vector<int> getDimensions(){
return this->dimensions;
}
int getDimension(int i){
return (int)this->dimensions.size() > i ? this->dimensions[i] : -1;
}
int getNumberOfDimensions(){
return (int)this->dimensions.size();
}
int getNumberOfEdges(){
return (int)this->hyperGraph.size();
}
void addEdge(std::vector<unsigned long long int> &v, int cost){
if(v.size() != this->getNumberOfDimensions()){
std::cout << "Error: the vertex array doesnot have the required size\n";
exit(0);
}
dataTypeHyperEdge edge = 0;
for(unsigned int i = 0; i < v.size(); i++){
edge <<= offset[i];
edge |= v[i];
}
this->hyperGraph[edge] = cost;
return;
}
dataTypeHyperEdge getHyperEdge(const dTuple &tuple){
dataTypeHyperEdge hyperEdge = 0;
for(unsigned int i = 0; i < tuple.size(); i++){
hyperEdge <<= offset[i];
hyperEdge |= tuple[i];
}
return hyperEdge;
}
dTuple getEdge(int index){
dTuple tuple(this->dimensions.size(), 0);
return tuple;
}
edgeCost getEdgeCost(const int hyperEdge){
std::map<dataTypeHyperEdge, edgeCost>::iterator it = hyperGraph.find(hyperEdge);
if(it == hyperGraph.end()){
std::cout << "Error: the edge doesnot exit\n";
exit(0);
}
return it->second;
}
edgeCost getEdgeCost(const dTuple &edge){
std::map<dataTypeHyperEdge, edgeCost>::iterator it = hyperGraph.find(this->getHyperEdge(edge));
if(it == hyperGraph.end()){
std::cout << "Error: the edge doesnot exit\n";
exit(0);
}
return it->second;
}
edgeCost getEdgeCost(const int dim1, const int dim2, const int dim3){
dTuple edge(3, 0);
edge[0] = dim1;
edge[1] = dim2;
edge[2] = dim3;
return this->getEdgeCost(edge);
}
edgeCost getEdgeCost(const int dim1, const int dim2, const int dim3, const int dim4){
dTuple edge(4, 0);
edge[0] = dim1;
edge[1] = dim2;
edge[2] = dim3;
edge[3] = dim4;
return this->getEdgeCost(edge);
}
bool isEdge(dTuple &edge){
return hyperGraph.find(this->getHyperEdge(edge)) != hyperGraph.end() ? true: false;
}
};
class CompleteHyperGraph{
private:
int numberOfEdges, next;
edgeCost *completeGraph;
std::vector<int> dimensions;
public:
CompleteHyperGraph(){
this->completeGraph = NULL;
}
CompleteHyperGraph(const int dimensions, const int n){
this->dimensions = std::vector<int>(dimensions, n);
this->setDimensions(this->dimensions);
}
~CompleteHyperGraph(){
if(this->completeGraph != NULL)
delete[] this->completeGraph;
this->dimensions.clear();
}
void reStart(){
this->next = 0;
return;
}
void setDimensions(const int s, const int n){
this->dimensions.clear();
this->numberOfEdges = 1;
for(int i = 0; i < s; i++){
this->dimensions.push_back(n);
this->numberOfEdges *= n;
}
this->completeGraph = new edgeCost[this->numberOfEdges];
this->next = 0;
return;
}
void setDimensions(const std::vector<int> &dimensions){
this->dimensions = dimensions;
this->numberOfEdges = 1;
for(unsigned int i = 0; i < dimensions.size(); i++)
this->numberOfEdges *= dimensions[i];
this->completeGraph = new edgeCost[this->numberOfEdges];
this->next = 0;
return;
}
std::vector<int> getDimensions(){
return this->dimensions;
}
int getDimension(int i){
return (int)this->dimensions.size() > i ? this->dimensions[i] : -1;
}
int getNumberOfDimensions(){
return (int)this->dimensions.size();
}
int getNumberOfEdges(){
return this->numberOfEdges;
}
void addEdge(edgeCost cost){
// cout << this->next << " " << cost << endl;
this->completeGraph[this->next++] = cost;
}
dTuple getEdge(int index){
dTuple tuple(this->dimensions.size(), 0);
for(int i = (int)this->dimensions.size()-1; i >= 0; i--){
tuple[i] = index%(this->dimensions[i]);
index /= this->dimensions[i];
}
return tuple;
}
edgeCost getEdgeCost(const int index){
return this->completeGraph[index];
}
edgeCost getEdgeCost(const dTuple &edge){
int index = 0, prod = 1;
for(int i = (int)edge.size()-1; i >= 0; i--){
index += edge[i]*prod;
prod *= this->getDimension(i);
}
return this->completeGraph[index];
}
edgeCost getEdgeCost(const int dim1, const int dim2, const int dim3){
return this->completeGraph[dim1*this->getDimension(1)*this->getDimension(2)+dim2*this->getDimension(2)+dim3];
}
edgeCost getEdgeCost(const int dim1, const int dim2, const int dim3, const int dim4){
return this->completeGraph[dim1*this->getDimension(1)*this->getDimension(2)*this->getDimension(3)
+dim2*this->getDimension(2)*this->getDimension(3)+dim3*this->getDimension(3)+dim4];
}
bool isEdge(dTuple &edge){
int index = 0, prod = 1;
for(int i = (int)edge.size()-1; i >= 0; i--){
index += edge[i]*prod;
prod *= this->getDimension(i);
}
return index < this->numberOfEdges;
}
};
class Assignment{
public:
std::vector<dTuple> matching;
edgeCost cost;
Assignment(){
}
Assignment(const std::vector<dTuple> &matching, const int cost){
this->matching = matching;
this->cost = cost;
}
Assignment(int cost){
this->cost = cost;
}
~Assignment(){
this->matching.clear();
}
bool operator<(const Assignment &assignment) const {
return this->cost<assignment.cost;
}
bool operator>(const Assignment &assignment) const {
return this->cost>assignment.cost;
}
bool operator==(const Assignment &assignment) const {
if(this->cost!=assignment.cost){
return false;
}
if(this->matching.size() != assignment.matching.size()){
return false;
}
for(unsigned int i = 0; i < this->matching.size(); i++){
if(this->matching[i].size() != assignment.matching[i].size()){
return false;
}
for(unsigned int j = 0; j < this->matching[i].size(); j++){
if(this->matching[i][j] != assignment.matching[i][j]){
return false;
}
}
}
return true;
}
};
//*/
#endif