1
- #include " util/nw/Nw .h"
1
+ #include " util/nw/Nw_table .h"
2
2
#include " file_readers/pln_Fio.h"
3
3
4
4
//
@@ -48,17 +48,17 @@ namespace pln {
48
48
49
49
static const char * _colLabels[] = {
50
50
51
- " NdEd" , // A
52
- " Id" , // B
53
- " color" , // C
54
- " depth" , // D
55
- nullptr , // E
56
- " flags" , // F
57
- nullptr , // G
58
- nullptr , // H
59
- " n1" , // I
60
- " n2" , // J
61
- " key" , // K
51
+ " NdEd" , // A-0
52
+ " Id" , // B-1
53
+ " color" , // C-2
54
+ " depth" , // D-3
55
+ nullptr , // E-4
56
+ " flags" , // F-5
57
+ nullptr , // G-6
58
+ nullptr , // H-7
59
+ " n1" , // I-8
60
+ " n2" , // J-9
61
+ " key" , // K-10
62
62
" label" , // L
63
63
nullptr , // M
64
64
" name" , // N
@@ -83,8 +83,158 @@ static const char* _colLabels[] = {
83
83
84
84
using std::endl;
85
85
86
- uint NW::printCsv (ostream& os) const noexcept {
86
+ NwTable::NwTable (const NW& g) noexcept {
87
+ if (g.size () < 2 )
88
+ return ;
89
+
90
+ size_t nn = g.numN (), ne = g.numN ();
91
+ nc_ = 27 ;
92
+ nr_ = nn + ne;
93
+ beginEdges_ = nn;
94
+
95
+ alloc_num_matrix ();
96
+ alloc_str_matrix ();
97
+
98
+ header_.reserve (nc_ + 1 );
99
+ header_.resize (26 );
100
+ assert (_colLabels[0 ]);
101
+ header_[0 ] = _colLabels[0 ];
102
+
103
+ for (uint i = 1 ; i < 26 ; i++) {
104
+ CStr label = _colLabels[i];
105
+ if (label)
106
+ header_[i] = label;
107
+ }
108
+
109
+ // nodes:
110
+ uint row_cnt = 0 ;
111
+ for (NW::cNI I (g); I.valid (); ++I, row_cnt++) {
112
+ const NW::Node& ii = *I;
113
+ smat_[row_cnt][0 ] = " Nd" ;
114
+ nmat_[row_cnt][1 ] = ii.id_ ;
115
+ nmat_[row_cnt][2 ] = ii.color_ ;
116
+ nmat_[row_cnt][3 ] = ii.par_ ;
117
+
118
+ nmat_[row_cnt][10 ] = ii.key_ ;
119
+ }
120
+
121
+ // edges:
122
+ beginEdges_ = row_cnt;
123
+ for (NW::cEI I (g); I.valid (); ++I, row_cnt++) {
124
+ const NW::Edge& e = *I;
125
+ smat_[row_cnt][0 ] = " Ed" ;
126
+ nmat_[row_cnt][1 ] = e.id_ ;
127
+ nmat_[row_cnt][2 ] = e.color_ ;
128
+
129
+ nmat_[row_cnt][8 ] = e.n1_ ;
130
+ nmat_[row_cnt][9 ] = e.n2_ ;
131
+ }
132
+ }
133
+
134
+ NwTable::~NwTable () {
135
+ free_num_matrix ();
136
+ free_str_matrix ();
137
+ }
138
+
139
+ void NwTable::alloc_num_matrix () noexcept {
140
+ assert (!nmat_);
141
+ assert (nr_ > 0 && nc_ > 1 );
142
+
143
+ nmat_ = new size_t *[nr_ + 2 ];
144
+ for (size_t r = 0 ; r < nr_ + 2 ; r++) {
145
+ nmat_[r] = new size_t [nc_ + 2 ];
146
+ ::memset (nmat_[r], 0 , (nc_ + 2 ) * sizeof(size_t ));
147
+ }
148
+ }
149
+
150
+ void NwTable::alloc_str_matrix () noexcept {
151
+ assert (!smat_);
152
+ assert (nr_ > 0 && nc_ > 1 );
153
+
154
+ smat_ = new string*[nr_ + 2 ];
155
+ for (size_t r = 0 ; r < nr_ + 2 ; r++) {
156
+ smat_[r] = new string[nc_ + 2 ];
157
+ }
158
+ }
159
+
160
+ void NwTable::free_num_matrix () noexcept {
161
+ if (!nmat_) {
162
+ nr_ = nc_ = 0 ;
163
+ return ;
164
+ }
165
+ if (nr_ < 2 ) {
166
+ nmat_ = nullptr ;
167
+ return ;
168
+ }
87
169
170
+ for (size_t r = 0 ; r < nr_ + 2 ; r++)
171
+ delete[] nmat_[r];
172
+
173
+ delete[] nmat_;
174
+ nmat_ = nullptr ;
175
+ }
176
+
177
+ void NwTable::free_str_matrix () noexcept {
178
+ if (!smat_) {
179
+ nr_ = nc_ = 0 ;
180
+ return ;
181
+ }
182
+ if (nr_ < 2 ) {
183
+ smat_ = nullptr ;
184
+ return ;
185
+ }
186
+
187
+ for (size_t r = 0 ; r < nr_ + 2 ; r++)
188
+ delete[] smat_[r];
189
+
190
+ delete[] smat_;
191
+ smat_ = nullptr ;
192
+ }
193
+
194
+ uint NwTable::printMatrix (ostream& os) noexcept {
195
+ if (!nc_ or !nr_ or !nmat_)
196
+ return 0 ;
197
+ assert (smat_);
198
+ assert (beginEdges_ > 0 );
199
+
200
+ // nodes:
201
+ for (size_t r = 0 ; r < beginEdges_; r++) {
202
+ size_t * row_nums = nmat_[r];
203
+ string* row_strs = smat_[r];
204
+ assert (row_nums);
205
+ assert (row_strs);
206
+ for (size_t c = 0 ; c < nc_; c++) {
207
+ if (c) os << ' ,' ;
208
+ if (row_strs[c].empty ())
209
+ os << row_nums[c];
210
+ else
211
+ os << row_strs[c];
212
+ }
213
+ os << endl;
214
+ }
215
+
216
+ // edges:
217
+ for (size_t r = beginEdges_; r < nr_; r++) {
218
+ size_t * row_nums = nmat_[r];
219
+ string* row_strs = smat_[r];
220
+ assert (row_nums);
221
+ assert (row_strs);
222
+ for (size_t c = 0 ; c < nc_; c++) {
223
+ if (c) os << ' ,' ;
224
+ if (row_strs[c].empty ())
225
+ os << row_nums[c];
226
+ else
227
+ os << row_strs[c];
228
+ }
229
+ os << endl;
230
+ }
231
+
232
+ return nr_;
233
+ }
234
+
235
+ uint NW::printCsv (ostream& os) const noexcept {
236
+ if (size () < 2 )
237
+ return 0 ;
88
238
for (uint i = 0 ; i < 26 ; i++) {
89
239
CStr label = _colLabels[i];
90
240
if (!label) {
@@ -97,7 +247,10 @@ uint NW::printCsv(ostream& os) const noexcept {
97
247
}
98
248
os << endl;
99
249
100
- return 1 ;
250
+ NwTable nwt (*this );
251
+ nwt.printMatrix (os);
252
+
253
+ return numN () + numE ();
101
254
}
102
255
103
256
uint NW::dumpCsv () const noexcept {
0 commit comments