mirror of https://github.com/qt/qtbase.git
QMap: add const equal_range() overload
... to prevent detaching. [ChangeLog][QtCore][QMap] Added const equal_range() overload. Change-Id: I4b39abb8ad41ba6eaa8f9a9a74ed74ed10337dd3 Reviewed-by: Sérgio Martins <sergio.martins@kdab.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
6f530fe4d6
commit
302cc32dee
|
@ -1161,6 +1161,12 @@ void QMapDataBase::freeData(QMapDataBase *d)
|
||||||
are stored under \a key.
|
are stored under \a key.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\fn QPair<const_iterator, const_iterator> QMap::equal_range(const Key &key) const
|
||||||
|
\overload
|
||||||
|
\since 5.6
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
/*! \class QMap::iterator
|
/*! \class QMap::iterator
|
||||||
\inmodule QtCore
|
\inmodule QtCore
|
||||||
|
|
|
@ -557,6 +557,7 @@ public:
|
||||||
typedef int size_type;
|
typedef int size_type;
|
||||||
inline bool empty() const { return isEmpty(); }
|
inline bool empty() const { return isEmpty(); }
|
||||||
QPair<iterator, iterator> equal_range(const Key &akey);
|
QPair<iterator, iterator> equal_range(const Key &akey);
|
||||||
|
QPair<const_iterator, const_iterator> equal_range(const Key &akey) const;
|
||||||
|
|
||||||
#ifdef Q_MAP_DEBUG
|
#ifdef Q_MAP_DEBUG
|
||||||
void dump() const;
|
void dump() const;
|
||||||
|
@ -864,6 +865,15 @@ QPair<typename QMap<Key, T>::iterator, typename QMap<Key, T>::iterator> QMap<Key
|
||||||
return QPair<iterator, iterator>(iterator(firstNode), iterator(lastNode));
|
return QPair<iterator, iterator>(iterator(firstNode), iterator(lastNode));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <class Key, class T>
|
||||||
|
QPair<typename QMap<Key, T>::const_iterator, typename QMap<Key, T>::const_iterator>
|
||||||
|
QMap<Key, T>::equal_range(const Key &akey) const
|
||||||
|
{
|
||||||
|
Node *firstNode, *lastNode;
|
||||||
|
d->nodeRange(akey, &firstNode, &lastNode);
|
||||||
|
return qMakePair(const_iterator(firstNode), const_iterator(lastNode));
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef Q_MAP_DEBUG
|
#ifdef Q_MAP_DEBUG
|
||||||
template <class Key, class T>
|
template <class Key, class T>
|
||||||
void QMap<Key, T>::dump() const
|
void QMap<Key, T>::dump() const
|
||||||
|
|
|
@ -996,11 +996,16 @@ void tst_QMap::const_shared_null()
|
||||||
void tst_QMap::equal_range()
|
void tst_QMap::equal_range()
|
||||||
{
|
{
|
||||||
QMap<int, QString> map;
|
QMap<int, QString> map;
|
||||||
|
const QMap<int, QString> &cmap = map;
|
||||||
|
|
||||||
QPair<QMap<int, QString>::iterator, QMap<int, QString>::iterator> result = map.equal_range(0);
|
QPair<QMap<int, QString>::iterator, QMap<int, QString>::iterator> result = map.equal_range(0);
|
||||||
QCOMPARE(result.first, map.end());
|
QCOMPARE(result.first, map.end());
|
||||||
QCOMPARE(result.second, map.end());
|
QCOMPARE(result.second, map.end());
|
||||||
|
|
||||||
|
QPair<QMap<int, QString>::const_iterator, QMap<int, QString>::const_iterator> cresult = cmap.equal_range(0);
|
||||||
|
QCOMPARE(cresult.first, cmap.cend());
|
||||||
|
QCOMPARE(cresult.second, cmap.cend());
|
||||||
|
|
||||||
map.insert(1, "one");
|
map.insert(1, "one");
|
||||||
|
|
||||||
result = map.equal_range(0);
|
result = map.equal_range(0);
|
||||||
|
@ -1015,6 +1020,18 @@ void tst_QMap::equal_range()
|
||||||
QCOMPARE(result.first, map.end());
|
QCOMPARE(result.first, map.end());
|
||||||
QCOMPARE(result.second, map.end());
|
QCOMPARE(result.second, map.end());
|
||||||
|
|
||||||
|
cresult = cmap.equal_range(0);
|
||||||
|
QCOMPARE(cresult.first, cmap.find(1));
|
||||||
|
QCOMPARE(cresult.second, cmap.find(1));
|
||||||
|
|
||||||
|
cresult = cmap.equal_range(1);
|
||||||
|
QCOMPARE(cresult.first, cmap.find(1));
|
||||||
|
QCOMPARE(cresult.second, cmap.cend());
|
||||||
|
|
||||||
|
cresult = cmap.equal_range(2);
|
||||||
|
QCOMPARE(cresult.first, cmap.cend());
|
||||||
|
QCOMPARE(cresult.second, cmap.cend());
|
||||||
|
|
||||||
for (int i = -10; i < 10; i += 2)
|
for (int i = -10; i < 10; i += 2)
|
||||||
map.insert(i, QString("%1").arg(i));
|
map.insert(i, QString("%1").arg(i));
|
||||||
|
|
||||||
|
@ -1030,11 +1047,28 @@ void tst_QMap::equal_range()
|
||||||
QCOMPARE(result.first, map.find(2));
|
QCOMPARE(result.first, map.find(2));
|
||||||
QCOMPARE(result.second, map.find(4));
|
QCOMPARE(result.second, map.find(4));
|
||||||
|
|
||||||
|
cresult = cmap.equal_range(0);
|
||||||
|
QCOMPARE(cresult.first, cmap.find(0));
|
||||||
|
QCOMPARE(cresult.second, cmap.find(1));
|
||||||
|
|
||||||
|
cresult = cmap.equal_range(1);
|
||||||
|
QCOMPARE(cresult.first, cmap.find(1));
|
||||||
|
QCOMPARE(cresult.second, cmap.find(2));
|
||||||
|
|
||||||
|
cresult = cmap.equal_range(2);
|
||||||
|
QCOMPARE(cresult.first, cmap.find(2));
|
||||||
|
QCOMPARE(cresult.second, cmap.find(4));
|
||||||
|
|
||||||
map.insertMulti(1, "another one");
|
map.insertMulti(1, "another one");
|
||||||
|
|
||||||
result = map.equal_range(1);
|
result = map.equal_range(1);
|
||||||
QCOMPARE(result.first, map.find(1));
|
QCOMPARE(result.first, map.find(1));
|
||||||
QCOMPARE(result.second, map.find(2));
|
QCOMPARE(result.second, map.find(2));
|
||||||
|
|
||||||
|
cresult = cmap.equal_range(1);
|
||||||
|
QCOMPARE(cresult.first, cmap.find(1));
|
||||||
|
QCOMPARE(cresult.second, cmap.find(2));
|
||||||
|
|
||||||
QCOMPARE(map.count(1), 2);
|
QCOMPARE(map.count(1), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue