Commit e4b45d4e authored by Jan Möbius's avatar Jan Möbius
Browse files

Accellerate Treeitem lookup for child by a map. This should speedup large...

Accellerate Treeitem lookup for child by a map. This should speedup large object count handling significantly.

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@15121 383ad7c9-94d9-4d36-a494-682f7c89f535
parent bc614da3
......@@ -42,6 +42,9 @@
#include "TreeItem.hh"
/// Acceleration map
static QMap<int,TreeItem*> treeMap_;
//--------------------------------------------------------------------------------
TreeItem::TreeItem(int _id, QString _name, DataType _type, TreeItem* _parent) :
......@@ -53,6 +56,19 @@ TreeItem::TreeItem(int _id, QString _name, DataType _type, TreeItem* _parent) :
name_(_name),
parentItem_(_parent)
{
// Remember ourself ;-)
treeMap_[_id] = this;
}
TreeItem::~TreeItem() {
// Remove itself from map
QMap<int,TreeItem*>::iterator iter = treeMap_.find( id() );
if( iter != treeMap_.end() ) {
treeMap_.erase(iter);
} else {
std::cerr << "Map accelerator destructor in DataControl: Currently removing object that is not in the map!" << std::endl;
}
}
......@@ -87,7 +103,7 @@ int TreeItem::group() {
if ( parent() == 0 )
return -1;
// Dont count root node as a group
// Don't count root node as a group
if ( parent()->parent() == 0 )
return -1;
......@@ -234,7 +250,8 @@ void TreeItem::setParent(TreeItem* _parent) {
void TreeItem::appendChild(TreeItem *item)
{
childItems_.append(item);
treeMap_[item->id()] = item;
childItems_.append(item);
}
//--------------------------------------------------------------------------------
......@@ -259,31 +276,32 @@ TreeItem* TreeItem::childExists(int _objectId) {
if ( id_ == _objectId )
return this;
// search in children
for ( int i = 0 ; i < childItems_.size(); ++i ) {
TreeItem* tmp = childItems_[i]->childExists(_objectId);
if ( tmp != 0)
return tmp;
}
// Check the map, for the item
QMap<int,TreeItem*>::const_iterator iter = treeMap_.find(_objectId);
return 0;
}
// Not found -> return 0
if( iter == treeMap_.end() ) {
return 0;
}
//--------------------------------------------------------------------------------
// Move the tree up and check if we are in the line to the root
TreeItem* current = *iter;
TreeItem* TreeItem::childExists(QString _name) {
while ( true ) {
// Check if this object has the requested id
if ( name() == _name )
return this;
// Current item is a parent of the found one
if ( current == this ) {
return *iter;
}
// search in children
for ( int i = 0 ; i < childItems_.size(); ++i ) {
TreeItem* tmp = childItems_[i]->childExists(_name);
if ( tmp != 0)
return tmp;
// Move to parent or if there is no parent, we return 0
if ( current->parent() != 0) {
current = current->parent();
} else
return 0;
}
// Not in the line, so child does not exist
return 0;
}
......
......@@ -55,6 +55,8 @@ class TreeItem {
TreeItem(int _id, QString _name, DataType _type, TreeItem* _parent);
~TreeItem();
// static members
public:
/// id
......@@ -139,9 +141,6 @@ class TreeItem {
/// Check if the element exists in the subtree of this element
TreeItem* childExists(int _objectId);
/// Check if the element exists in the subtree of this element
TreeItem* childExists(QString _name);
/// add a child to this node
void appendChild(TreeItem *child);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment