diff --git a/src/corelib/tools/qhash.cpp b/src/corelib/tools/qhash.cpp index 6d7ed95aca..bc8c9490d9 100644 --- a/src/corelib/tools/qhash.cpp +++ b/src/corelib/tools/qhash.cpp @@ -1865,14 +1865,15 @@ size_t qHash(long double key, size_t seed) noexcept \sa count(), QMultiHash::contains() */ -/*! \fn template T QHash::value(const Key &key, const T &defaultValue = T()) const +/*! \fn template T QHash::value(const Key &key) const + \fn template T QHash::value(const Key &key, const T &defaultValue) const \overload Returns the value associated with the \a key. If the hash contains no item with the \a key, the function - returns \a defaultValue, which is a \l{default-constructed value} if the - parameter has not been specified. + returns \a defaultValue, or a \l{default-constructed value} if this + parameter has not been supplied. */ /*! \fn template T &QHash::operator[](const Key &key) @@ -1935,11 +1936,13 @@ size_t qHash(long double key, size_t seed) noexcept */ /*! - \fn template Key QHash::key(const T &value, const Key &defaultKey = Key()) const + \fn template Key QHash::key(const T &value) const + \fn template Key QHash::key(const T &value, const Key &defaultKey) const \since 4.3 - Returns the first key mapped to \a value, or \a defaultKey if the - hash contains no item mapped to \a value. + Returns the first key mapped to \a value. If the hash contains no item + mapped to \a value, returns \a defaultKey, or a \l{default-constructed + value}{default-constructed key} if this parameter has not been supplied. This function can be slow (\l{linear time}), because QHash's internal data structure is optimized for fast lookup by key, not @@ -2899,14 +2902,14 @@ size_t qHash(long double key, size_t seed) noexcept \sa keys(), values() */ -/*! \fn template T QMultiHash::value(const Key &key, const T &defaultValue = T()) const - \overload +/*! \fn template T QMultiHash::value(const Key &key) const + \fn template T QMultiHash::value(const Key &key, const T &defaultValue) const Returns the value associated with the \a key. If the hash contains no item with the \a key, the function - returns \a defaultValue, which is a \l{default-constructed value} if the - parameter has not been specified. + returns \a defaultValue, or a \l{default-constructed value} if this + parameter has not been supplied. If there are multiple items for the \a key in the hash, the value of the most recently @@ -3054,11 +3057,13 @@ size_t qHash(long double key, size_t seed) noexcept */ /*! - \fn template Key QMultiHash::key(const T &value, const Key &defaultKey = Key()) const + \fn template Key QMultiHash::key(const T &value) const + \fn template Key QMultiHash::key(const T &value, const Key &defaultKey) const \since 4.3 - Returns the first key mapped to \a value, or \a defaultKey if the - hash contains no item mapped to \a value. + Returns the first key mapped to \a value. If the hash contains no item + mapped to \a value, returns \a defaultKey, or a \l{default-constructed + value}{default-constructed key} if this parameter has not been supplied. This function can be slow (\l{linear time}), because QMultiHash's internal data structure is optimized for fast lookup by key, not diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index 3d92d95e62..0d5fa52111 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -926,28 +926,64 @@ public: return contains(key) ? 1 : 0; } - Key key(const T &value, const Key &defaultKey = Key()) const noexcept +private: + const Key *keyImpl(const T &value) const noexcept { if (d) { const_iterator i = begin(); while (i != end()) { if (i.value() == value) - return i.key(); + return &i.key(); ++i; } } - return defaultKey; + return nullptr; } - T value(const Key &key, const T &defaultValue = T()) const noexcept + +public: + Key key(const T &value) const noexcept + { + if (auto *k = keyImpl(value)) + return *k; + else + return Key(); + } + Key key(const T &value, const Key &defaultKey) const noexcept + { + if (auto *k = keyImpl(value)) + return *k; + else + return defaultKey; + } + +private: + T *valueImpl(const Key &key) const noexcept { if (d) { Node *n = d->findNode(key); if (n) - return n->value; + return &n->value; } - return defaultValue; + return nullptr; } +public: + T value(const Key &key) const noexcept + { + if (T *v = valueImpl(key)) + return *v; + else + return T(); + } + + T value(const Key &key, const T &defaultValue) const noexcept + { + if (T *v = valueImpl(key)) + return *v; + else + return defaultValue; + } + T &operator[](const Key &key) { detach(); @@ -1427,30 +1463,63 @@ public: return d->findNode(key) != nullptr; } - Key key(const T &value, const Key &defaultKey = Key()) const noexcept +private: + const Key *keyImpl(const T &value) const noexcept { if (d) { auto i = d->begin(); while (i != d->end()) { Chain *e = i.node()->value; if (e->contains(value)) - return i.node()->key; + return &i.node()->key; ++i; } } - return defaultKey; + return nullptr; } - T value(const Key &key, const T &defaultValue = T()) const noexcept +public: + Key key(const T &value) const noexcept + { + if (auto *k = keyImpl(value)) + return *k; + else + return Key(); + } + Key key(const T &value, const Key &defaultKey) const noexcept + { + if (auto *k = keyImpl(value)) + return *k; + else + return defaultKey; + } + +private: + T *valueImpl(const Key &key) const noexcept { if (d) { Node *n = d->findNode(key); if (n) { Q_ASSERT(n->value); - return n->value->value; + return &n->value->value; } } - return defaultValue; + return nullptr; + } +public: + T value(const Key &key) const noexcept + { + if (auto *v = valueImpl(key)) + return *v; + else + return T(); + } + T value(const Key &key, const T &defaultValue) const noexcept + { + if (auto *v = valueImpl(key)) + return *v; + else + return defaultValue; } T &operator[](const Key &key)