From 09ad6d7877c64cffd95f1a68eb415d227a7b3747 Mon Sep 17 00:00:00 2001 From: Olly Betts Date: Mon, 16 Jul 2018 11:35:45 +1200 Subject: [PATCH] Fix handling showing both a parent and child survey In this case it makes most sense to show all child surveys of the parent, but we actually showed a slightly arbitrary subset of the child surveys of the parent. --- src/model.cc | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/model.h | 7 ++++--- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/model.cc b/src/model.cc index 7553b951..6d779321 100644 --- a/src/model.cc +++ b/src/model.cc @@ -467,6 +467,56 @@ void Model::CentreDataset(const Vector3& vmin) } } +void +SurveyFilter::add(const wxString& name) +{ + auto it = filters.lower_bound(name); + if (it != filters.end()) { + // It's invalid to add a survey which is already present. + assert(*it != name); + // Check if a survey prefixing name is visible. + if (name.StartsWith(*it) && name[it->size()] == separator) { + redundant_filters.insert(name); + return; + } + } + while (it != filters.begin()) { + --it; + const wxString& s = *it; + if (s.size() <= name.size()) break; + if (s.StartsWith(name) && s[name.size()] == separator) { + redundant_filters.insert(s); + it = filters.erase(it); + } + } + filters.insert(name); +} + +void +SurveyFilter::remove(const wxString& name) +{ + if (filters.erase(name) == 0) { + redundant_filters.erase(name); + return; + } + if (redundant_filters.empty()) { + return; + } + auto it = redundant_filters.upper_bound(name); + while (it != redundant_filters.begin()) { + --it; + // Check if a survey prefixed by name should be made visible. + const wxString& s = *it; + if (s.size() <= name.size()) { + break; + } + if (!(s.StartsWith(name) && s[name.size()] == separator)) + break; + filters.insert(s); + it = redundant_filters.erase(it); + } +} + bool SurveyFilter::CheckVisible(const wxString& name) const { diff --git a/src/model.h b/src/model.h index e5846935..c59e8f9e 100644 --- a/src/model.h +++ b/src/model.h @@ -113,16 +113,17 @@ class traverse : public vector { class SurveyFilter { std::set> filters; + std::set> redundant_filters; wxChar separator = 0; public: SurveyFilter() {} - void add(const wxString& survey) { filters.insert(survey); } + void add(const wxString& survey); - void remove(const wxString& survey) { filters.erase(survey); } + void remove(const wxString& survey); - void clear() { filters.clear(); } + void clear() { filters.clear(); redundant_filters.clear(); } bool empty() const { return filters.empty(); } -- 2.11.4.GIT