/
AdjMatrix.cpp
114 lines (90 loc) · 2.4 KB
/
AdjMatrix.cpp
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
/*
* AdjMatrix.cpp
*
* Created on: 17/apr/2010
* Author: ciccio
*/
#include "AdjMatrix.h"
#include <stdio.h>
#include <vector>
#include "globals.h"
using namespace std;
AdjMatrix::AdjMatrix() {
// TODO Auto-generated constructor stub
}
AdjMatrix::~AdjMatrix() {
for(int i=0;i<_n;i++)
delete (*_mat)[i];
delete *_mat;
delete _mat;
}
AdjMatrix::AdjMatrix(int n, int m) {
_n=n;
_m=m;
_mat= new int **;
(*_mat)= new int *[n];
for(int i=0;i<n;i++) {
(*_mat)[i]=new int [m];
for(int j=0;j<m;j++)
(*_mat)[i][j]=0;
}
}
/* THE FOLLOWING WORKS BUT NOW THE SAME IS DONE IN Graph.cpp
void AdjMatrix::toDot(Nodeset &Ns, string fname) {
FILE *fid;
Nodeset::iterator i;
Nodeset::iterator j;
int ii=0, ij=0;
fid=fopen((string(fname)).c_str(), "w");
string attr_string="";
std::map<std::string, std::string>::iterator mapit;
std::map<std::string, std::string> attribs;
fprintf(fid, "digraph G {\n");
fprintf(fid, "node [shape=box, style=rounded];\n");
fprintf(fid, "rankdir=LR;\n");
for(i=Ns.begin(); i!=Ns.end(); ++i){
attr_string = "[ label = ";
attr_string += i->getName();
attribs = i->GetAttributes();
for(mapit = attribs.begin(); mapit!=attribs.end(); mapit++){
attr_string += ", ";
attr_string += (mapit -> first).c_str();
attr_string += " = ";
attr_string += (mapit -> second).c_str();
}
attr_string += "] ";
fprintf(fid, "%d %s;\n", i->getId(), attr_string.c_str());
//fprintf(fid, "%d [ label = \"%s\", color = \"%s\" ];\n", i->getId(), i->getName().c_str(), i->GetAttribute("color").c_str());
}
for (ii=0, i=Ns.begin(); ii<_n; ++ii, ++i){ // process ARCs
for (ij=0; ij<_m; ++ij) {
if((*_mat)[ii][ij]) {
bool found = false;
attribs = i->GetAttributes();
for(mapit = attribs.begin(); mapit!=attribs.end(); mapit++){
if(mapit->first == "LEAF_FLAGS" && mapit->second == "F") {
fprintf(fid, "%d->%d [arrowhead = \"empty\"];\n", ii, ij);
found = true;
}
}
if (!found)
fprintf(fid, "%d->%d;\n", ii, ij);
}
}
}
fprintf(fid, "}");
fclose(fid);
}
*/
void AdjMatrix::Set(unsigned i, unsigned j)
{
(*_mat)[i][j]=1;
}
ostream& AdjMatrix::operator>>(std::ostream &){
int j,i;
for(i=0; i<_n; i++){
for(j=0; j<_m-1; j++)
cout << (*_mat)[i][j] << ",";
cout <<(*_mat)[i][j] << endl;
}
}