diff --git a/autotests/mapstest.cpp b/autotests/mapstest.cpp index 8d86b44..6bc0862 100644 --- a/autotests/mapstest.cpp +++ b/autotests/mapstest.cpp @@ -1,165 +1,155 @@ /* Copyright 2018 David Rosca This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #include "mapstest.h" #include "maps.h" #include "module.h" #include #include using namespace PulseAudioQt; void MapsTest::initTestCase() { } void MapsTest::cleanupTestCase() { } static pa_module_info *module_info_new() { pa_module_info *info = (pa_module_info*) calloc(1, sizeof(pa_module_info)); info->name = "name"; info->argument = "arg"; info->n_used = PA_INVALID_INDEX; info->proplist = pa_proplist_new(); return info; } static void module_info_free(pa_module_info *info) { pa_proplist_free(info->proplist); free(info); } void MapsTest::basicTest() { QObject p; ModuleMap map; pa_module_info *info = module_info_new(); info->index = 10; QCOMPARE(map.count(), 0); map.updateEntry(info, &p); QCOMPARE(map.count(), 1); map.updateEntry(info, &p); QCOMPARE(map.count(), 1); QObject *index10 = map.objectAt(0); QVERIFY(index10); QCOMPARE(map.indexOfObject(index10), 0); - // Insert 0 to the beginning (0, 10) + // Add 0 (10, 0) info->index = 0; map.updateEntry(info, &p); QCOMPARE(map.count(), 2); - QCOMPARE(map.objectAt(1), index10); + QCOMPARE(map.objectAt(0), index10); - QObject *index0 = map.objectAt(0); + QObject *index0 = map.objectAt(1); QVERIFY(index0); - QCOMPARE(map.indexOfObject(index0), 0); - QCOMPARE(map.indexOfObject(index10), 1); + QCOMPARE(map.indexOfObject(index0), 1); + QCOMPARE(map.indexOfObject(index10), 0); - // Insert 4 in the middle (0, 4, 10) + // Add 4, 15 (10, 0, 4, 15) info->index = 4; map.updateEntry(info, &p); - QCOMPARE(map.count(), 3); - QCOMPARE(map.objectAt(0), index0); - QCOMPARE(map.objectAt(2), index10); - - QObject *index4 = map.objectAt(1); - QVERIFY(index4); - - QCOMPARE(map.indexOfObject(index0), 0); - QCOMPARE(map.indexOfObject(index4), 1); - QCOMPARE(map.indexOfObject(index10), 2); - - // Insert 15 at the end (0, 4, 10, 15) info->index = 15; map.updateEntry(info, &p); + QCOMPARE(map.count(), 4); - QCOMPARE(map.objectAt(0), index0); - QCOMPARE(map.objectAt(1), index4); - QCOMPARE(map.objectAt(2), index10); + QCOMPARE(map.objectAt(0), index10); + QCOMPARE(map.objectAt(1), index0); + QObject *index4 = map.objectAt(2); + QVERIFY(index4); QObject *index15 = map.objectAt(3); QVERIFY(index15); - QCOMPARE(map.indexOfObject(index0), 0); - QCOMPARE(map.indexOfObject(index4), 1); - QCOMPARE(map.indexOfObject(index10), 2); + QCOMPARE(map.indexOfObject(index10), 0); + QCOMPARE(map.indexOfObject(index0), 1); + QCOMPARE(map.indexOfObject(index4), 2); QCOMPARE(map.indexOfObject(index15), 3); - // Remove 4 from the middle (0, 10, 15) + // Remove 4 (10, 0, 15) map.removeEntry(4); QCOMPARE(map.count(), 3); - QCOMPARE(map.objectAt(0), index0); - QCOMPARE(map.objectAt(1), index10); + QCOMPARE(map.objectAt(0), index10); + QCOMPARE(map.objectAt(1), index0); QCOMPARE(map.objectAt(2), index15); - // Remove 0 from the beginning (10, 15) + // Remove 0 (10, 15) map.removeEntry(0); QCOMPARE(map.count(), 2); QCOMPARE(map.objectAt(0), index10); QCOMPARE(map.objectAt(1), index15); - // Remove 15 from the end (10) + // Remove 15 (10) map.removeEntry(15); QCOMPARE(map.count(), 1); QCOMPARE(map.objectAt(0), index10); // Remove last () map.removeEntry(10); QCOMPARE(map.count(), 0); module_info_free(info); } void MapsTest::pendingRemovalsTest() { QObject p; ModuleMap map; pa_module_info *info = module_info_new(); info->index = 10; QCOMPARE(map.count(), 0); // 10 is not in map, should set pending removal map.removeEntry(10); // 10 is in pending removals, this should eat it and not add it into map map.updateEntry(info, &p); QCOMPARE(map.count(), 0); // No more pending removals, this should add it map.updateEntry(info, &p); QCOMPARE(map.count(), 1); module_info_free(info); } QTEST_MAIN(MapsTest) diff --git a/src/maps.h b/src/maps.h index ddaa82f..fb9f42d 100644 --- a/src/maps.h +++ b/src/maps.h @@ -1,181 +1,163 @@ /* Copyright 2014-2015 Harald Sitter + Copyright 2018 David Rosca This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #pragma once -#include "debug.h" -#include +#include +#include +#include #include #include #include namespace PulseAudioQt { // Used for typedefs. class Card; class Client; class Sink; class SinkInput; class Source; class SourceOutput; class StreamRestore; class Module; /** * @see MapBase * This class is nothing more than the QObject base since moc cannot handle * templates. */ class MapBaseQObject : public QObject { Q_OBJECT public: virtual int count() const = 0; virtual QObject *objectAt(int index) const = 0; virtual int indexOfObject(QObject *object) const = 0; Q_SIGNALS: void aboutToBeAdded(int index); void added(int index); void aboutToBeRemoved(int index); void removed(int index); }; /** * Maps a specific index to a specific object pointer. * This is used to give the unique arbitrary PulseAudio index of a PulseObject a * serialized list index. Namely it enables us to translate a discrete list * index to a pulse index to an object, and any permutation thereof. */ template class MapBase : public MapBaseQObject { public: virtual ~MapBase() {} - const QMap &data() const { return m_data; } + const QVector &data() const { return m_data; } int count() const override { return m_data.count(); } int indexOfObject(QObject *object) const override { - int index = 0; - for (auto it = m_data.constBegin(); it != m_data.constEnd(); ++it) { - if (it.value() == object) { - return index; - } - index++; - } - return -1; + return m_data.indexOf(static_cast(object)); } QObject *objectAt(int index) const override { - Q_ASSERT(index >= 0 && index < m_data.count()); - return (m_data.constBegin() + index).value(); + return m_data.at(index); } void reset() { - while (!m_data.isEmpty()) { - removeEntry(m_data.lastKey()); + while (!m_hash.isEmpty()) { + removeEntry(m_data.at(m_data.count() - 1)->index()); } m_pendingRemovals.clear(); } void insert(Type *object) { - Q_ASSERT(!m_data.contains(object->index())); - - int modelIndex = 0; - for (auto it = m_data.constBegin(); it != m_data.constEnd(); ++it) { - if (object->index() < it.key()) { - break; - } - modelIndex++; - } + Q_ASSERT(!m_data.contains(object)); + + const int modelIndex = m_data.count(); Q_EMIT aboutToBeAdded(modelIndex); - m_data.insert(object->index(), object); - Q_ASSERT(modelIndex == m_data.keys().indexOf(object->index())); + m_data.append(object); + m_hash[object->index()] = object; Q_EMIT added(modelIndex); } // Context is passed in as parent because context needs to include the maps // so we'd cause a circular dep if we were to try to use the instance here. // Plus that's weird separation anyway. void updateEntry(const PAInfo *info, QObject *parent) { Q_ASSERT(info); if (m_pendingRemovals.remove(info->index)) { // Was already removed again. return; } - auto *obj = m_data.value(info->index); + auto *obj = m_hash.value(info->index); if (!obj) { obj = new Type(parent); - } - obj->update(info); - - if (!m_data.contains(info->index)) { + obj->update(info); insert(obj); + } else { + obj->update(info); } } void removeEntry(quint32 index) { - if (!m_data.contains(index)) { + if (!m_hash.contains(index)) { m_pendingRemovals.insert(index); } else { - int modelIndex = 0; - for (auto it = m_data.constBegin(); it != m_data.constEnd(); ++it) { - if (it.key() == index) { - break; - } - modelIndex++; - } - Q_ASSERT(modelIndex == m_data.keys().indexOf(index)); + const int modelIndex = m_data.indexOf(m_hash.value(index)); Q_EMIT aboutToBeRemoved(modelIndex); - delete m_data.take(index); + m_data.removeAt(modelIndex); + delete m_hash.take(index); Q_EMIT removed(modelIndex); } } protected: - QMap m_data; + QVector m_data; + QHash m_hash; QSet m_pendingRemovals; }; typedef MapBase SinkInputMap; typedef MapBase SourceOutputMap; typedef MapBase ClientMap; typedef MapBase CardMap; typedef MapBase ModuleMap; typedef MapBase StreamRestoreMap; } // PulseAudioQt