3

The standard specifies (23.4.4.2:5, etc.) that constructing all four ordered associative containers (map, multimap, set, multiset) from a range [first, last) shall be linear in N = last - first if the range is already sorted.

For merging a range (e.g. a container of the same type) into an existing container, however, 23.2.4:8 table 102 specifies only that inserting a range [i, j) into the container shall have complexity N log(a.size() + N) where N = distance(i, j). This would seem to indicate that using the hinted insert:

for (auto it = a.begin(); i != j; ++i)
    it = next(a.insert(it, *i));

could be more efficient than a.insert(i, j), having complexity strictly less than N log(a.size() + N), depending on the proportion of hints that are correct (except in the trivial case where N == 0); and being linear, since every hint is correct, if a is initially empty.

Is this a deficiency in the standard, or is there some other language (or facility) that covers this case? In practice, do implementations optimise for merging an ordered associative container into another?

Credit to https://stackoverflow.com/a/11362162/567292 for inspiring this question.

Community
  • 1
  • 1
ecatmur
  • 152,476
  • 27
  • 293
  • 366
  • +1 I was wondering the same thing when I read the "inspiring" answer. – Andrew Durward Jul 06 '12 at 14:53
  • If the answer to your last part is "yes", then the answer to the question that inspires it is probably to use my first code snippet. Which would be nice, since it's also the simplest :-) – Steve Jessop Jul 06 '12 at 14:55

1 Answers1

1

I would assume in the latter case that the standard is only specifying the worst-case guaranteed run-time performance of the associative container merge, leaving it up to the implementation to find faster performance short-cuts if they are possible. Keep in mind that unlike other languages, the C++ specification is exactly that - a spec. There is no "reference" implementation. Thus one could write an implementation that only meets certain performance criteria, or one could decide to implement features that would exceed the specified performance criteria. In the end though the specification is designed to give you an "at minimum" performance guarantee in the way certain algorithms are implemented.

That being said, should you always use hinting for every element, your worst-case performance actually ends up being twice as slow as if you had simply inserted without hinting. So always using hinting is not necessarily a panacea.

Jason
  • 31,834
  • 7
  • 59
  • 78