[prev in list] [next in list] [prev in thread] [next in thread] 

List:       koffice-devel
Subject:    QList::at and so on :}
From:       Werner Trobin <trobin () kde ! org>
Date:       2001-06-26 15:27:13
[Download RAW message or body]

Hi!

First of all: I'm very sorry that I didn't have the time
to check the recent KPresenter patches before they have
been committed due to lack of time and I still didn't
*really* check them. Today I just briefly looked at the
diff and I just wanted to highlight one issue:

If you see code like that:

for ( uint i = 0; i < objectList()->count(); ++i ) {
    object = objectList()->at( i );
    object->doSomething();
    object->doSomethingElse(foo);
}

you have to think about what you are doing. QList is
implemented as a doubly linked list. The worst case
would be that the list->at(index) method would just
go to the first item and "walk" along the list till it
arrives at index. This would result in an ugly amount
of steps in *every* single iteration. Fortunately QList
is "sloppy-programmer-safe" and stores the last position
as currentNode and uses this information to walk on. So
basically it's not *that* bad in reality, but still this
can be made faster (and more "beautiful" == understandable
for people reading the code) by using iterators:

You can either write (from the Qt documentation):

Employee *emp;
for ( emp=list.first(); emp != 0; emp=list.next() )
    printf( "%s earns %d\n", emp->name().latin1(), emp->salary() );

...or:

QListIterator<KoViewChild> it( d->m_children );
for (; it.current(); ++it )
    it.current()->doIt();

However, there are a few possibilities to do that, and it
really "looks better" if you use iterators. (It should also
be a little bit faster).

Oh, and consider using ConstIterators if you only call const
methods.

Ciao,
Werner

-- 
Werner Trobin - trobin@kde.org
_______________________________________________
Koffice-devel mailing list
Koffice-devel@master.kde.org
http://master.kde.org/mailman/listinfo/koffice-devel

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic