QMultiMap: fix regression in find(Key, T)

1) Implementing the const version in terms of the non-const
version exposes to accidental detaches. Avoid that.

2) The non-const version has to detach, just like find(Key),
or doing a comparison like find(Key, T) != end() might report
a wrong result.

3) Properly check if the value was found by checking find_if's
return value (against its second parameter, the end of the
iterated range). If the value was NOT found, then return
the map's end() (again because clients of find() will check
against end()).

Change-Id: I03533e89f1e7a52ad888d159d78f38002765953c
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Topi Reiniö <topi.reinio@qt.io>
bb10
Giuseppe D'Angelo 2020-08-17 17:38:41 +02:00
parent 2ba1d540e6
commit 8191a19df4
1 changed files with 12 additions and 5 deletions

View File

@ -1249,22 +1249,29 @@ public:
iterator find(const Key &key, const T &value)
{
if (!d)
return iterator();
detach();
auto range = d->m.equal_range(key);
auto i = std::find_if(range.first, range.second,
MapData::valueIsEqualTo(value));
return iterator(i);
if (i != range.second)
return iterator(i);
return iterator(d->m.end());
}
const_iterator find(const Key &key, const T &value) const
{
if (!d)
return const_iterator();
// a bit evil, but effective
return const_iterator(const_cast<QMultiMap *>(this)->find(key, value));
auto range = d->m.equal_range(key);
auto i = std::find_if(range.first, range.second,
MapData::valueIsEqualTo(value));
if (i != range.second)
return const_iterator(i);
return const_iterator(d->m.end());
}
const_iterator constFind(const Key &key, const T &value) const