I am needing to print out all pairs of numbers from each line read in from a text document. A sample text document would be:
6 8
1 3 5
2 3 4
3 6 5
7 6 8
4 6
7 5
Where the first line is the number of nets (6) and the number of cells (8) for a hypergraph. The rest of the lines are the cells that are in the net. So net 1 consists of cells 1, 3, and 5, net 2 consists of cells 2, 3, and 4 and so on. In order to turn this netlist into an actual graph I need to go through each line and basically take all combinations of the numbers on each line. So after reading in the first net I would like to be able to make a graph with (1,3), (1,5), and (3,5) and then go down the netlist and add to the graph. So far I am able to read everything in from the text file and print out the individual cells that I put into a 2D array. Here is my code for that:
int main() {
ifstream infileHGR; // set stream for hypergraph text file
string inputFileName = "structP.hgr"; // input hypergraph filename here
infileHGR.open(inputFileName, ios::in);
clock_t start = clock(); // start clock
string line;
string data[2]; // initialize data array to take in # of nets and # of cells
int nets = 0;
int cells = 0;
// Reads in the first line of the text file to get # for nets and cells
getline(infileHGR, line);
stringstream ssin(line);
int i = 0;
while (ssin.good() && i < 2) { // error checking to make sure first line is correct format
ssin >> data[i];
i++;
}
nets = atoi(data[0].c_str()); // set first number to number of nets
cells = atoi(data[1].c_str()); // set second number to number of cells
freopen("output.txt", "w", stdout); // writes outptut to text file
// TESTING PURPOSES
cout << "Number of nets = " << nets << endl;
cout << "Number of cells = " << cells << endl;
// while loop to go through rest of the hgr file to make hypergraph (starts at line 2)
string str;
int count = 1; // counter for nets
while (infileHGR.good()) {
getline(infileHGR, str);
stringstream in(str);
int i = 0;
// have the line in str
int n = 1; // start at 1, spaces + 1 = number of nodes per net
for (int i = 0; i < str.length(); ++i) {
if (str.at(i) == ' ') {
n++; // n is number of cells in the net
}
}
// testing
//cout << "str = " << str << endl;
//cout << "n = " << n << endl;
int number;
vector<vector<int> > netList;
vector<int> temp;
while (in >> number){
temp.push_back(number);
}
netList.push_back(temp);
//printNetList(temp); // test to see if info is being put into the vectors
// loop through the 2d vector
for (const auto& inner : netList) {
cout << "net " << count << " = "; //TESTING PURPOSES
for (const auto& item : inner) {
cout << item << " ";
}
count = count + 1;
}
cout << endl;
}
clock_t stop = clock(); // end clock
infileHGR.close();
double elapsed = (double)(stop - start) * 1000.0 / CLOCKS_PER_SEC;
printf("Time elapsed in ms: %f", elapsed);
system("pause"); //for original testing
return 0;
}
I used vectors because every input file will be of different size, and some contain a lot of nets, and some nets have up to 20 cells in them. I need help with getting all pairs (coordinates) from the netlist and printing them out to show all of them. I have messed around with the for loops a lot but can't seem to get something that works. Any help would be greatly appreciated, and just ask if I need to include anything else. Thank you!
Looking at your example,