Hi.
Here I present "intrusive_sort" which works like std::sort except that you
pass it a custom "swapper" functor that is used to exchange elements as
required by the sort algorithm. The swapper is passed the iterators of
the two elements that require exchanging.
Example usage:
neolib::intrusive_sort(component_data().begin(), component_data().end(),
[this](auto lhs, auto rhs)
{
std::swap(*lhs, *rhs);
auto lhsIndex = lhs - component_data().begin();
auto rhsIndex = rhs - component_data().begin();
std::swap(index()[lhsIndex], index()[rhsIndex]);
std::swap(reverse_index()[index()[lhsIndex]],
reverse_index()[index()[rhsIndex]]);
},
[](const rigid_body& lhs, const rigid_body& rhs)
{
return lhs.mass > rhs.mass;
});
As you can see intrusive_sort allows us to sort multiple containers in
parallel in a single operation. An alternative approach using a zip
iterator and std::sort wouldn't work in this case as 'reverse_index()' is
a sparse reverse-lookup array.
intrusive_sort has the same complexity guarantees as std::sort.
https://github.com/i42output/neolib/blob/master/include/neolib/intrusive_sort.hpp
/Flibble
--
"Suppose it’s all true, and you walk up to the pearly gates, and are
confronted by God," Bryne asked on his show The Meaning of Life. "What
will Stephen Fry say to him, her, or it?"
"I’d say, bone cancer in children? What’s that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery
that is not our fault. It’s not right, it’s utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a
world that is so full of injustice and pain. That’s what I would say."