Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix distances reader #35

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions ripser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,32 @@ template <typename T> T read(std::istream& input_stream) {
return result;
}

template <typename T> bool next_value_stream(T& s, value_t& next_value) {
value_t value;
std::string tmp;
bool valid = false;

if (s >> value || !s.eof()) {
/* Any input stream fails to convert correctly when Inf or NaN is
* present, this checks if it encounters this kind of values
* if stof fails to find any valid convertion, it throws an
* invalid_argument exception*/
if (s.fail()) {
s.clear();
s >> tmp;
next_value = std::stof(tmp.c_str());
} else {
next_value = value;
}

/* This check prevent encountering any zero values in distances matrices */
if (next_value != 0) valid = true;
}

s.ignore();
return valid;
}

compressed_lower_distance_matrix read_point_cloud(std::istream& input_stream) {
std::vector<std::vector<value_t>> points;

Expand Down Expand Up @@ -892,21 +918,15 @@ sparse_distance_matrix read_sparse_distance_matrix(std::istream& input_stream) {
compressed_lower_distance_matrix read_lower_distance_matrix(std::istream& input_stream) {
std::vector<value_t> distances;
value_t value;
while (input_stream >> value) {
distances.push_back(value);
input_stream.ignore();
}
while (next_value_stream(input_stream, value)) { distances.push_back(value); }

return compressed_lower_distance_matrix(std::move(distances));
}

compressed_lower_distance_matrix read_upper_distance_matrix(std::istream& input_stream) {
std::vector<value_t> distances;
value_t value;
while (input_stream >> value) {
distances.push_back(value);
input_stream.ignore();
}
while (next_value_stream(input_stream, value)) { distances.push_back(value); }

return compressed_lower_distance_matrix(compressed_upper_distance_matrix(std::move(distances)));
}
Expand All @@ -918,10 +938,7 @@ compressed_lower_distance_matrix read_distance_matrix(std::istream& input_stream
value_t value;
for (int i = 0; std::getline(input_stream, line); ++i) {
std::istringstream s(line);
for (int j = 0; j < i && s >> value; ++j) {
distances.push_back(value);
s.ignore();
}
for (int j = 0; j < i && next_value_stream(s, value); ++j) { distances.push_back(value); }
}

return compressed_lower_distance_matrix(std::move(distances));
Expand Down