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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
|
#include "mbl_table.h"
//:
// \file
// \author Kevin de Souza
// \date 05-Aug-2004
// \brief Container for tabulated data suitable for reading/writing to delimited text files
#include <vcl_cstdlib.h>
#include <vcl_iostream.h>
#include <vcl_cmath.h>
// Tolerance used to determine whether table entries are equal
static double tolerance_ = 1e-15;
// Whether the tolerance is applied as a fractional difference
static bool fractional_tolerance_ = false;
// Level of verbosity used for error output.
static int verbosity_ = 0;
//========================================================================
// Constructor
//========================================================================
mbl_table::mbl_table(const char delim)
: delimiter_(delim)
{
}
//========================================================================
//: Constructor
//========================================================================
mbl_table::mbl_table(const char delim,
const vcl_vector<vcl_string>& headers)
: delimiter_(delim),
column_headers_(headers)
{
unsigned ncols = column_headers_.size();
// Allocate column vectors
columns_.resize(ncols);
// Map each column header string to column index
for (unsigned int c=0; c<ncols; ++c)
{
header_to_column_index_[headers[c]] = c;
}
}
//========================================================================
// Return the number of columns
//========================================================================
unsigned mbl_table::num_cols() const
{
return columns_.size();
}
//========================================================================
// Return the number of rows
//========================================================================
unsigned mbl_table::num_rows() const
{
int nrows=0;
// If there are at least 1 column, assume all columns are the same as the first
if (columns_.size()>0) nrows=columns_[0].size();
return nrows;
}
//========================================================================
// Returns true if column exists
//========================================================================
bool mbl_table::column_exists(const vcl_string& header) const
{
// Does the map contain this header?
vcl_map<vcl_string, unsigned>::const_iterator iter =
header_to_column_index_.find(header);
return iter != header_to_column_index_.end();
}
//========================================================================
// Get the column of data corresponding to a particular heading.
//========================================================================
bool mbl_table::get_column(const vcl_string& header,
vcl_vector<double>& column) const
{
bool success = false;
column.clear();
// Does the map contain this header?
vcl_map<vcl_string, unsigned>::const_iterator iter =
header_to_column_index_.find(header);
if (iter != header_to_column_index_.end())
{
// Get the corresponding column vector and copy it to the output vector
column = columns_[iter->second];
success = true;
}
else
{
vcl_cerr << "ERROR: mbl_table::get_column(): column \""
<< header << "\" does not exist in the table.\n";
}
return success;
}
//========================================================================
// Get a specified row of data.
//========================================================================
bool mbl_table::get_row(const unsigned& r,
vcl_vector<double>& row) const
{
// Clear output data
row.clear();
// Check that the specified row index is valid
if (r < num_rows())
{
unsigned int ncols = num_cols();
row.resize(ncols);
for (unsigned c=0; c<ncols; ++c)
{
row[c] = columns_[c][r];
}
return true;
}
else
{
vcl_cerr << "ERROR: mbl_table::get_row(): row "
<< r << " does not exist in the table.\n";
return false;
}
}
//========================================================================
// Get the list of column headers (in column order).
//========================================================================
void mbl_table::get_column_headers(vcl_vector<vcl_string>& headers) const
{
headers = column_headers_;
}
//========================================================================
// Set the value of an existing element.
//========================================================================
bool mbl_table::set_element(const vcl_string& header,
const unsigned r,
const double value)
{
bool success = false;
// Does the map contain this column header?
vcl_map<vcl_string, unsigned>::const_iterator iter =
header_to_column_index_.find(header);
if (iter != header_to_column_index_.end())
{
// Does the column have sufficient rows?
vcl_vector<double>& col = columns_[iter->second];
if (col.size()>r)
{
// Set the value
col[r] = value;
success = true;
}
else
{
vcl_cerr << "ERROR: mbl_table::set_element(): row "
<< r << " does not exist in the table.\n";
}
}
else
{
vcl_cerr << "ERROR: mbl_table::set_element(): column \""
<< header << "\" does not exist in the table.\n";
}
return success;
}
//========================================================================
// Get the value of an existing element.
//========================================================================
double mbl_table::get_element(const vcl_string& header,
const unsigned r,
bool* success/*=0*/) const
{
double value = 1e-19;
if (success)
*success = false;
// Does the map contain this column header?
vcl_map<vcl_string, unsigned>::const_iterator iter =
header_to_column_index_.find(header);
if (iter != header_to_column_index_.end())
{
// Does the column have sufficient rows?
const vcl_vector<double>& col = columns_[iter->second];
if (col.size()>r)
{
// Get the value
value = col[r];
if (success)
*success = true;
}
else
{
vcl_cerr << "ERROR: mbl_table::get_element(): row "
<< r << " does not exist in the table.\n";
}
}
else
{
vcl_cerr << "ERROR: mbl_table::get_element(): column \""
<< header << "\" does not exist in the table.\n";
}
return value;
}
//========================================================================
// Append a column of data with its own heading.
//========================================================================
bool mbl_table::append_column(const vcl_string& header,
const vcl_vector<double>& column)
{
// Check whether there is already a column with this heading
if (header_to_column_index_.find(header) == header_to_column_index_.end())
{
// Check that the length of the new column matches the existing columns
if (num_rows()==0 || num_rows()==column.size())
{
column_headers_.push_back(header);
columns_.push_back(column);
header_to_column_index_[header] = columns_.size()-1;
return true;
}
else
{
vcl_cerr << "ERROR: mbl_table::append_column(): "
<< "new column is different length from existing columns.\n"
<< "Column not appended.\n";
return false;
}
}
else
{
vcl_cerr << "ERROR: mbl_table::append_column(): a column with header \""
<< header << "\" already exists.\n"
<< "Column not appended.\n";
return false;
}
}
//========================================================================
//: Append an empty column with its own heading.
//========================================================================
bool mbl_table::append_column(const vcl_string& header,
const double val/*=0*/)
{
// Check whether there is already a column with this heading
if (header_to_column_index_.find(header) == header_to_column_index_.end())
{
// Append a new column of the same length as existing columns
column_headers_.push_back(header);
unsigned c = columns_.size();
header_to_column_index_[header] = c;
columns_.push_back(vcl_vector<double>());
columns_[c].resize(num_rows(), val);
return true;
}
else
{
vcl_cerr << "ERROR: mbl_table::append_column(): a column with header \""
<< header << "\" already exists.\n"
<< "Column not appended.\n";
return false;
}
}
//========================================================================
// Append a row of data.
//========================================================================
bool mbl_table::append_row(const vcl_vector<double>& row)
{
// Check that the length of the new row matches the existing rows
unsigned ncols = num_cols();
if (ncols==row.size())
{
for (unsigned c=0; c<ncols; ++c)
{
columns_[c].push_back(row[c]);
}
return true;
}
else
{
vcl_cerr << "ERROR: mbl_table::append_row(): "
<< "new row is different length from existing row.\n"
<< "Row not appended.\n";
return false;
}
}
//========================================================================
// Append an empty row.
//========================================================================
bool mbl_table::append_row(const double val/*=0*/)
{
// Append a new element to each column
unsigned ncols = num_cols();
for (unsigned c=0; c<ncols; ++c)
{
columns_[c].push_back(val);
}
return true;
}
//========================================================================
// Load this table's data from specified text stream.
//========================================================================
bool mbl_table::read(vcl_istream& is)
{
bool success = false;
if (is.good() && !is.eof())
{
// Read header row
bool eol = false;
bool eof = false;
unsigned col = 0;
while (!eol && !eof)
{
vcl_string str;
if (read_delimited_string(is, str, eol, eof))
{
// Create an empty column vector and enter it into the map
column_headers_.push_back(str);
header_to_column_index_[str] = col;
columns_.push_back(vcl_vector<double>(0));
col++;
}
}
// Read table data
while (!is.eof())
{
// Read one row
eol = false;
eof = false;
unsigned col = 0;
while (!eol && !eof)
{
vcl_string str;
if (read_delimited_string(is, str, eol, eof))
{
// Convert string to double (NB sets to 0 if string is non-numeric)
double val = vcl_atof(str.c_str());
// Add this double value to the current column vector
columns_[col].push_back(val);
// Advance to the next column or back to the first column
col++;
if (col==num_cols()) col = 0;
}
}
}
success = true;
}
return success;
}
//========================================================================
// Save this table's data to specified text stream.
//========================================================================
void mbl_table::write(vcl_ostream& os) const
{
// How many columns are there?
unsigned int ncols = num_cols();
// How many rows of data do we expect?
unsigned int nrows = num_rows();
// Write column headers row
for (unsigned c=0; c<ncols; ++c)
{
os << column_headers_[c] << delimiter_;
}
os << '\n';
// Write data rows
for (unsigned r=0; r<nrows; ++r)
{
for (unsigned c=0; c<ncols; ++c)
{
os << columns_[c][r] << delimiter_;
}
os << '\n';
}
}
//========================================================================
//: Create a new table with as subset of columns defined by headers
//========================================================================
bool mbl_table::subtable(mbl_table &new_table, const vcl_vector<vcl_string> &headers) const
{
bool ret = true;
new_table = mbl_table();
// Write column headers row
for (unsigned c=0; c<headers.size(); ++c)
{
// get the column for the header if available
vcl_map<vcl_string, unsigned>::const_iterator iter =
header_to_column_index_.find(headers[c]);
if (iter != header_to_column_index_.end())
{
new_table.append_column(headers[c],columns_[iter->second]);
}
else
{
ret=false;
}
}
return ret;
}
//========================================================================
// Read a series of characters from the stream until a delimiter character or eol.
//========================================================================
bool mbl_table::read_delimited_string(vcl_istream& is,
vcl_string& str,
bool& eol,
bool& eof)
{
str = "";
eol = false;
bool success = false;
char c = 0;
bool eos = false;
while (!eos && is.good() && !is.eof())
{
is.get(c);
if (c=='\n')
{
eol = true;
eos = true;
}
else if (c==delimiter_)
{
eos = true;
}
else if (c==0) // We sometimes get this at end-of-file
{
eof = true;
}
else
{
str += c;
}
}
if (eos && str.length()>0)
{
success = true;
}
return success;
}
//========================================================================
// Is another table identical to this one?
//========================================================================
bool mbl_table::operator==(const mbl_table& rhs) const
{
// Are both tables the same object - do they occupy the same memory location?
if (this == &rhs)
{
if (verbosity_>0)
vcl_cout << "Both tables are actually the same memory object!" << vcl_endl;
return true;
}
// Is the delimiter the same?
if (delimiter_ != rhs.delimiter_)
{
if (verbosity_>0)
vcl_cout << "Tables have different delimiter characters" << vcl_endl;
return false;
}
// Is the column headers vector the same?
if (column_headers_ != rhs.column_headers_)
{
if (verbosity_>0)
vcl_cout << "Tables have different column headers" << vcl_endl;
return false;
}
// Is the header to index map the same?
if (header_to_column_index_ != rhs.header_to_column_index_)
{
if (verbosity_>0)
vcl_cout << "Tables have different header-to-column index map" << vcl_endl;
return false;
}
// Are the numbers of columns the same?
unsigned ncols = columns_.size();
if (ncols != rhs.columns_.size())
{
if (verbosity_>0)
vcl_cout << "Tables have different number of columns" << vcl_endl;
return false;
}
// Is the table data the same (within the current tolerance)?
bool values_different = false;
for (unsigned c=0; c<ncols; ++c)
{
// Are the numbers of rows in this column the same?
unsigned nrows = columns_[c].size();
if (nrows != rhs.columns_[c].size())
{
if (verbosity_>0)
vcl_cout << "Tables have different number of elements in some columns"
<< vcl_endl;
return false;
}
// Compare all data values in this column
for (unsigned r=0; r<nrows; ++r)
{
double diff = columns_[c][r] - rhs.columns_[c][r];
if (fractional_tolerance_)
{
diff /= columns_[c][r];
}
if (vcl_fabs(diff) > tolerance_)
{
if (verbosity_>0)
vcl_cout << "Tables have different values in column " << c
<< " (" << column_headers_[c] << "), row " << r
<< ": " << columns_[c][r] << ", "
<< rhs.columns_[c][r]
<< " (diff=" << diff << ") "
<< vcl_endl;
if (verbosity_<=1)
return false; // Don't bother checking any more elements
else
values_different = true; // Proceed to check all elements
}
}
}
if (values_different) return false;
// Passed all tests, table is identical to this one.
return true;
}
//========================================================================
// Is another table different from this one?
//========================================================================
bool mbl_table::operator!=(const mbl_table& rhs) const
{
return !(*this==rhs);
}
//========================================================================
// Set the tolerance used to determine whether table entries are equal.
//========================================================================
void mbl_table::set_tolerance(const double& tol,
const bool& fract/*=false*/)
{
tolerance_ = tol;
fractional_tolerance_ = fract;
}
//========================================================================
// Set the level of verbosity used for error output.
//========================================================================
void mbl_table::set_verbosity(const int& v)
{
verbosity_ = v;
}
|