1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

[ClosedTabsManager] Use QLinkedList for storing closed tabs

The operations prepend() and takeFirst() are O(1) in linked list
This commit is contained in:
nowrep 2014-02-10 19:23:08 +01:00
parent b88bbe4fc8
commit 51498fdb6e
2 changed files with 30 additions and 22 deletions

View File

@ -42,43 +42,49 @@ void ClosedTabsManager::saveView(WebTab* tab, int position)
m_closedTabs.prepend(closedTab); m_closedTabs.prepend(closedTab);
} }
ClosedTabsManager::Tab ClosedTabsManager::getFirstClosedTab() bool ClosedTabsManager::isClosedTabAvailable()
{
return !m_closedTabs.isEmpty();
}
ClosedTabsManager::Tab ClosedTabsManager::takeLastClosedTab()
{ {
Tab tab; Tab tab;
tab.position = -1; tab.position = -1;
if (m_closedTabs.count() > 0) { if (m_closedTabs.count() > 0) {
tab = m_closedTabs.first(); tab = m_closedTabs.takeFirst();
m_closedTabs.remove(0);
} }
return tab; return tab;
} }
ClosedTabsManager::Tab ClosedTabsManager::getTabAt(int index) ClosedTabsManager::Tab ClosedTabsManager::takeTabAt(int index)
{ {
Tab tab; Tab tab;
tab.position = -1; tab.position = -1;
if (QzTools::vectorContainsIndex(m_closedTabs, index)) { QLinkedList<Tab>::iterator it;
tab = m_closedTabs.at(index); int i = 0;
m_closedTabs.remove(index);
for (it = m_closedTabs.begin(); it != m_closedTabs.end(); ++it, ++i) {
if (i == index) {
tab = *it;
m_closedTabs.erase(it);
break;
}
} }
return tab; return tab;
} }
bool ClosedTabsManager::isClosedTabAvailable() QLinkedList<ClosedTabsManager::Tab> ClosedTabsManager::allClosedTabs()
{ {
return (m_closedTabs.count() != 0); return m_closedTabs;
} }
void ClosedTabsManager::clearList() void ClosedTabsManager::clearList()
{ {
m_closedTabs.clear(); m_closedTabs.clear();
} }
QVector<ClosedTabsManager::Tab> ClosedTabsManager::allClosedTabs()
{
return m_closedTabs;
}

View File

@ -19,7 +19,7 @@
#define CLOSEDTABSMANAGER_H #define CLOSEDTABSMANAGER_H
#include <QUrl> #include <QUrl>
#include <QVector> #include <QLinkedList>
#include "qz_namespace.h" #include "qz_namespace.h"
@ -44,20 +44,22 @@ public:
explicit ClosedTabsManager(); explicit ClosedTabsManager();
void saveView(WebTab* tab, int position); void saveView(WebTab* tab, int position);
ClosedTabsManager::Tab getFirstClosedTab();
ClosedTabsManager::Tab getTabAt(int index);
bool isClosedTabAvailable(); bool isClosedTabAvailable();
// Takes tab that was most recently closed
Tab takeLastClosedTab();
// Takes tab at given index
Tab takeTabAt(int index);
QLinkedList<Tab> allClosedTabs();
void clearList(); void clearList();
QVector<ClosedTabsManager::Tab> allClosedTabs();
private: private:
QVector<ClosedTabsManager::Tab> m_closedTabs; QLinkedList<Tab> m_closedTabs;
}; };
// Hint to QVector to use std::realloc on item moving // Hint to Qt to use std::realloc on item moving
Q_DECLARE_TYPEINFO(ClosedTabsManager::Tab, Q_MOVABLE_TYPE); Q_DECLARE_TYPEINFO(ClosedTabsManager::Tab, Q_MOVABLE_TYPE);
#endif // CLOSEDTABSMANAGER_H #endif // CLOSEDTABSMANAGER_H