From 41972a06c0d9fb07db4a4789a9897ebdfec4dc80 Mon Sep 17 00:00:00 2001 From: Vasileios Karakasis <karakasis@cscs.ch> Date: Mon, 18 Apr 2016 11:31:09 +0200 Subject: [PATCH] Reject SWC input if branches are not contiguously numbered. --- src/algorithms.hpp | 1 + src/swcio.cpp | 29 ++++++++++++++++++++--------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/algorithms.hpp b/src/algorithms.hpp index d627b755..25210915 100644 --- a/src/algorithms.hpp +++ b/src/algorithms.hpp @@ -3,6 +3,7 @@ #include <algorithm> #include <numeric> #include <type_traits> +#include <vector> /* * Some simple wrappers around stl algorithms to improve readability of code diff --git a/src/swcio.cpp b/src/swcio.cpp index 2c3fe4d2..796972a6 100644 --- a/src/swcio.cpp +++ b/src/swcio.cpp @@ -4,6 +4,7 @@ #include <sstream> #include <unordered_set> +#include <algorithms.hpp> #include <swcio.hpp> namespace nest { @@ -183,21 +184,21 @@ swc_record_range_clean::swc_record_range_clean(std::istream &is) bool needsort = false; swc_record curr_record; - for (auto c : swc_get_records<swc_io_raw>(is)) { - if (c.parent() == -1 && ++num_trees > 1) { + for (auto r : swc_get_records<swc_io_raw>(is)) { + if (r.parent() == -1 && ++num_trees > 1) { // only a single tree is allowed break; } - auto inserted = ids.insert(c.id()); + auto inserted = ids.insert(r.id()); if (inserted.second) { // not a duplicate; insert record - records_.push_back(c); - if (!needsort && c.id() < last_id) { + records_.push_back(r); + if (!needsort && r.id() < last_id) { needsort = true; } - last_id = c.id(); + last_id = r.id(); } } @@ -208,13 +209,23 @@ swc_record_range_clean::swc_record_range_clean(std::istream &is) // Renumber records if necessary std::map<swc_record::id_type, swc_record::id_type> idmap; swc_record::id_type next_id = 0; - for (auto &c : records_) { - if (c.id() != next_id) { - c.renumber(next_id, idmap); + for (auto &r : records_) { + if (r.id() != next_id) { + r.renumber(next_id, idmap); } ++next_id; } + + // Reject if branches are not contiguously numbered + std::vector<swc_record::id_type> parent_list = { 0 }; + for (std::size_t i = 1; i < records_.size(); ++i) { + parent_list.push_back(records_[i].parent()); + } + + if (!nest::mc::algorithms::is_contiguously_numbered(parent_list)) { + throw swc_parse_error("branches are not contiguously numbered", 0); + } } } // namespace io -- GitLab