Commit 4300cfbf authored by David Faure's avatar David Faure
Browse files

Fix the previous fix: skip algo if the guessed index was out of bounds.

Setting to the last item would miss checking the last item itself,
so the method would return -1 by mistake if the last item was the one we
were looking for.

Arguably it would be faster to do a reverse search in that case though.

Note that removing the -1 in qMin wouldn't work either, bwdIt==end skips
all backward search.

parent 6948f86e
......@@ -344,11 +344,10 @@ int Item::indexOfChildItem(Item *child) const
// will find the new position by going from the old guess rather than scanning
// the list from the beginning. The worst case scenario is equal to not having
// any guess at all.
// TODO: Should we just start from mChildItems->count() / 2.0 if idx == 0?
if (idx > 0) {
if (idx > 0 && idx < d_ptr->mChildItems->count()) {
const auto begin = d_ptr->mChildItems->cbegin();
const auto end = d_ptr->mChildItems->cend();
auto fwdIt = begin + qMin(idx, d_ptr->mChildItems->count() - 1);
auto fwdIt = begin + idx;
auto bwdIt = fwdIt;
idx = -1; // invalidate idx so it's -1 in case we fail to find the item
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