October 19, 2010

dusktreader dusktreader
Lab Rat
59 posts

getDefault function for QMap and other keyed containers

 

The QMap class already has the functionality to return a default value if an item isn’t found at the specified key. This is found in
const T QMap::value ( const Key & key, const T & defaultValue ) const [doc.qt.nokia.com]

This functionality is very useful, but I often find with keyed collections like a map, that I would like to insert an item with some default value at a given key. I usually implement some sort of getDefault() function. This could be defined as such:

  1. /** Gets a value associated with a key from the QMap.
  2.   * If no entry matching the key is found, the default value is inserted and returned
  3.   * @param  key - The key with which to look up the item
  4.   * @param  defaultValue - The default value to insert if the associated key is not found in the map
  5.   * @return A reference to the value associated with the given key.
  6.   *         If there was no value at the key, this will be a reference to the newly added default value
  7.   */
  8. const T& QMap::getDefault( const Key& key, const T& defaultValue );

I have added this as a suggestion in the bugtracker, but I was interested to know what other developers think of this sort of functionality. What sort of problems could this introduce, and why don’t we see this sort of functionality added with many implementations of keyed containers in Qt and other libraries?

 Signature 

“Machines take me by surprise with great frequency.” —Alan Mathison Turing

5 replies

October 19, 2010

Frank Frank
Lab Rat
17 posts

I find the concept a bit awkward and the name totally misleading. A “getFoo()” method that modifies the object? Not very intuitive in my book. one could have insertIfNotExistant() or sth like that, but it would still appear as API clutter to me. What is your use case where this would make more sense than value() with default value?

October 19, 2010

Alexander Kuchumov Alexander Kuchumov
Lab Rat
379 posts

In which case do you want to use this function? I guess, it’s redundant function.

  1. if ( !QMap.contains( key ) )
  2.   QMap.insert(key, defValue)
  3. T = QMap[key];

It’s simple.

October 20, 2010

Andre Andre
Robot Herder
6399 posts

I don’t get the use case for this function either. What is the use of storing the default value in the keyed container at all? It sounds much more efficient to me not to store data points that have the default value. If you want something else there than the default, then it obviously isn’t the default value.

However, I must say that I implemented a matrix class once, and for that class I did find it was useful to set a default value explicitly. However, that value was not inserted into the data backend, it was just returned if there was not actual data in the internal storage. I am wondering if that is more efficient than passing a constructed value of the correct type each time, especially if that type is a bit more complex than a basic data type?

October 21, 2010

dusktreader dusktreader
Lab Rat
59 posts

@ Alexander — Admittedly, it is easy to explicitly code this sort of functionality. As you pointed out, it is three lines of code. However, if this sort of functionality is needed in more than one place, it would be nice to package it into a single function call to avoid repetition. I’ve used this functionality often. I like to avoid redundancy in implementing a common use-case.

@Frank — Yes, I know the name is awkward, and it needs to be improved. It is basic functionality, but I would think that other programmers have used the same functionality often.

@Andre — You would not be storing the default value in the keyed container, you would be binding a new keyed entry in the container to an initial default value. Imagine, for a simple case, that you are using a QMap as an indexing word counter. You are going to be initializing each entry in the map with zero every time a new word is encountered. Now, you could use the defaultValue argument of the value() function to return 0 if the word didn’t exist in the index. However, you would still have to set the entry key-value pair explicitly.

 Signature 

“Machines take me by surprise with great frequency.” —Alan Mathison Turing

October 21, 2010

dusktreader dusktreader
Lab Rat
59 posts

Here’s an example of my most recent use of this functionality from some code that I edited a few days ago:

  1. if( !_dataMap.contains( dataKey ) )
  2.     _dataMap.insert( dataKey, Datum( sampleName, elemName) );
  3. Datum& datum = _dataMap[dataKey];

 Signature 

“Machines take me by surprise with great frequency.” —Alan Mathison Turing

 
  ‹‹ Problem with QImage and JPEG      error in compiling helloworld program ››

You must log in to post a reply. Not a member yet? Register here!