RecentEntries: Extract more algorithms.

This commit is contained in:
Jared Boone 2016-09-03 18:26:48 -07:00
parent c8f7863c83
commit bd785d8bf4
4 changed files with 36 additions and 36 deletions

View File

@ -336,7 +336,7 @@ void AISAppView::on_packet(const ais::Packet& packet) {
logger->on_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); entry.update(packet);
recent_entries_view.set_dirty(); recent_entries_view.set_dirty();

View File

@ -150,7 +150,7 @@ void ERTAppView::on_packet(const ert::Packet& packet) {
} }
if( packet.crc_ok() ) { 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); entry.update(packet);
recent_entries_view.set_dirty(); recent_entries_view.set_dirty();
} }

View File

@ -41,38 +41,38 @@ public:
using ContainerType = std::list<EntryType>; using ContainerType = std::list<EntryType>;
using const_reference = typename ContainerType::const_reference; using const_reference = typename ContainerType::const_reference;
using const_iterator = typename ContainerType::const_iterator; 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, typename Key>
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<typename ContainerType>
static void truncate_entries(ContainerType& entries, const size_t entries_max = 64) {
while(entries.size() > entries_max) {
entries.pop_back();
}
}
template<typename ContainerType, typename Key>
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<typename ContainerType> template<typename ContainerType>
static std::pair<typename ContainerType::const_iterator, typename ContainerType::const_iterator> range_around( static std::pair<typename ContainerType::const_iterator, typename ContainerType::const_iterator> range_around(
const ContainerType& entries, const ContainerType& entries,
@ -142,7 +142,7 @@ public:
draw_header(target_rect, painter, style_header); draw_header(target_rect, painter, style_header);
target_rect.pos.y += target_rect.height(); target_rect.pos.y += target_rect.height();
auto selected = recent.find(selected_key); auto selected = find(recent, selected_key);
if( selected == std::end(recent) ) { if( selected == std::end(recent) ) {
selected = std::begin(recent); selected = std::begin(recent);
} }
@ -171,7 +171,7 @@ public:
bool on_key(const ui::KeyEvent event) override { bool on_key(const ui::KeyEvent event) override {
if( event == ui::KeyEvent::Select ) { if( event == ui::KeyEvent::Select ) {
if( on_select ) { if( on_select ) {
const auto selected = recent.find(selected_key); const auto selected = find(recent, selected_key);
if( selected != std::end(recent) ) { if( selected != std::end(recent) ) {
on_select(*selected); on_select(*selected);
return true; return true;
@ -193,7 +193,7 @@ private:
EntryKey selected_key = Entry::invalid_key; EntryKey selected_key = Entry::invalid_key;
void advance(const int32_t amount) { void advance(const int32_t amount) {
auto selected = recent.find(selected_key); auto selected = find(recent, selected_key);
if( selected == std::end(recent) ) { if( selected == std::end(recent) ) {
if( recent.empty() ) { if( recent.empty() ) {
selected_key = Entry::invalid_key; selected_key = Entry::invalid_key;

View File

@ -199,7 +199,7 @@ void TPMSAppView::on_packet(const tpms::Packet& packet) {
const auto reading_opt = packet.reading(); const auto reading_opt = packet.reading();
if( reading_opt.is_valid() ) { if( reading_opt.is_valid() ) {
const auto reading = reading_opt.value(); 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); entry.update(reading);
recent_entries_view.set_dirty(); recent_entries_view.set_dirty();
} }