< Summary

Information
Class: Gravitas.Queries.PhysicsMixedHitSorter
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/Queries/Mixed/PhysicsMixedHitSorter.cs
Line coverage
100%
Covered lines: 40
Uncovered lines: 0
Coverable lines: 40
Total lines: 86
Line coverage: 100%
Branch coverage
100%
Covered branches: 28
Total branches: 28
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SortByDistance(...)100%11100%
SortByDistance(...)100%66100%
ComesBefore(...)100%11100%
SiftDown(...)100%1010100%
Compare(...)100%1212100%
Swap(...)100%11100%

File(s)

/home/runner/work/Gravitas/Gravitas/src/Gravitas/Queries/Mixed/PhysicsMixedHitSorter.cs

#LineLine coverage
 1//=======================================================================
 2// PhysicsMixedHitSorter.cs
 3//=======================================================================
 4// MIT License, Copyright (c) 2026–present David Oravsky (mrdav30)
 5// See LICENSE file in the project root for full license information.
 6//=======================================================================
 7
 8using SwiftCollections;
 9using System.Runtime.CompilerServices;
 10
 11namespace Gravitas.Queries;
 12
 13internal static class PhysicsMixedHitSorter
 14{
 15    internal static void SortByDistance(SwiftList<PhysicsMixedHit> hits)
 16    {
 707517        SortByDistance(hits, 0, hits.Count);
 707518    }
 19
 20    internal static void SortByDistance(SwiftList<PhysicsMixedHit> hits, int start, int count)
 21    {
 707522        if (count < 2)
 699823            return;
 24
 52425        for (int root = (count / 2) - 1; root >= 0; root--)
 18526            SiftDown(hits, start, root, count);
 27
 81828        for (int end = count - 1; end > 0; end--)
 29        {
 33230            Swap(hits, start, start + end);
 33231            SiftDown(hits, start, 0, end);
 32        }
 7733    }
 34
 99135    internal static bool ComesBefore(PhysicsMixedHit left, PhysicsMixedHit right) => Compare(left, right) < 0;
 36
 37    private static void SiftDown(SwiftList<PhysicsMixedHit> hits, int start, int root, int count)
 38    {
 54839        while (true)
 40        {
 106541            int child = (root * 2) + 1;
 106542            if (child >= count)
 48143                return;
 44
 58445            int swapIndex = root;
 58446            if (ComesBefore(hits[start + swapIndex], hits[start + child]))
 54847                swapIndex = child;
 48
 58449            int right = child + 1;
 58450            if (right < count && ComesBefore(hits[start + swapIndex], hits[start + right]))
 18351                swapIndex = right;
 52
 58453            if (swapIndex == root)
 3654                return;
 55
 54856            Swap(hits, start + root, start + swapIndex);
 54857            root = swapIndex;
 58        }
 59    }
 60
 61    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 62    private static int Compare(PhysicsMixedHit left, PhysicsMixedHit right)
 63    {
 99164        int distance = left.Distance.CompareTo(right.Distance);
 99165        if (distance != 0)
 98466            return distance;
 67
 768        int left3D = left.Collider3D?.Id ?? -1;
 769        int right3D = right.Collider3D?.Id ?? -1;
 770        int collider3D = left3D.CompareTo(right3D);
 771        if (collider3D != 0)
 372            return collider3D;
 73
 474        int left2D = left.Collider2D?.Id ?? -1;
 475        int right2D = right.Collider2D?.Id ?? -1;
 476        return left2D.CompareTo(right2D);
 77    }
 78
 79    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 80    private static void Swap(SwiftList<PhysicsMixedHit> hits, int left, int right)
 81    {
 88082        PhysicsMixedHit temp = hits[left];
 88083        hits[left] = hits[right];
 88084        hits[right] = temp;
 88085    }
 86}