QMap::findValue

Discuss anything related to product development
Post Reply
seasoned_geek
Posts: 254
Joined: Thu Jun 11 2020 12:18 pm

QMap::findValue

Post by seasoned_geek »

This is something that has been needed since QMap was originally conceived. There must literally be tens of thousands of programs that have code much like the following:

Code: Select all

QString retVal;
bool foundFlg = false;

QMapIterator<QString, int> iter(map);
 
while (iter.hasNext()  &&  !foundFlg) 
{
    iter.next();
    if (iter.value() == valueImLookingFor)
    {
        retVal = iter.key();
        foundFlg = true;
    }
}

Really need a findValue() that operates like find()

https://www.copperspice.com/docs/cs_api/class_qmap.html#a10fa252c264c8b8aa4d8d374cddd6289
barbara
Posts: 446
Joined: Sat Apr 04 2015 2:32 am
Contact:

Re: QMap::findValue

Post by barbara »

If I understand your question you are looking for a method in QMap which will return the first key that matches a particular value. Is this what you are looking for?

Code: Select all

QString retVal;
retVal = map.key( valueImLookingFor ); 
seasoned_geek
Posts: 254
Joined: Thu Jun 11 2020 12:18 pm

Re: QMap::findValue

Post by seasoned_geek »

No, that is close. Btw, that seems like a really bad name for such a method, but that is my opinion.

Code: Select all

iterator 	find (const Key &key)

iterator 	findValue ((const Val &value)
I apologize if my request was unclear. I was tired. Now that I go back and look at it, the code snippet I stole from the documentation and quick-hacked (AGILE) was bad too.

The problem with

Code: Select all

const Key 	key (const Val &value, const Key &defaultKey=Key ()) const
is clear in the documentation.
const Key QMap< Key, Val, C >::key ( const Val & value,
const Key & defaultKey = Key()
) const

Returns the first key with value. If the map contains no item with value then defaultKey is returned.

This method can be slow (linear time), because the internal data structure is optimized for fast lookup by key, not by value.
To find a miss you have to have a known bogus key. The documentation is rather murky about what said "defaultKey" will have for a value. If it is an int will it give you -1 for an illegal subscript? If it is a QString key, what could it possibly give you to indicate a miss?
iterator QMap< Key, Val, C >::find ( const Key & key )
inline

Returns an iterator pointing to the item with the specified key. If the map contains no item with this key, the method returns end().

See also
constFind(), value(), values(), lowerBound(), upperBound()

No matter what the value is, end() tells us we didn't find it. More importantly,

Code: Select all

QMapIterator<QString, int> iter = findValue( "my value");

while ( iter.hasNext())
{
  iter.next();
  blah blah blah
}
should always work and though it is linear, we should find every key for every matching value. We don't have to translate bogus keys and trap for bogus values. We also don't have to worry about an accidental new empty item created for bogus key.
ansel
Posts: 152
Joined: Fri Apr 10 2015 8:23 am

Re: QMap::findValue

Post by ansel »

Thanks for explaining your suggestion, we have added this to our roadmap for a future release. We will also update the documentation for defaulted values.
Ansel Sermersheim
CopperSpice Cofounder
barbara
Posts: 446
Joined: Sat Apr 04 2015 2:32 am
Contact:

Re: QMap::findValue

Post by barbara »

In our updated CS API documentation we added a link in several places where a default constructed value is used if no data is passed. For example, the updated docs for QMap::key() contain the following information.
If no defaultKey was passed it will be value initialized using the data type of Key. Refer to default constructed elements for additional information.
The phrase "default constructed elements" links to a page on "Container Classes " which includes an introduction to the terms default initialized and value initialized. Here is a link to this page:

https://www.copperspice.com/docs/cs_api/containers-c.html#containers-default-elements

Barbara
Post Reply