From 23337b3b21cd7b07473f31e667242f96cae6749c Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Mon, 23 May 2022 22:32:22 -0500 Subject: [PATCH] Use common map utility to get radar product record --- .../scwx/qt/manager/radar_product_manager.cpp | 45 +++---------------- wxdata/include/scwx/util/map.hpp | 12 +++-- 2 files changed, 14 insertions(+), 43 deletions(-) diff --git a/scwx-qt/source/scwx/qt/manager/radar_product_manager.cpp b/scwx-qt/source/scwx/qt/manager/radar_product_manager.cpp index 425f24da..970993d8 100644 --- a/scwx-qt/source/scwx/qt/manager/radar_product_manager.cpp +++ b/scwx-qt/source/scwx/qt/manager/radar_product_manager.cpp @@ -1,7 +1,8 @@ #include #include -#include #include +#include +#include #include #include @@ -66,9 +67,6 @@ public: } ~RadarProductManagerImpl() = default; - static std::shared_ptr - GetRadarProductRecord(RadarProductRecordMap& map, - std::chrono::system_clock::time_point time); std::shared_ptr GetLevel2ProductRecord(std::chrono::system_clock::time_point time); std::shared_ptr @@ -311,46 +309,13 @@ void RadarProductManagerImpl::LoadNexradFile( }); } -std::shared_ptr -RadarProductManagerImpl::GetRadarProductRecord( - RadarProductRecordMap& map, std::chrono::system_clock::time_point time) -{ - std::shared_ptr record = nullptr; - - // TODO: Round to minutes - - // Find the first product record greater than the time requested - auto it = map.upper_bound(time); - - // A product record with a time greater was found - if (it != map.cend()) - { - // Are there product records prior to this record? - if (it != map.cbegin()) - { - // Get the product record immediately preceding, this the record we are - // looking for - record = (--it)->second; - } - } - else if (map.size() > 0) - { - // A product record with a time greater was not found. If it exists, it - // must be the last record. - record = map.rbegin()->second; - } - - return record; -} - std::shared_ptr RadarProductManagerImpl::GetLevel2ProductRecord( std::chrono::system_clock::time_point time) { - std::shared_ptr record = - GetRadarProductRecord(level2ProductRecords_, time); - // TODO: Round to minutes + std::shared_ptr record = + util::GetBoundedElementValue(level2ProductRecords_, time); // Does the record contain the time we are looking for? if (record != nullptr && (time < record->level2_file()->start_time() || @@ -372,7 +337,7 @@ RadarProductManagerImpl::GetLevel3ProductRecord( if (it != level3ProductRecords_.cend()) { - record = GetRadarProductRecord(it->second, time); + record = util::GetBoundedElementValue(it->second, time); } return record; diff --git a/wxdata/include/scwx/util/map.hpp b/wxdata/include/scwx/util/map.hpp index 12ee96b5..e60000ba 100644 --- a/wxdata/include/scwx/util/map.hpp +++ b/wxdata/include/scwx/util/map.hpp @@ -8,10 +8,10 @@ namespace scwx namespace util { -template -std::optional GetBoundedElement(std::map& map, Key key) +template> +ReturnType GetBoundedElement(std::map& map, Key key) { - std::optional element = std::nullopt; + ReturnType element; // Find the first element greater than the key requested auto it = map.upper_bound(key); @@ -37,5 +37,11 @@ std::optional GetBoundedElement(std::map& map, Key key) return element; } +template +inline T GetBoundedElementValue(std::map& map, Key key) +{ + return GetBoundedElement(map, key); +} + } // namespace util } // namespace scwx