+ bool foundid = false;
+
+ // this loop finds the last *nonempty* row with the same id
+ // and position <= pos
+ RowList::const_iterator bestrow = rowlist.begin();
+ RowList::const_iterator it = rowlist.begin();
+ RowList::const_iterator const end = rowlist.end();
+ for (; it != end; ++it) {
+ if (it->id() == id && it->pos() <= pos) {
+ foundid = true;
+ if (bestrow->id() != id || it->pos() > bestrow->pos())
+ bestrow = it;
+ } else if (foundid)
+ break;
+ }
+ if (!foundid)
+ return rowlist.size();
+ return distance(rowlist.begin(), bestrow) + 1;