Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

elements of ordered_non_unique index with same key don't preserve insertion order when inserted as range #68

Closed
SCFrench opened this issue May 19, 2023 · 2 comments

Comments

@SCFrench
Copy link

The program below as-is inserts a set of values into a multi_index_container with ordered_non_unique indices via a for loop, with each element being inserted individually. When the elements with the key "Bob" (as identified by equal_range) are iterated over, they appear in the same order as they were inserted (which can be seen by looking at the age of each "Bob"). However, if you change the #if 1 to #if 0 then the elements are inserted via a call to the iterator range-based overload of insert. In this case, the ages of the people named "Bob" are not displayed in the order of insertion (which I assume would be forward pass from the begin iterator to the end iterator).

The following demo program can also be found here: https://gcc.godbolt.org/z/EvG4rW1ba

#include <iostream>
#include <string>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>

using namespace boost::multi_index;

struct person {
   std::string name;
   int age;
};

typedef multi_index_container<
   person,
   indexed_by<
      ordered_non_unique<member<person, std::string, &person::name>>,
      ordered_non_unique<member<person, int, &person::age>>
   > 
> person_set;

int main()
{
  std::vector<person> people = {
      {"Bob", 32},
      {"Charlie", 32},
      {"Bob", 32},
      {"Emily", 32},
      {"Bob", 30},
      {"Alex", 32},
      {"Bob", 56},
      {"Bob", 32}
   };

   person_set persons;

#if 1
   for (auto const & p : people)
   {
    persons.insert(p);
   }
#else
  persons.insert(people.begin(), people.end());
#endif

   std::cout << "People named Bob: \n";
   auto &name_index = persons.get<0>();
   auto range2 = name_index.equal_range("Bob");
   for (auto it = range2.first; it != range2.second; ++it) {
      std::cout << it->name << ", " << it->age << "\n";
   }

   return 0;
}
@SCFrench SCFrench changed the title ordered_non_unique elements with same key don't preserve insertion order when inserted as range elements of ordered_non_unique index with same key don't preserve insertion order when inserted as range May 19, 2023
@joaquintides
Copy link
Member

Thanks for the report! Fixed in f8143b9 if you'd like to confirm locally.

@SCFrench
Copy link
Author

Confirmed locally. Thanks for the fast response!

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

No branches or pull requests

2 participants