Skip to content

Commit 02f2485

Browse files
committed
checker: NwTable class for CSV-io
1 parent cc78257 commit 02f2485

File tree

3 files changed

+215
-15
lines changed

3 files changed

+215
-15
lines changed

planning/src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
static const char* _pln_VERSION_STR = "pln0307";
1+
static const char* _pln_VERSION_STR = "pln0308";
22

33
#include "RS/rsEnv.h"
44
#include "util/pln_log.h"

planning/src/util/nw/Nw_io.cpp

Lines changed: 167 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "util/nw/Nw.h"
1+
#include "util/nw/Nw_table.h"
22
#include "file_readers/pln_Fio.h"
33

44
//
@@ -48,17 +48,17 @@ namespace pln {
4848

4949
static const char* _colLabels[] = {
5050

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
6262
"label", // L
6363
nullptr, // M
6464
"name", // N
@@ -83,8 +83,158 @@ static const char* _colLabels[] = {
8383

8484
using std::endl;
8585

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+
}
87169

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;
88238
for (uint i = 0; i < 26; i++) {
89239
CStr label = _colLabels[i];
90240
if (!label) {
@@ -97,7 +247,10 @@ uint NW::printCsv(ostream& os) const noexcept {
97247
}
98248
os << endl;
99249

100-
return 1;
250+
NwTable nwt(*this);
251+
nwt.printMatrix(os);
252+
253+
return numN() + numE();
101254
}
102255

103256
uint NW::dumpCsv() const noexcept {

planning/src/util/nw/Nw_table.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#pragma once
2+
#ifndef _PLN_util_Nw_TABLE_h_88c68a84f705_
3+
#define _PLN_util_Nw_TABLE_h_88c68a84f705_
4+
5+
#include "util/nw/Nw.h"
6+
7+
// Table representation of Network
8+
// for CSV-io and for pretty-printing
9+
10+
namespace pln {
11+
12+
using std::string;
13+
using std::vector;
14+
15+
struct NwTable {
16+
17+
vector<string> header_;
18+
19+
size_t nr_ = 0, nc_ = 0; // #rows and #columns, without header_
20+
21+
size_t beginEdges_ = 0; // 1st row of the edges section
22+
23+
uint16_t trace_ = 0;
24+
25+
NwTable() noexcept = default;
26+
NwTable(const NW& g) noexcept;
27+
~NwTable();
28+
29+
NwTable(const NwTable&) = delete;
30+
NwTable& operator=(const NwTable&) = delete;
31+
32+
uint printMatrix(ostream& os) noexcept;
33+
34+
private:
35+
void alloc_num_matrix() noexcept;
36+
void alloc_str_matrix() noexcept;
37+
void free_num_matrix() noexcept;
38+
void free_str_matrix() noexcept;
39+
40+
string** smat_ = nullptr; // string matrix
41+
size_t** nmat_ = nullptr; // number matrix
42+
};
43+
44+
}
45+
46+
#endif
47+

0 commit comments

Comments
 (0)