/
mapping2cg.cpp
159 lines (125 loc) · 3.26 KB
/
mapping2cg.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
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
#include <iostream>
#include <vector>
#include <fstream>
#include <cassert>
#include <cstdlib>
using namespace std;
// ---------------------------------------------------------------------------
// Global vars
unsigned int width;
unsigned int height;
char *map_fname;
char *cg_fname;
// ---------------------------------------------------------------------------
typedef struct
{
pair<int,int> comm;
double pir;
} TCommunication;
// ---------------------------------------------------------------------------
vector<int> ReadMapping(char *fname)
{
ifstream fin;
fin.open(fname, ifstream::in);
if (!fin.is_open())
{
cerr << "Cannot open " << fname << endl;
assert(false);
}
char line[256]; // first line is not used
fin.getline(line, sizeof(line)-1);
vector<int> mapping;
while (!fin.eof())
{
int id;
fin >> id;
if (id != -1)
mapping.push_back(id);
else
break;
}
assert(mapping.size() == width*height);
return mapping;
}
// ---------------------------------------------------------------------------
vector<TCommunication> ReadCG(char *fname)
{
ifstream fin(fname);
if (!fin.is_open())
{
cerr << "Cannot open " << fname << endl;
assert(false);
}
char line[128];
int sv, tv;
double pir;
vector<TCommunication> comms;
while ( !fin.eof() )
{
fin.getline(line, sizeof(line)-1);
if (line[0] != '%')
{
pir = -1.0;
int n = sscanf(line, "%d%d%lf", &sv, &tv, &pir);
if (n != 2 && n != 3)
assert(false);
TCommunication comm;
comm.comm.first = sv;
comm.comm.second = tv;
comm.pir = pir;
comms.push_back(comm);
}
}
return comms;
}
// ---------------------------------------------------------------------------
vector<TCommunication> RemapCG(vector<TCommunication>& cg,
vector<int>& map)
{
vector<TCommunication> rcg;
for (unsigned int i=0; i<cg.size(); i++)
{
TCommunication comm;
comm.comm.first = map[cg[i].comm.first];
comm.comm.second = map[cg[i].comm.second];
comm.pir = cg[i].pir;
rcg.push_back(comm);
}
return rcg;
}
// ---------------------------------------------------------------------------
void ShowCG(vector<TCommunication>& cg)
{
cout << "% File generated from " << map_fname << ", and " << cg_fname << endl;
for (unsigned int i=0; i<cg.size(); i++)
{
cout << cg[i].comm.first << "\t" << cg[i].comm.second;
if (cg[i].pir == -1)
cout << endl;
else
cout << "\t" << cg[i].pir << endl;
}
}
// ---------------------------------------------------------------------------
void ParseCmdLine(int argc, char* argv[])
{
if (argc != 5)
{
cerr << "Usage " << argv[0] << " <width> <height> <map file> <cg file>" << endl;
assert(false);
}
width = atoi(argv[1]);
height = atoi(argv[2]);
map_fname = argv[3];
cg_fname = argv[4];
}
// ---------------------------------------------------------------------------
int main(int argc, char* argv[])
{
ParseCmdLine(argc, argv);
vector<int> mapping = ReadMapping(map_fname);
vector<TCommunication> cg = ReadCG(cg_fname);
vector<TCommunication> rcg = RemapCG(cg, mapping);
ShowCG(rcg);
return 0;
}