-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
132 lines (125 loc) · 6.08 KB
/
utils.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
#include "utils.h"
#include "ktn.h"
#include <iostream>
using namespace std;
/* print sparse matrix, used for debugging */
void print_sparse_matrix(const Csr_mtx &T_csr) {
cout << "\n>>>>>PRINTING SPARSE MATRIX<<<<<" << "\nrow lengths:";
vector<pair<double,int>>::const_iterator it_vec;
for (auto rl: T_csr.second) { cout << " " << rl; }; cout << endl;
int k=0, rn=0; cout << "\nrow: " << rn << endl;
if (k==T_csr.second[rn]) while (k==T_csr.second[rn]) { rn++; cout << "\nrow: " << rn << endl; }
for (it_vec=T_csr.first.begin();it_vec!=T_csr.first.end();it_vec++) {
cout << " col: " << it_vec->second << " val: " << it_vec->first << endl;
k++;
if ((k==T_csr.second[rn]) && (k<T_csr.first.size())) while (k==T_csr.second[rn]) { rn++; cout << "\nrow: " << rn << endl; };
}
cout << endl;
}
/* loop over TO or FROM edges downstream from edgeptr in linked list (used in debug tests) */
void print_edgeptr_info(Edge *edgeptr, int opt) {
do {
cout << "ts_id: " << edgeptr->ts_id << " w " << edgeptr->w << " FROM " << edgeptr->from_node->min_id << \
" TO " << edgeptr->to_node->min_id << endl;
if (opt==1) { edgeptr = edgeptr->next_to; } // scan TO nodes
else if (opt==2) { edgeptr = edgeptr->next_from; } // scan FROM nodes
} while (edgeptr != nullptr);
}
/* test merging of nodes */
void test_merge(Network &ktn, int n1, int n2) {
Edge *edgeptr;
cout << "\n\n*******\n\nmerging nodes " << n1 << " and " << n2 << "..." << endl;
cout << "\noriginally edges pointing TO node " << n1 << ":" << endl;
edgeptr = ktn.min_nodes[n1-1].top_to;
print_edgeptr_info(edgeptr,1);
cout << "\noriginally edges pointing FROM node " << n1 << ":" << endl;
edgeptr = ktn.min_nodes[n1-1].top_from;
print_edgeptr_info(edgeptr,2);
cout << "\noriginally edges pointing TO node " << n2 << ":" << endl;
edgeptr = ktn.min_nodes[n2-1].top_to;
print_edgeptr_info(edgeptr,1);
cout << "\noriginally edges pointing FROM node " << n2 << ":" << endl;
edgeptr = ktn.min_nodes[n2-1].top_from;
print_edgeptr_info(edgeptr,2);
cout << "\nnow merge nodes " << n1 << " & " << n2 << "..." << endl;
ktn.merge_nodes(n1-1,n2-1);
cout << "\nnew edges pointing TO node " << n1 << ":" << endl;
edgeptr = ktn.min_nodes[n1-1].top_to;
print_edgeptr_info(edgeptr,1);
cout << "\nnode " << n2 << " has no TO edges? " << (ktn.min_nodes[n2-1].top_to==nullptr) << \
" node " << n2 << " is deleted? " << ktn.min_nodes[n2-1].deleted << endl;
cout << "\n new edges pointing FROM node " << n1 << ":" << endl;
edgeptr = ktn.min_nodes[n1-1].top_from;
print_edgeptr_info(edgeptr,2);
cout << "\nnode " << n2 << " has no FROM edges? " << (ktn.min_nodes[n2-1].top_from==nullptr) << endl;
}
/* debug tests for implementation of data structure */
void run_debug_tests(Network &ktn) {
// TESTS
// trivial tests
cout << ktn.min_nodes[3].min_id << " (should be 4)" << endl;
cout << ktn.ts_edges[2*500].from_node->min_id << " " << ktn.ts_edges[2*500].to_node->min_id << endl;
cout << ktn.ts_edges[(2*500)+1].from_node->min_id << " " << ktn.ts_edges[(2*500)+1].to_node->min_id << endl;
// loop over TO nodes
cout << "iterate over all edges pointing TO node 1..." << endl;
int i=0;
Node *nodeptr; Edge *edgeptr;
edgeptr = ktn.min_nodes[i].top_to;
print_edgeptr_info(edgeptr,1);
// loop over FROM nodes
cout << "\niterate over all edges pointing FROM node " << i+1 << "..." << endl;
edgeptr = ktn.min_nodes[i].top_from;
print_edgeptr_info(edgeptr,2);
// delete a single TO edge
int j=252;
cout << "\ndeleting TO edge with ts_id " << j << " from stack for node " << i+1 << "..." << endl;
ktn.del_spec_to_edge(i,j);
edgeptr = ktn.min_nodes[i].top_to;
print_edgeptr_info(edgeptr,1);
// update an edge so that it now is TO a new node
j=3000;
edgeptr = &ktn.ts_edges[j];
cout << "\nupdating edge with ts_pos " << edgeptr->ts_pos << " so that it points TO node " << i+1 << endl;
cout << "edge originally points FROM " << edgeptr->from_node->min_id << " TO " << \
edgeptr->to_node->min_id << " ts_id: " << edgeptr->ts_id << endl;
cout << "TO edges for the original TO node:" << endl;
edgeptr = ktn.min_nodes[edgeptr->to_node->min_id-1].top_to;
int old_to_min_id = edgeptr->to_node->min_id;
print_edgeptr_info(edgeptr,1);
cout << "now update TO edge:" << endl;
ktn.update_to_edge(i,j);
cout << "new TO edges for the original TO node:" << endl;
edgeptr = ktn.min_nodes[old_to_min_id-1].top_to;
print_edgeptr_info(edgeptr,1);
cout << "new TO edges for the new TO node:" << endl;
edgeptr = ktn.min_nodes[i].top_to;
print_edgeptr_info(edgeptr,1);
cout << "now specifically delete the top TO edge for the original TO node:" << endl;
edgeptr = ktn.min_nodes[old_to_min_id-1].top_to;
ktn.del_spec_to_edge(old_to_min_id-1,edgeptr->ts_id);
cout << " is top TO for this node nullptr? " << (ktn.min_nodes[old_to_min_id-1].top_to==nullptr) << endl;
// delete all TO edges
cout << "\nnow deleting all edges pointing TO node " << i+1 << "..." << endl;
edgeptr = ktn.min_nodes[i].top_to;
Edge **edgeptrptr = &ktn.min_nodes[i].top_to;
cout << "edgeptrptr initially points to: " << (*edgeptrptr)->ts_id << endl;
do {
cout << "deleting edge, ts_id " << edgeptr->ts_id << endl;
ktn.del_to_edge(i);
edgeptr = edgeptr->next_to;
if ((*edgeptrptr) != nullptr) {
cout << "edgeptrptr now points to: " << (*edgeptrptr)->ts_id << endl;
} else {
cout << "edgeptrptr now points to nullptr" << endl;
}
} while (edgeptr != nullptr);
// try to delete a TO edge now that such an edge no longer exists
cout << "\ntry to delete an edge pointing TO node 1 (no such edge exists now)..." << endl;
try {
ktn.del_to_edge(i);
} catch (Network::Ktn_exception& ktn_exc) {
cout << "I caught a KTN_exception!" << endl;
}
// test merging of nodes
test_merge(ktn,6,100); // merge nodes with min ID's 6 and 100
}