Skip to content

Preserve equivalent element order during deserialization of multimap and multiset (#865) - #890

Open
Tyagiquamar wants to merge 1 commit into
USCiLab:masterfrom
Tyagiquamar:fix-multimap-multiset-order-865
Open

Tyagiquamar wants to merge 1 commit into
USCiLab:masterfrom
Tyagiquamar:fix-multimap-multiset-order-865

Conversation

@Tyagiquamar

Copy link
Copy Markdown

Summary of Changes

Fixes #865

In standard C++ containers with equivalent keys (std::multimap and std::multiset), the relative order of elements that compare equivalent is required to be preserved as their order of insertion.

Previously, pair_associative_container.hpp and set.hpp initialized their insertion hint to map.begin() / set.begin(), and then passed the iterator returned by emplace_hint() into the next iteration. Because associative container hint insertion inserts the new element immediately prior to the hint position, using the previous element's iterator as a hint caused each successive equivalent key to be inserted before the preceding one, reversing the order of equivalent keys on every serialization round-trip.

This change fixes the deserialization behavior:

  • Updated pair_associative_container.hpp to pass map.end() as the insertion hint. Because serialized archives are written in forward sorted order, incoming elements belong at the end of the container, preserving the original sequence of equivalent keys in O(1) amortized insertion time.
  • Updated set.hpp to pass set.end() as the insertion hint for set and multiset containers.
  • Enhanced unittests/multimap.hpp and unittests/multiset.hpp with full sequence equality assertions (std::equal) in addition to count checks.
  • Added deterministic duplicate-key ordering tests across Binary, Portable Binary, XML, and JSON archives in unittests/multimap.cpp and unittests/multiset.cpp.

Verification

All unit tests passed in Linux Docker (gcc:14.3.0):

[doctest] test cases: 4 | 4 passed | 0 failed
[doctest] assertions: 804008 | 804008 passed | 0 failed
[doctest] test cases: 4 | 4 passed | 0 failed
[doctest] assertions: 404008 | 404008 passed | 0 failed
100% tests passed, 0 tests failed out of 8

@redchairman

redchairman commented Oct 1, 2026 via email

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Original order of keys lost after deserialization for std::multimap and std::multiset

2 participants