diff options
author | Konstantin Ritt <ritt.ks@gmail.com> | 2015-09-14 02:35:20 +0400 |
---|---|---|
committer | Konstantin Ritt <ritt.ks@gmail.com> | 2015-09-16 13:35:56 +0000 |
commit | 7a0a5edcc747bdef71f11638a8c3c47fb31f8be6 (patch) | |
tree | 50f97c613097a084abffa7880fd252c5b0b74071 /src/bluetooth/qbluetoothlocaldevice_bluez.cpp | |
parent | ff59294c0d7a7464870a7a7d3bc1449c7a7695a7 (diff) |
[Bluetooth] Optimize loops
Decrease complexity from O(N+N*logN) to just O(N) for cases like
foreach (Key key, map.keys())
Value value = map.value(key);
, by rewriting with use of iterators.
Change-Id: I81f1334797f16b624293fcebdee885b2be3c89f1
Reviewed-by: Alex Blasche <alexander.blasche@theqtcompany.com>
Diffstat (limited to 'src/bluetooth/qbluetoothlocaldevice_bluez.cpp')
-rw-r--r-- | src/bluetooth/qbluetoothlocaldevice_bluez.cpp | 48 |
1 files changed, 33 insertions, 15 deletions
diff --git a/src/bluetooth/qbluetoothlocaldevice_bluez.cpp b/src/bluetooth/qbluetoothlocaldevice_bluez.cpp index cae21e1b..d2ec70b9 100644 --- a/src/bluetooth/qbluetoothlocaldevice_bluez.cpp +++ b/src/bluetooth/qbluetoothlocaldevice_bluez.cpp @@ -213,17 +213,22 @@ QList<QBluetoothHostInfo> QBluetoothLocalDevice::allDevices() if (reply.isError()) return localDevices; - foreach (const QDBusObjectPath &path, reply.value().keys()) { - const InterfaceList ifaceList = reply.value().value(path); - foreach (const QString &iface, ifaceList.keys()) { + ManagedObjectList managedObjectList = reply.value(); + for (ManagedObjectList::const_iterator it = managedObjectList.constBegin(); it != managedObjectList.constEnd(); ++it) { + const InterfaceList &ifaceList = it.value(); + + for (InterfaceList::const_iterator jt = ifaceList.constBegin(); jt != ifaceList.constEnd(); ++jt) { + const QString &iface = jt.key(); + const QVariantMap &ifaceValues = jt.value(); + if (iface == QStringLiteral("org.bluez.Adapter1")) { QBluetoothHostInfo hostInfo; - const QString temp = ifaceList.value(iface).value(QStringLiteral("Address")).toString(); + const QString temp = ifaceValues.value(QStringLiteral("Address")).toString(); hostInfo.setAddress(QBluetoothAddress(temp)); if (hostInfo.address().isNull()) continue; - hostInfo.setName(ifaceList.value(iface).value(QStringLiteral("Name")).toString()); + hostInfo.setName(ifaceValues.value(QStringLiteral("Name")).toString()); localDevices.append(hostInfo); } } @@ -447,10 +452,13 @@ void QBluetoothLocalDevicePrivate::requestPairingBluez5(const QBluetoothAddress return; } + ManagedObjectList managedObjectList = reply.value(); + for (ManagedObjectList::const_iterator it = managedObjectList.constBegin(); it != managedObjectList.constEnd(); ++it) { + const QDBusObjectPath &path = it.key(); + const InterfaceList &ifaceList = it.value(); - foreach (const QDBusObjectPath &path, reply.value().keys()) { - const InterfaceList ifaceList = reply.value().value(path); - foreach (const QString &iface, ifaceList.keys()) { + for (InterfaceList::const_iterator jt = ifaceList.constBegin(); jt != ifaceList.constEnd(); ++jt) { + const QString &iface = jt.key(); if (iface == QStringLiteral("org.bluez.Device1")) { @@ -603,9 +611,13 @@ QBluetoothLocalDevice::Pairing QBluetoothLocalDevice::pairingStatus( if (reply.isError()) return Unpaired; - foreach (const QDBusObjectPath &path, reply.value().keys()) { - const InterfaceList ifaceList = reply.value().value(path); - foreach (const QString &iface, ifaceList.keys()) { + ManagedObjectList managedObjectList = reply.value(); + for (ManagedObjectList::const_iterator it = managedObjectList.constBegin(); it != managedObjectList.constEnd(); ++it) { + const QDBusObjectPath &path = it.key(); + const InterfaceList &ifaceList = it.value(); + + for (InterfaceList::const_iterator jt = ifaceList.constBegin(); jt != ifaceList.constEnd(); ++jt) { + const QString &iface = jt.key(); if (iface == QStringLiteral("org.bluez.Device1")) { @@ -670,9 +682,16 @@ void QBluetoothLocalDevicePrivate::connectDeviceChanges() return; OrgFreedesktopDBusPropertiesInterface *monitor = 0; - foreach (const QDBusObjectPath &path, reply.value().keys()) { - const InterfaceList ifaceList = reply.value().value(path); - foreach (const QString &iface, ifaceList.keys()) { + + ManagedObjectList managedObjectList = reply.value(); + for (ManagedObjectList::const_iterator it = managedObjectList.constBegin(); it != managedObjectList.constEnd(); ++it) { + const QDBusObjectPath &path = it.key(); + const InterfaceList &ifaceList = it.value(); + + for (InterfaceList::const_iterator jt = ifaceList.constBegin(); jt != ifaceList.constEnd(); ++jt) { + const QString &iface = jt.key(); + const QVariantMap &ifaceValues = jt.value(); + if (iface == QStringLiteral("org.bluez.Device1")) { monitor = new OrgFreedesktopDBusPropertiesInterface(QStringLiteral("org.bluez"), path.path(), @@ -681,7 +700,6 @@ void QBluetoothLocalDevicePrivate::connectDeviceChanges() SLOT(PropertiesChanged(QString,QVariantMap,QStringList))); deviceChangeMonitors.insert(path.path(), monitor); - const QVariantMap ifaceValues = ifaceList.value(QStringLiteral("org.bluez.Device1")); if (ifaceValues.value(QStringLiteral("Connected"), false).toBool()) { QBluetoothAddress address(ifaceValues.value(QStringLiteral("Address")).toString()); connectedDevicesSet.insert(address); |