Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 6

Qt-interest Archive, August 2006
QMap<QString, ...> : optimized search...


Message 1 in thread

Hi all,

I would need to search a key in a QMap<QString, ...> in an optimized way,
knowing that the key to find is **most probably** after a given
QMap::iterator.


Question :
According to the efficiency tests I made, something like the "last accessed
iterator" is kept by the QMap. Consequently, searching for the key next to
the "last accessed iterator" is already optimized in Qt.

Is that right ?




In other words, for an optimized search, should I write something like :

----------------------------
QMap<QString, ...> myMap
// insert various elements

int aNumber = random(...) ;


QString searchedKey = QString().setNum(aNumber);
QMap<QString, ...>::iterator curItr = myMap.find( searchedKey );

if(curItr !=  m_namedLinks_M.end() {
        // do smthing with the curItr ;
        curItr  ++ ;
}

// now searching for the key QString().setNum(aNumber + 1) ;
QString searchedKey = QString().setNum( aNumber + 1 );

if(curItr != m_namedLinks_M.end()
        && curItr.key() == searchedKey ) {
        // check if the searchedKey is right after the latest search...
        qDebug() << "next key found" ;
}
else {
        curItr= m_namedLinks_M.constFind(searchedKey ) ;
        if(     curItr != m_namedLinks_M.end()) {
        qDebug() << "next key found" ;
        }
        else {
        qDebug() << "next key NOT found" ;
        }
}

----------------------------

Or should I write more simply, with the same optimality :

----------------------------

QMap<QString, ...> myMap
// insert various elements

int aNumber = random(...) ;


QString searchedKey = QString().setNum(aNumber);
QMap<QString, ...>::iterator curItr = myMap.find( searchedKey );


if(curItr !=  m_namedLinks_M.end(){
        // do smthing with the curItr ;
        curItr  ++ ;
}

// now searching for the key QString().setNum(aNumber + 1) ;
QString searchedKey = QString().setNum( aNumber + 1 );

curItr= m_namedLinks_M.constFind(searchedKey ) ;
if(     curItr != m_namedLinks_M.end()) {
        qDebug() << "next key found" ;
}
else {
        qDebug() << "next key NOT found" ;
}

--
 [ signature omitted ]