From b1707298b7c629143607a641c4a0b5b1c9028fe5 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Thu, 14 Jan 2016 11:49:19 -0800 Subject: [PATCH] Extract weird range-of-entries algorithm out of view. --- firmware/application/ais_app.cpp | 40 +++++++++++++++++++------------- firmware/application/ais_app.hpp | 7 +++++- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/firmware/application/ais_app.cpp b/firmware/application/ais_app.cpp index e69c6a00..83e8fc0a 100644 --- a/firmware/application/ais_app.cpp +++ b/firmware/application/ais_app.cpp @@ -161,6 +161,28 @@ void AISRecentEntries::truncate_entries() { } } +AISRecentEntries::RangeType AISRecentEntries::range_around( + ContainerType::const_iterator item, const size_t count +) const { + auto start = item; + auto end = item; + size_t i = 0; + + // Move start iterator toward first entry. + while( (start != std::begin(entries)) && (i < count / 2) ) { + std::advance(start, -1); + i++; + } + + // Move end iterator toward last entry. + while( (end != std::end(entries)) && (i < count) ) { + std::advance(end, 1); + i++; + } + + return { start, end }; +} + namespace ui { AISRecentEntriesView::AISRecentEntriesView( @@ -207,23 +229,9 @@ void AISRecentEntriesView::paint(Painter& painter) { selected = std::begin(recent); } - auto start = selected; - auto end = selected; - size_t i = 0; + auto range = recent.range_around(selected, visible_item_count); - // Move start iterator toward first entry. - while( (start != std::begin(recent)) && (i < visible_item_count / 2) ) { - std::advance(start, -1); - i++; - } - - // Move end iterator toward last entry. - while( (end != std::end(recent)) && (i < visible_item_count) ) { - std::advance(end, 1); - i++; - } - - for(auto p = start; p != end; p++) { + for(auto p = range.first; p != range.second; p++) { const auto& entry = *p; const auto is_selected_key = (selected_key == entry.mmsi); ais_list_item_draw(entry, target_rect, painter, s, (has_focus() && is_selected_key)); diff --git a/firmware/application/ais_app.hpp b/firmware/application/ais_app.hpp index 162d06d6..31f890a4 100644 --- a/firmware/application/ais_app.hpp +++ b/firmware/application/ais_app.hpp @@ -68,7 +68,8 @@ struct AISRecentEntry { class AISRecentEntries { public: using ContainerType = std::list; - + using RangeType = std::pair; + void on_packet(const ais::Packet& packet); ContainerType::const_reference front() const { @@ -89,6 +90,10 @@ public: return entries.empty(); } + RangeType range_around( + ContainerType::const_iterator, const size_t count + ) const; + private: ContainerType entries; const size_t entries_max = 64;