Dark_Tower
19th December 2006, 16:38
Hi all, I have the following method where I manage various lists: "llistaAccions" is a list containing identificators to the actions that I need to execute sequentally until the previous to the last one. "llistaParamsColors", "llistaParamsRects", "llistaParamsDoubles" and "llistaParamsInts" contain the parameters of each action. Now, I use QMutableListIterators to iterate through the lists but I think that the code can be slighty more efficient because I go through the list sequentally and, also, I need to delete only the last items on each list and not the items in the middle. Here's the code:
void MyApp::executeAllUntilPrevious()
{
int aux1, aux2, aux3, aux4;
double auxd1, auxd2, auxd3, auxd4;
QImage imatgeEnganxar;
llistaAccions.removeLast();
QMutableListIterator<IdentificadorAccions> itAccions(llistaAccions);
QMutableListIterator<QColor> itColors(llistaParamsColors);
QMutableListIterator<QRect> itRects(llistaParamsRects);
QMutableListIterator<double> itDoubles(llistaParamsDoubles);
QMutableListIterator<int> itInts(llistaParamsInts);
while(itAccions.hasNext())
{
switch(itAccions.next())
{
case Rotat:
rotat(itColors.next(), itDoubles.next());
break;
case Retallat:
retallat(itRects.next());
break;
case Pintat:
pintat(itColors.next(), itRects.next());
break;
case Copiat:
if (itAccions.hasNext())
imatgeEnganxar = foto -> copiarImatge(itRects.next());
else
itAccions.remove();
break;
case Enganxat:
enganxat(itRects.next(), imatgeEnganxar);
break;
case Restaurat:
restaurat();
break;
case EscalatCm:
auxd1 = itDoubles.next();
auxd2 = itDoubles.next();
canviTamanyCm(auxd1, auxd2);
break;
case EscalatPixels:
aux1 = itInts.next();
aux2 = itInts.next();
canviTamanyPixels(aux1, aux2);
break;
case CanviatResolucioPixelsCm:
aux1 = itInts.next();
aux2 = itInts.next();
canviResolucioPixelsCm(aux1, aux2);
break;
case CanviatResolucioPixelsPolzada:
aux1 = itInts.next();
aux2 = itInts.next();
canviResolucioPixelsPolzada(aux1, aux2);
break;
case CanviatMargesCm:
auxd1 = itDoubles.next();
auxd2 = itDoubles.next();
auxd3 = itDoubles.next();
auxd4 = itDoubles.next();
canviTamanyMargesCm(itColors.next(), auxd1, auxd2, auxd3, auxd4);
break;
case CanviatMargesPixels:
aux1 = itInts.next();
aux2 = itInts.next();
aux3 = itInts.next();
aux4 = itInts.next();
canviTamanyMargesPixels(itColors.next(), aux1, aux2, aux3, aux4);
}
}
//This part is a bit "dirty" and I think that could be achieved with just one sentence...
while(itColors.hasNext())
{
itColors.next();
itColors.remove();
}
while(itRects.hasNext())
{
itRects.next();
itRects.remove();
}
while(itDoubles.hasNext())
{
itDoubles.next();
itDoubles.remove();
}
while(itInts.hasNext())
{
itInts.next();
itInts.remove();
}
}
Well, I've readed that STL style iterators can be slightly faster than the Java-style iterators (QMutableListIterator). And, inside the STL style iterators, QList::const_iterator is slightly faster than QList::iterator. First of all, do you think that I really need iterators with the code above to obtain a better performance and/or readability? And, in that case, what kind of iterator you think that could fit better in what I need to do: iterate sequentially through the lists and, at the end, delete all the items from the current position until the end. Thanks.
void MyApp::executeAllUntilPrevious()
{
int aux1, aux2, aux3, aux4;
double auxd1, auxd2, auxd3, auxd4;
QImage imatgeEnganxar;
llistaAccions.removeLast();
QMutableListIterator<IdentificadorAccions> itAccions(llistaAccions);
QMutableListIterator<QColor> itColors(llistaParamsColors);
QMutableListIterator<QRect> itRects(llistaParamsRects);
QMutableListIterator<double> itDoubles(llistaParamsDoubles);
QMutableListIterator<int> itInts(llistaParamsInts);
while(itAccions.hasNext())
{
switch(itAccions.next())
{
case Rotat:
rotat(itColors.next(), itDoubles.next());
break;
case Retallat:
retallat(itRects.next());
break;
case Pintat:
pintat(itColors.next(), itRects.next());
break;
case Copiat:
if (itAccions.hasNext())
imatgeEnganxar = foto -> copiarImatge(itRects.next());
else
itAccions.remove();
break;
case Enganxat:
enganxat(itRects.next(), imatgeEnganxar);
break;
case Restaurat:
restaurat();
break;
case EscalatCm:
auxd1 = itDoubles.next();
auxd2 = itDoubles.next();
canviTamanyCm(auxd1, auxd2);
break;
case EscalatPixels:
aux1 = itInts.next();
aux2 = itInts.next();
canviTamanyPixels(aux1, aux2);
break;
case CanviatResolucioPixelsCm:
aux1 = itInts.next();
aux2 = itInts.next();
canviResolucioPixelsCm(aux1, aux2);
break;
case CanviatResolucioPixelsPolzada:
aux1 = itInts.next();
aux2 = itInts.next();
canviResolucioPixelsPolzada(aux1, aux2);
break;
case CanviatMargesCm:
auxd1 = itDoubles.next();
auxd2 = itDoubles.next();
auxd3 = itDoubles.next();
auxd4 = itDoubles.next();
canviTamanyMargesCm(itColors.next(), auxd1, auxd2, auxd3, auxd4);
break;
case CanviatMargesPixels:
aux1 = itInts.next();
aux2 = itInts.next();
aux3 = itInts.next();
aux4 = itInts.next();
canviTamanyMargesPixels(itColors.next(), aux1, aux2, aux3, aux4);
}
}
//This part is a bit "dirty" and I think that could be achieved with just one sentence...
while(itColors.hasNext())
{
itColors.next();
itColors.remove();
}
while(itRects.hasNext())
{
itRects.next();
itRects.remove();
}
while(itDoubles.hasNext())
{
itDoubles.next();
itDoubles.remove();
}
while(itInts.hasNext())
{
itInts.next();
itInts.remove();
}
}
Well, I've readed that STL style iterators can be slightly faster than the Java-style iterators (QMutableListIterator). And, inside the STL style iterators, QList::const_iterator is slightly faster than QList::iterator. First of all, do you think that I really need iterators with the code above to obtain a better performance and/or readability? And, in that case, what kind of iterator you think that could fit better in what I need to do: iterate sequentially through the lists and, at the end, delete all the items from the current position until the end. Thanks.