diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2018-06-16 14:06:35 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2018-06-17 06:53:13 +0900 |
commit | 1f654050fa7e461166ee4d8b46d66b4ca91f31d4 (patch) | |
tree | f6158e8fc750ccd5de64b15a04a242f72ca3cd67 /core/divecomputer.cpp | |
parent | 8e8cd7a8d9304b96cc80fbd2ea63e28d9f7595a3 (diff) | |
download | subsurface-1f654050fa7e461166ee4d8b46d66b4ca91f31d4.tar.gz |
Dive computers: turn QMultiMap into sorted vector
The list of known dive computers was stored in a multi-map indexed
by the device name. Turn this into a sorted QVector. Thus, no
map-to-list conversion is needed in the device editing dialog,
which distinctly simplifies the code.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core/divecomputer.cpp')
-rw-r--r-- | core/divecomputer.cpp | 70 |
1 files changed, 31 insertions, 39 deletions
diff --git a/core/divecomputer.cpp b/core/divecomputer.cpp index 79edfc494..e9de3f64b 100644 --- a/core/divecomputer.cpp +++ b/core/divecomputer.cpp @@ -19,6 +19,11 @@ bool DiveComputerNode::operator!=(const DiveComputerNode &a) const return !(*this == a); } +bool DiveComputerNode::operator<(const DiveComputerNode &a) const +{ + return std::tie(model, deviceId) < std::tie(a.model, a.deviceId); +} + bool DiveComputerNode::changesValues(const DiveComputerNode &b) const { if (model != b.model || deviceId != b.deviceId) { @@ -32,27 +37,23 @@ bool DiveComputerNode::changesValues(const DiveComputerNode &b) const const DiveComputerNode *DiveComputerList::getExact(const QString &m, uint32_t d) { - for (QMap<QString, DiveComputerNode>::iterator it = dcMap.find(m); it != dcMap.end() && it.key() == m; ++it) - if (it->deviceId == d) - return &*it; - return NULL; + auto it = std::lower_bound(dcs.begin(), dcs.end(), DiveComputerNode { m, d , {}, {}, {} } ); + return it != dcs.end() && it->model == m && it->deviceId == d ? &*it : NULL; } const DiveComputerNode *DiveComputerList::get(const QString &m) { - QMap<QString, DiveComputerNode>::iterator it = dcMap.find(m); - if (it != dcMap.end()) - return &*it; - return NULL; + auto it = std::lower_bound(dcs.begin(), dcs.end(), DiveComputerNode { m, 0 , {}, {}, {} } ); + return it != dcs.end() && it->model == m ? &*it : NULL; } void DiveComputerNode::showchanges(const QString &n, const QString &s, const QString &f) const { - if (nickName != n) + if (nickName != n && !n.isEmpty()) qDebug("new nickname %s for DC model %s deviceId 0x%x", qPrintable(n), qPrintable(model), deviceId); - if (serialNumber != s) + if (serialNumber != s && !s.isEmpty()) qDebug("new serial number %s for DC model %s deviceId 0x%x", qPrintable(s), qPrintable(model), deviceId); - if (firmware != f) + if (firmware != f && !f.isEmpty()) qDebug("new firmware version %s for DC model %s deviceId 0x%x", qPrintable(f), qPrintable(model), deviceId); } @@ -60,28 +61,21 @@ void DiveComputerList::addDC(QString m, uint32_t d, QString n, QString s, QStrin { if (m.isEmpty() || d == 0) return; - const DiveComputerNode *existNode = this->getExact(m, d); - - if (existNode) { - // Update any non-existent fields from the old entry - if (n.isEmpty()) - n = existNode->nickName; - if (s.isEmpty()) - s = existNode->serialNumber; - if (f.isEmpty()) - f = existNode->firmware; - - // Do all the old values match? - if (n == existNode->nickName && s == existNode->serialNumber && f == existNode->firmware) - return; - + auto it = std::lower_bound(dcs.begin(), dcs.end(), DiveComputerNode { m, d , {}, {}, {} } ); + if (it != dcs.end() && it->model == m && it->deviceId == d) { // debugging: show changes if (verbose) - existNode->showchanges(n, s, f); - dcMap.remove(m, *existNode); + it->showchanges(n, s, f); + // Update any non-existent fields from the old entry + if (!n.isEmpty()) + it->nickName = n; + if (!s.isEmpty()) + it->serialNumber = s; + if (!f.isEmpty()) + it->firmware = f; + } else { + dcs.insert(it, DiveComputerNode { m, d, s, f, n }); } - - dcMap.insert(m, { m, d, s, f, n }); } extern "C" void create_device_node(const char *model, uint32_t deviceid, const char *serial, const char *firmware, const char *nickname) @@ -89,7 +83,7 @@ extern "C" void create_device_node(const char *model, uint32_t deviceid, const c dcList.addDC(model, deviceid, nickname, serial, firmware); } -extern "C" bool compareDC(const DiveComputerNode &a, const DiveComputerNode &b) +static bool compareDCById(const DiveComputerNode &a, const DiveComputerNode &b) { return a.deviceId < b.deviceId; } @@ -98,10 +92,9 @@ extern "C" void call_for_each_dc (void *f, void (*callback)(void *, const char * const char *, const char *, const char *), bool select_only) { - QList<DiveComputerNode> values = dcList.dcMap.values(); - qSort(values.begin(), values.end(), compareDC); - for (int i = 0; i < values.size(); i++) { - const DiveComputerNode *node = &values.at(i); + QVector<DiveComputerNode> values = dcList.dcs; + std::sort(values.begin(), values.end(), compareDCById); + for (const DiveComputerNode &node: values) { bool found = false; if (select_only) { int j; @@ -111,7 +104,7 @@ extern "C" void call_for_each_dc (void *f, void (*callback)(void *, const char * if (!d->selected) continue; for_each_dc(d, dc) { - if (dc->deviceid == node->deviceId) { + if (dc->deviceid == node.deviceId) { found = true; break; } @@ -123,12 +116,11 @@ extern "C" void call_for_each_dc (void *f, void (*callback)(void *, const char * found = true; } if (found) - callback(f, qPrintable(node->model), node->deviceId, qPrintable(node->nickName), - qPrintable(node->serialNumber), qPrintable(node->firmware)); + callback(f, qPrintable(node.model), node.deviceId, qPrintable(node.nickName), + qPrintable(node.serialNumber), qPrintable(node.firmware)); } } - extern "C" int is_default_dive_computer(const char *vendor, const char *product) { auto dc = SettingsObjectWrapper::instance()->dive_computer_settings; |