Skip to content
Snippets Groups Projects
Commit d64883e8 authored by Giovanni La Mura's avatar Giovanni La Mura
Browse files

Use vector clear() function to destroy vector elements

parent 3c6365e2
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment