refs #484 Optimize getClosestObjects by using CSequence::partiallySort.

This commit is contained in:
Mathew Sutcliffe
2015-10-19 02:07:21 +01:00
parent eb45d7a409
commit c45157078f
2 changed files with 26 additions and 1 deletions

View File

@@ -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;
}

View File

@@ -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;