PDA

View Full Version : Removing items from QPtrList efficiency issues



kalos80
5th April 2007, 09:56
Hi,

I have a QPtrList<myClass> which is a queue.
Each element of myClass has an id.

Given the id of an element in the queue, I need to remove such element from the QPtrList in the most efficient way.

In order to do this, I would like to use a map for ids of elements in the queue, and associate something to each id in the map, such that allows me to efficiently find the element in the queue and delete it ( ideally with complexity O(1) ).

These are the information I thought I can associate to each id in the map in order to remove such element from the queue:

- the Pointer "p" (reference) to the element in the queue (I would need to use the removeRef(p) function of the QPtrList)
or
- the index "i" of the element in the queue (I would need to use the remove(i) function of the QPtrList)

Does anyone know which function is the most efficient between the removeRef(p) or the remove(i) of the QPtrList class?

Is it possible to use some other information (i.e. a QPtrListIterator) to delete an element from the QPtrList in a more efficient way?

Thanks in advance for your help!

wysota
5th April 2007, 12:04
The problem is you'll have to remove the item from two lists and one of them won't be sorted. The thing you could do is to cheat by changing the queue into a sorted list (insertions slower) and keeping pointers to elements of the list as your queue, so that you can quickly access "the first" element. I think my explanation is a bit unclear, so ask if you have doubts :)