QMap<int, QString> map;
const int THE_SAME_KEY = 15;
map.insertMulti(THE_SAME_KEY, "Inserted first");
map.insertMulti(THE_SAME_KEY, "Inserted second");
map.insertMulti(THE_SAME_KEY, "Inserted third");
qDebug() << map;
QMap<int, QString> map;
const int THE_SAME_KEY = 15;
map.insertMulti(THE_SAME_KEY, "Inserted first");
map.insertMulti(THE_SAME_KEY, "Inserted second");
map.insertMulti(THE_SAME_KEY, "Inserted third");
qDebug() << map;
To copy to clipboard, switch view to plain text mode
I added this function. It should work identical to insertMulti, with the exception
//that if two or more elements share the same key, the earlier they were added, the earlier they appear in the map.
//(With the normal insertMulti() function, the most recently added element gets inserted before earlier elements that
//share the same value).
template <class Key, class T>
Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::insertMultiAfter(const Key &akey,
const T &avalue)
{
detach();
QMapData::Node *update[QMapData::LastLevel + 1];
mutableFindLastNode(update, akey); //<--- The only line that changed.
return iterator(node_create(d, update, akey, avalue));
}
I added this function, to return the *last* node with the matching key,
//instead of the *first* node with the matching key.
template <class Key, class T>
Q_OUTOFLINE_TEMPLATE QMapData::Node *QMap<Key, T>::mutableFindLastNode(QMapData::Node *aupdate[],
const Key &akey) const
{
QMapData::Node *cur = e;
QMapData::Node *next = e;
for (int i = d->topLevel; i >= 0; i--) {
while ((next = cur->forward[i]) != e && qMapLessThanOrEqualKey<Key>(concrete(next)->key, akey)) //<------------- 'qMapLessThanOrEqualKey()' is called instead of qMapLessThanKey()
cur = next;
aupdate[i] = cur;
}
if (next != e && !qMapLessThanOrEqualKey<Key>(akey, concrete(next)->key)) { //<------------- 'qMapLessThanOrEqualKey()' is called instead of qMapLessThanKey()
return next;
} else {
return e;
}
}
//This function is added as an alternative to 'qMapLessThanKey' (Likewise, the overloaded pointer versions of these functions have a 'LessThanOrEqual' alternative).
template <class Key> inline bool qMapLessThanOrEqualKey(const Key &key1, const Key &key2)
{
return key1 <= key2; //<------------- The only line that changed.
}
I added this function. It should work identical to insertMulti, with the exception
//that if two or more elements share the same key, the earlier they were added, the earlier they appear in the map.
//(With the normal insertMulti() function, the most recently added element gets inserted before earlier elements that
//share the same value).
template <class Key, class T>
Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::insertMultiAfter(const Key &akey,
const T &avalue)
{
detach();
QMapData::Node *update[QMapData::LastLevel + 1];
mutableFindLastNode(update, akey); //<--- The only line that changed.
return iterator(node_create(d, update, akey, avalue));
}
I added this function, to return the *last* node with the matching key,
//instead of the *first* node with the matching key.
template <class Key, class T>
Q_OUTOFLINE_TEMPLATE QMapData::Node *QMap<Key, T>::mutableFindLastNode(QMapData::Node *aupdate[],
const Key &akey) const
{
QMapData::Node *cur = e;
QMapData::Node *next = e;
for (int i = d->topLevel; i >= 0; i--) {
while ((next = cur->forward[i]) != e && qMapLessThanOrEqualKey<Key>(concrete(next)->key, akey)) //<------------- 'qMapLessThanOrEqualKey()' is called instead of qMapLessThanKey()
cur = next;
aupdate[i] = cur;
}
if (next != e && !qMapLessThanOrEqualKey<Key>(akey, concrete(next)->key)) { //<------------- 'qMapLessThanOrEqualKey()' is called instead of qMapLessThanKey()
return next;
} else {
return e;
}
}
//This function is added as an alternative to 'qMapLessThanKey' (Likewise, the overloaded pointer versions of these functions have a 'LessThanOrEqual' alternative).
template <class Key> inline bool qMapLessThanOrEqualKey(const Key &key1, const Key &key2)
{
return key1 <= key2; //<------------- The only line that changed.
}
To copy to clipboard, switch view to plain text mode
Bookmarks