From bd785d8bf4dbf2099029fa72cbaa04aa6e19b67b Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Sat, 3 Sep 2016 18:26:48 -0700 Subject: [PATCH] RecentEntries: Extract more algorithms. --- firmware/application/ais_app.cpp | 2 +- firmware/application/ert_app.cpp | 2 +- firmware/application/recent_entries.hpp | 66 ++++++++++++------------- firmware/application/tpms_app.cpp | 2 +- 4 files changed, 36 insertions(+), 36 deletions(-) diff --git a/firmware/application/ais_app.cpp b/firmware/application/ais_app.cpp index 049acf71..5c50dbac 100644 --- a/firmware/application/ais_app.cpp +++ b/firmware/application/ais_app.cpp @@ -336,7 +336,7 @@ void AISAppView::on_packet(const ais::Packet& packet) { logger->on_packet(packet); } - auto& entry = recent.on_packet(packet.source_id()); + auto& entry = ::on_packet(recent, packet.source_id()); entry.update(packet); recent_entries_view.set_dirty(); diff --git a/firmware/application/ert_app.cpp b/firmware/application/ert_app.cpp index fcfc9b1e..8c1b305f 100644 --- a/firmware/application/ert_app.cpp +++ b/firmware/application/ert_app.cpp @@ -150,7 +150,7 @@ void ERTAppView::on_packet(const ert::Packet& packet) { } if( packet.crc_ok() ) { - auto& entry = recent.on_packet({ packet.id(), packet.commodity_type() }); + auto& entry = ::on_packet(recent, ERTRecentEntry::Key { packet.id(), packet.commodity_type() }); entry.update(packet); recent_entries_view.set_dirty(); } diff --git a/firmware/application/recent_entries.hpp b/firmware/application/recent_entries.hpp index a06038e0..22576588 100644 --- a/firmware/application/recent_entries.hpp +++ b/firmware/application/recent_entries.hpp @@ -41,38 +41,38 @@ public: using ContainerType = std::list; using const_reference = typename ContainerType::const_reference; using const_iterator = typename ContainerType::const_iterator; - - EntryType& on_packet(const Key key) { - auto matching_recent = find(key); - if( matching_recent != std::end(*this) ) { - // Found within. Move to front of list, increment counter. - this->push_front(*matching_recent); - this->erase(matching_recent); - } else { - this->emplace_front(key); - truncate_entries(); - } - - return this->front(); - } - - const_iterator find(const Key key) const { - return std::find_if( - std::begin(*this), std::end(*this), - [key](const EntryType& e) { return e.key() == key; } - ); - } - -private: - const size_t entries_max = 64; - - void truncate_entries() { - while(this->size() > entries_max) { - this->pop_back(); - } - } }; +template +typename ContainerType::const_iterator find(const ContainerType& entries, const Key key) { + return std::find_if( + std::begin(entries), std::end(entries), + [key](typename ContainerType::const_reference e) { return e.key() == key; } + ); +} + +template +static void truncate_entries(ContainerType& entries, const size_t entries_max = 64) { + while(entries.size() > entries_max) { + entries.pop_back(); + } +} + +template +typename ContainerType::reference on_packet(ContainerType& entries, const Key key) { + auto matching_recent = find(entries, key); + if( matching_recent != std::end(entries) ) { + // Found within. Move to front of list, increment counter. + entries.push_front(*matching_recent); + entries.erase(matching_recent); + } else { + entries.emplace_front(key); + truncate_entries(entries); + } + + return entries.front(); +} + template static std::pair range_around( const ContainerType& entries, @@ -142,7 +142,7 @@ public: draw_header(target_rect, painter, style_header); target_rect.pos.y += target_rect.height(); - auto selected = recent.find(selected_key); + auto selected = find(recent, selected_key); if( selected == std::end(recent) ) { selected = std::begin(recent); } @@ -171,7 +171,7 @@ public: bool on_key(const ui::KeyEvent event) override { if( event == ui::KeyEvent::Select ) { if( on_select ) { - const auto selected = recent.find(selected_key); + const auto selected = find(recent, selected_key); if( selected != std::end(recent) ) { on_select(*selected); return true; @@ -193,7 +193,7 @@ private: EntryKey selected_key = Entry::invalid_key; void advance(const int32_t amount) { - auto selected = recent.find(selected_key); + auto selected = find(recent, selected_key); if( selected == std::end(recent) ) { if( recent.empty() ) { selected_key = Entry::invalid_key; diff --git a/firmware/application/tpms_app.cpp b/firmware/application/tpms_app.cpp index a00ff16f..c435bce1 100644 --- a/firmware/application/tpms_app.cpp +++ b/firmware/application/tpms_app.cpp @@ -199,7 +199,7 @@ void TPMSAppView::on_packet(const tpms::Packet& packet) { const auto reading_opt = packet.reading(); if( reading_opt.is_valid() ) { const auto reading = reading_opt.value(); - auto& entry = recent.on_packet({ reading.type(), reading.id() }); + auto& entry = ::on_packet(recent, TPMSRecentEntry::Key { reading.type(), reading.id() }); entry.update(reading); recent_entries_view.set_dirty(); }