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

Stablize sorts used by path finders #1805

Merged
merged 8 commits into from
Dec 16, 2024
9 changes: 8 additions & 1 deletion rts/Sim/Path/HAPFS/PathDataTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ struct PathNode {
/// functor to define node priority
struct lessCost {
inline bool operator() (const PathNode* x, const PathNode* y) const {
return (x->fCost == y->fCost) ? (x->gCost < y->gCost) : (x->fCost > y->fCost);
if (x->fCost == y->fCost) {
if (x->gCost == y->gCost)
// This is needed to guarantee that the sorting is stable.
return (x->nodeNum < y->nodeNum);
else
return (x->gCost < y->gCost);
} else
return (x->fCost > y->fCost);
lostsquirrel1 marked this conversation as resolved.
Show resolved Hide resolved
}
};

Expand Down
24 changes: 19 additions & 5 deletions rts/Sim/Path/QTPFS/PathThreads.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,25 @@ namespace QTPFS {
};

struct SearchQueueNode {
bool operator < (const SearchQueueNode& n) const { return (heapPriority < n.heapPriority); }
bool operator > (const SearchQueueNode& n) const { return (heapPriority > n.heapPriority); }
bool operator == (const SearchQueueNode& n) const { return (heapPriority == n.heapPriority); }
bool operator <= (const SearchQueueNode& n) const { return (heapPriority <= n.heapPriority); }
bool operator >= (const SearchQueueNode& n) const { return (heapPriority >= n.heapPriority); }
// Sorting Comparisons
// These need to guarantee stable ordering, even if the sorting algorithm itself is not stable.
bool operator < (const SearchQueueNode& n) const {
if (heapPriority == n.heapPriority)
lostsquirrel1 marked this conversation as resolved.
Show resolved Hide resolved
return (nodeIndex > n.nodeIndex);
else
return (heapPriority < n.heapPriority);
}
bool operator > (const SearchQueueNode& n) const {
if (heapPriority == n.heapPriority)
return (nodeIndex < n.nodeIndex);
else
return (heapPriority > n.heapPriority);
}

// General Comparisons
bool operator == (const SearchQueueNode& n) const { return (heapPriority == n.heapPriority); }
bool operator <= (const SearchQueueNode& n) const { return (heapPriority <= n.heapPriority); }
bool operator >= (const SearchQueueNode& n) const { return (heapPriority >= n.heapPriority); }
lostsquirrel1 marked this conversation as resolved.
Show resolved Hide resolved

SearchQueueNode(int index, float newPriorty)
: heapPriority(newPriorty)
Expand Down