mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-05 01:05:34 +08:00
refs #484 Optimize getClosestObjects by using CSequence::partiallySort.
This commit is contained in:
@@ -48,6 +48,17 @@ namespace BlackMisc
|
||||
});
|
||||
}
|
||||
|
||||
template <class OBJ, class CONTAINER>
|
||||
CONTAINER IGeoObjectList<OBJ, CONTAINER>::findClosest(int number, const ICoordinateGeodetic &coordinate) const
|
||||
{
|
||||
CONTAINER closest = this->container().partiallySorted(number, [ & ](const OBJ & a, const OBJ & b)
|
||||
{
|
||||
return calculateEuclideanDistanceSquared(a, coordinate) < calculateEuclideanDistanceSquared(b, coordinate);
|
||||
});
|
||||
closest.truncate(number);
|
||||
return closest;
|
||||
}
|
||||
|
||||
template <class OBJ, class CONTAINER>
|
||||
void IGeoObjectWithRelativePositionList<OBJ, CONTAINER>::calculcateDistanceAndBearingToPosition(const ICoordinateGeodetic &position)
|
||||
{
|
||||
@@ -82,13 +93,19 @@ namespace BlackMisc
|
||||
this->container().sort([ & ](const OBJ & a, const OBJ & b) { return a.getDistanceToOwnAircraft() < b.getDistanceToOwnAircraft(); });
|
||||
}
|
||||
|
||||
template <class OBJ, class CONTAINER>
|
||||
void IGeoObjectWithRelativePositionList<OBJ, CONTAINER>::partiallySortByDistanceToOwnAircraft(int number)
|
||||
{
|
||||
this->container().partiallySort(number, [ & ](const OBJ & a, const OBJ & b) { return a.getDistanceToOwnAircraft() < b.getDistanceToOwnAircraft(); });
|
||||
}
|
||||
|
||||
template <class OBJ, class CONTAINER>
|
||||
CONTAINER IGeoObjectWithRelativePositionList<OBJ, CONTAINER>::getClosestObjects(int number) const
|
||||
{
|
||||
if (number < 1) { return CONTAINER(); }
|
||||
if (this->container().size() >= number) { return (this->container()); }
|
||||
CONTAINER closest(this->container());
|
||||
closest.sortByDistanceToOwnAircraft();
|
||||
closest.partiallySortByDistanceToOwnAircraft(number);
|
||||
closest.truncate(number);
|
||||
return closest;
|
||||
}
|
||||
|
||||
@@ -50,6 +50,11 @@ namespace BlackMisc
|
||||
*/
|
||||
CONTAINER findWithinRange(const BlackMisc::Geo::ICoordinateGeodetic &coordinate, const BlackMisc::PhysicalQuantities::CLength &range) const;
|
||||
|
||||
/*!
|
||||
* Find 0..n objects closest to the given coordinate.
|
||||
*/
|
||||
CONTAINER findClosest(int number, const BlackMisc::Geo::ICoordinateGeodetic &coordinate) const;
|
||||
|
||||
protected:
|
||||
//! Constructor
|
||||
IGeoObjectList();
|
||||
@@ -79,6 +84,9 @@ namespace BlackMisc
|
||||
//! If distance is already set, just sort
|
||||
void sortByDistanceToOwnAircraft();
|
||||
|
||||
//! Sort the first n closest objects
|
||||
void partiallySortByDistanceToOwnAircraft(int number);
|
||||
|
||||
//! Get n closest objects
|
||||
CONTAINER getClosestObjects(int number) const;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user