QFlatMap: add full is_transparent support [3/3]: add overloads
Now add the missing overloads for mixed-type lookups, supported by is_transparent Compare objects. Pick-to: 6.3 Change-Id: Ib588b6a4f733d5d9908c8c7d7c209df6e7bd6674 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> Reviewed-by: Jörg Bornemann <joerg.bornemann@qt.io>bb10
parent
d336fdd393
commit
64bc6509c3
|
|
@ -610,6 +610,12 @@ public:
|
|||
return do_remove(binary_find(key));
|
||||
}
|
||||
|
||||
template <class X, class Y = Compare, is_marked_transparent<Y> = nullptr>
|
||||
bool remove(const X &key)
|
||||
{
|
||||
return do_remove(binary_find(key));
|
||||
}
|
||||
|
||||
iterator erase(iterator it)
|
||||
{
|
||||
c.values.erase(toValuesIterator(it));
|
||||
|
|
@ -621,23 +627,49 @@ public:
|
|||
return do_take(binary_find(key));
|
||||
}
|
||||
|
||||
template <class X, class Y = Compare, is_marked_transparent<Y> = nullptr>
|
||||
T take(const X &key)
|
||||
{
|
||||
return do_take(binary_find(key));
|
||||
}
|
||||
|
||||
bool contains(const Key &key) const
|
||||
{
|
||||
return binary_find(key) != end();
|
||||
}
|
||||
|
||||
template <class X, class Y = Compare, is_marked_transparent<Y> = nullptr>
|
||||
bool contains(const X &key) const
|
||||
{
|
||||
return binary_find(key) != end();
|
||||
}
|
||||
|
||||
T value(const Key &key, const T &defaultValue) const
|
||||
{
|
||||
auto it = binary_find(key);
|
||||
return it == end() ? defaultValue : it.value();
|
||||
}
|
||||
|
||||
template <class X, class Y = Compare, is_marked_transparent<Y> = nullptr>
|
||||
T value(const X &key, const T &defaultValue) const
|
||||
{
|
||||
auto it = binary_find(key);
|
||||
return it == end() ? defaultValue : it.value();
|
||||
}
|
||||
|
||||
T value(const Key &key) const
|
||||
{
|
||||
auto it = binary_find(key);
|
||||
return it == end() ? T() : it.value();
|
||||
}
|
||||
|
||||
template <class X, class Y = Compare, is_marked_transparent<Y> = nullptr>
|
||||
T value(const X &key) const
|
||||
{
|
||||
auto it = binary_find(key);
|
||||
return it == end() ? T() : it.value();
|
||||
}
|
||||
|
||||
T &operator[](const Key &key)
|
||||
{
|
||||
return try_emplace(key).first.value();
|
||||
|
|
@ -793,11 +825,23 @@ public:
|
|||
return binary_find(k);
|
||||
}
|
||||
|
||||
template <class X, class Y = Compare, is_marked_transparent<Y> = nullptr>
|
||||
iterator find(const X &key)
|
||||
{
|
||||
return binary_find(key);
|
||||
}
|
||||
|
||||
const_iterator find(const key_type &k) const
|
||||
{
|
||||
return binary_find(k);
|
||||
}
|
||||
|
||||
template <class X, class Y = Compare, is_marked_transparent<Y> = nullptr>
|
||||
const_iterator find(const X &key) const
|
||||
{
|
||||
return binary_find(key);
|
||||
}
|
||||
|
||||
key_compare key_comp() const noexcept
|
||||
{
|
||||
return static_cast<key_compare>(*this);
|
||||
|
|
@ -912,6 +956,12 @@ private:
|
|||
return { &c, std::as_const(*this).binary_find(key).i };
|
||||
}
|
||||
|
||||
template <class X, class Y = Compare, is_marked_transparent<Y> = nullptr>
|
||||
iterator binary_find(const X &key)
|
||||
{
|
||||
return { &c, std::as_const(*this).binary_find(key).i };
|
||||
}
|
||||
|
||||
const_iterator binary_find(const Key &key) const
|
||||
{
|
||||
auto it = lower_bound(key);
|
||||
|
|
@ -923,6 +973,18 @@ private:
|
|||
return it;
|
||||
}
|
||||
|
||||
template <class X, class Y = Compare, is_marked_transparent<Y> = nullptr>
|
||||
const_iterator binary_find(const X &key) const
|
||||
{
|
||||
auto it = lower_bound(key);
|
||||
if (it != end()) {
|
||||
if (!key_compare::operator()(key, it.key()))
|
||||
return it;
|
||||
it = end();
|
||||
}
|
||||
return it;
|
||||
}
|
||||
|
||||
void ensureOrderedUnique()
|
||||
{
|
||||
std::vector<size_type> p(size_t(c.keys.size()));
|
||||
|
|
|
|||
|
|
@ -410,7 +410,7 @@ void tst_QFlatMap::transparency()
|
|||
struct StringViewCompare
|
||||
{
|
||||
using is_transparent = void;
|
||||
bool operator()(const QStringView &lhs, const QStringView &rhs) const
|
||||
bool operator()(QAnyStringView lhs, QAnyStringView rhs) const
|
||||
{
|
||||
return lhs < rhs;
|
||||
}
|
||||
|
|
@ -424,8 +424,21 @@ void tst_QFlatMap::transparency()
|
|||
const QStringView sv2{numbers.constData() + 4, 3};
|
||||
const QStringView sv3{numbers.constData() + 8, 5};
|
||||
QCOMPARE(m.lower_bound(sv1).value(), "een");
|
||||
QCOMPARE(m.value(sv1), "een");
|
||||
QCOMPARE(m.lower_bound(sv2).value(), "twee");
|
||||
QCOMPARE(m.value(sv2), "twee");
|
||||
QCOMPARE(m.lower_bound(sv3).value(), "dree");
|
||||
QCOMPARE(m.value(sv3), "dree");
|
||||
|
||||
QVERIFY(m.contains(sv2));
|
||||
auto twee = m.take(sv2);
|
||||
static_assert(std::is_same_v<decltype(twee), QString>);
|
||||
QCOMPARE(twee, "twee");
|
||||
QVERIFY(!m.contains(sv2));
|
||||
|
||||
QVERIFY(m.contains(QLatin1String("one")));
|
||||
QVERIFY(m.remove(QAnyStringView(u8"one")));
|
||||
QVERIFY(!m.contains(QLatin1String("one")));
|
||||
}
|
||||
|
||||
void tst_QFlatMap::try_emplace_and_insert_or_assign()
|
||||
|
|
|
|||
Loading…
Reference in New Issue