From d64883e800d73fe702f20cd7c7efe59dc5528693 Mon Sep 17 00:00:00 2001 From: Giovanni La Mura <giovanni.lamura@inaf.it> Date: Wed, 29 May 2024 20:33:43 +0200 Subject: [PATCH] Use vector clear() function to destroy vector elements --- src/libnptm/file_io.cpp | 108 +++++++++++++++++++++++----------------- 1 file changed, 61 insertions(+), 47 deletions(-) diff --git a/src/libnptm/file_io.cpp b/src/libnptm/file_io.cpp index 00afb199..28efaba4 100644 --- a/src/libnptm/file_io.cpp +++ b/src/libnptm/file_io.cpp @@ -277,9 +277,10 @@ VirtualAsciiFile::VirtualAsciiFile(const mixMPI *mpidata, int rr) { VirtualAsciiFile::~VirtualAsciiFile() { // is it necessary to pop them out one by one? isn't there the dedicated method of std::vector to clean the vector? // besides, shouldn't this be done anyway by the destructor of std:vector? - while (!_file_lines->size() > 0) { - _file_lines->pop_back(); - } + _file_lines->clear(); + // while (_file_lines->size() > 0) { + // _file_lines->pop_back(); + // } if (_file_lines != NULL) delete _file_lines; } @@ -300,15 +301,17 @@ void VirtualAsciiFile::append_line(const string& line) { int VirtualAsciiFile::append_to_disk(const std::string& file_name) { // dump to disk the contents of the virtualasciifile, appending at the end of the given file_name int result = 0; - fstream output_file; - output_file.open(file_name, ios::app); - if (output_file.is_open()) { - for (vector<string>::iterator it = _file_lines->begin(); it != _file_lines->end(); ++it) { - output_file << *it; + if (_file_lines->size() > 0) { + fstream output_file; + output_file.open(file_name, ios::app); + if (output_file.is_open()) { + for (vector<string>::iterator it = _file_lines->begin(); it != _file_lines->end(); ++it) { + output_file << *it; + } + output_file.close(); + } else { + result = 1; } - output_file.close(); - } else { - result = 1; } return result; } @@ -334,15 +337,17 @@ int VirtualAsciiFile::insert(int32_t position, VirtualAsciiFile& rhs, int32_t st int VirtualAsciiFile::write_to_disk(const std::string& file_name) { // dump to disk the contents of the virtualasciifile, replacing the given file_name int result = 0; - fstream output_file; - output_file.open(file_name, ios::out); - if (output_file.is_open()) { - for (vector<string>::iterator it = _file_lines->begin(); it != _file_lines->end(); ++it) { - output_file << *it; + if (_file_lines->size() > 0) { + fstream output_file; + output_file.open(file_name, ios::out); + if (output_file.is_open()) { + for (vector<string>::iterator it = _file_lines->begin(); it != _file_lines->end(); ++it) { + output_file << *it; + } + output_file.close(); + } else { + result = 1; } - output_file.close(); - } else { - result = 1; } return result; } @@ -354,13 +359,15 @@ void VirtualAsciiFile::mpisend(const mixMPI *mpidata) { int32_t num_lines = _file_lines->size(); MPI_Send(&num_lines, 1, MPI_INT32_T, 0, 10, MPI_COMM_WORLD); // now loop over data to send - int32_t mysize; - for (vector<string>::iterator it = _file_lines->begin(); it != _file_lines->end(); ++it) { - // send the length of each string - mysize = (int32_t) it->size(); - MPI_Send(&mysize, 1, MPI_INT32_T, 0, 10, MPI_COMM_WORLD); - // send the string itself - MPI_Send(it->c_str(), mysize+1, MPI_CHAR, 0, 10, MPI_COMM_WORLD); + if (num_lines>0) { + int32_t mysize; + for (vector<string>::iterator it = _file_lines->begin(); it != _file_lines->end(); ++it) { + // send the length of each string + mysize = (int32_t) it->size(); + MPI_Send(&mysize, 1, MPI_INT32_T, 0, 10, MPI_COMM_WORLD); + // send the string itself + MPI_Send(it->c_str(), mysize+1, MPI_CHAR, 0, 10, MPI_COMM_WORLD); + } } } #endif @@ -483,9 +490,10 @@ VirtualBinaryFile::~VirtualBinaryFile() { // for (vector<VirtualBinaryLine>::iterator it = _file_lines->begin(); it != _file_lines->end(); ++it) { // delete it; // } - while (!_file_lines->size() > 0) { - _file_lines->pop_back(); - } + _file_lines->clear(); + // while (_file_lines->size() > 0) { + // _file_lines->pop_back(); + // } if (_file_lines != NULL) delete _file_lines; } @@ -506,15 +514,17 @@ void VirtualBinaryFile::append_line(const VirtualBinaryLine& line) { int VirtualBinaryFile::append_to_disk(const std::string& file_name) { // dump to disk the contents of the virtualasciifile, appending at the end of the given file_name int result = 0; - fstream output_file; - output_file.open(file_name, ios::app | ios::binary); - if (output_file.is_open()) { - for (vector<VirtualBinaryLine>::iterator it = _file_lines->begin(); it != _file_lines->end(); ++it) { - output_file.write(it->_data_pointer, it->_data_size); + if (_file_lines->size() > 0) { + fstream output_file; + output_file.open(file_name, ios::app | ios::binary); + if (output_file.is_open()) { + for (vector<VirtualBinaryLine>::iterator it = _file_lines->begin(); it != _file_lines->end(); ++it) { + output_file.write(it->_data_pointer, it->_data_size); + } + output_file.close(); + } else { + result = 1; } - output_file.close(); - } else { - result = 1; } return result; } @@ -541,15 +551,17 @@ int VirtualBinaryFile::append_to_disk(const std::string& file_name) { int VirtualBinaryFile::write_to_disk(const std::string& file_name) { // dump to disk the contents of the virtualasciifile, replacing the given file_name int result = 0; - fstream output_file; - output_file.open(file_name, ios::out | ios::binary); - if (output_file.is_open()) { - for (vector<VirtualBinaryLine>::iterator it = _file_lines->begin(); it != _file_lines->end(); ++it) { - output_file.write(it->_data_pointer, it->_data_size); + if (_file_lines->size() > 0) { + fstream output_file; + output_file.open(file_name, ios::out | ios::binary); + if (output_file.is_open()) { + for (vector<VirtualBinaryLine>::iterator it = _file_lines->begin(); it != _file_lines->end(); ++it) { + output_file.write(it->_data_pointer, it->_data_size); + } + output_file.close(); + } else { + result = 1; } - output_file.close(); - } else { - result = 1; } return result; } @@ -561,8 +573,10 @@ void VirtualBinaryFile::mpisend(const mixMPI *mpidata) { int32_t num_lines = _file_lines->size(); MPI_Send(&num_lines, 1, MPI_INT32_T, 0, 10, MPI_COMM_WORLD); // now loop over data to send - for (vector<VirtualBinaryLine>::iterator it = _file_lines->begin(); it != _file_lines->end(); ++it) { - it->mpisend(mpidata); + if (num_lines>0) { + for (vector<VirtualBinaryLine>::iterator it = _file_lines->begin(); it != _file_lines->end(); ++it) { + it->mpisend(mpidata); + } } } #endif -- GitLab