Jonathan Boccara's blog

A smart iterator for aggregating new elements with existing ones in a map or a set

Published March 21, 2017 - 4 Comments

One thing that is cruelly lacking with std::inserter is that it can do just this: inserting. In some situations this is not enough, in particular for a map: what if an element with the same key is already there? std::inserter, since it calls std::map::insert, will not do anything at all in this case. But maybe we would like to replace the current element with the new one? Or maybe a more complex aggregation behaviour is needed, like adding the values together for example? This last case has been encountered in the project of Coarse Grain Automatic Differentiation when composing derivatives to multiple variables.

This post is part of a series on smart iterators in sorted containers:

Said differently, we need a even smarter iterator, to which you could describe what to do when trying to insert elements with keys already present in the map. The most generic expression I found was to provide an aggregator, that is, a function that describes how to merge two values, for elements having the same key. This would let new elements being “inserted” into the map regardless of whether or not their key is already present, and still keep the unicity of the key in the map (so this solution is effectively different from using a multimap).

Here is how map_aggregator could be implemented:

Here is a helper function to instantiate it and deduce template parameters:

This has several major differences with std::inserter:

  • map_aggregator embarks a aggregator function in its constructor,
  • operator=  aggregates the new value into the existing element by using the aggregator function, if the key is already present in the collection.
  • Like sorted_inserter presented in the previous post of this series, you don’t have to pass a hint. (In fact you could pass it if you knew it, but to alleviate the code in this post I am not showing this functionality here.)

Here is a how map_aggregator can be used:

Here the first call to map_aggregator is not strictly necessary, since the collection results is empty. It could be replaced by an simple std::inserter or, more to the point, by a sorted_inserter presented in the first post of this series.

What about sets?

The above aggregator has been designed to work with maps, that contain pairs of keys and values. But sometimes the key of an element is embedded inside the element, like with a reference number that is a member of an object for example. In this case you may want to use a set with a customized comparison based on the subpart of the element that represents the key.

We can then define another smart iterator for aggregating into a set, with much the same logic as the one for maps, the main difference lying in the operator=:

The thing with sets is that they don’t allow their values to be modified (some platforms let you get away with it but relying on that prevents code from being portable). Therefore, we have to remove the old value and then add the aggregated one. This is what operator= does here when it finds that the element was there already.

For clarity, the rest of the implementation of this inserter in sets is omitted in the writing of this post, but it’s essentially the same as the one for maps.

To see the entire code of the components presented here, you can head over to the dedicated GitHub repository.


Over to you

Do you find these components of this series useful? Have you encountered the problems they are solving? How would you have gone about solving them differently?

Whether you are a new reader on Fluent C++ or a regular one, your feedback matters to me. And not only on this particular series by the way. Depending on the size and visibility you want your feedback to have you can drop a comment below, or use email or Twitter to get in touch directly. Hoping to hear from you!

Related articles:

Share this post! Facebooktwittergoogle_pluslinkedin    Don't want to miss out ? Follow:   twitterlinkedinrss

Receive regular updates to make your code more expressive.