< Summary

Information
Class: Gravitas.CollisionHandling.ContactWarmStartCache2D
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/CollisionHandling/Contacts/2D/ContactWarmStartCache2D.cs
Line coverage
100%
Covered lines: 67
Uncovered lines: 0
Coverable lines: 67
Total lines: 138
Line coverage: 100%
Branch coverage
100%
Covered branches: 66
Total branches: 66
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Count()100%44100%
GetGroup(...)100%22100%
SetGroup(...)100%22100%
Clear()100%22100%
Set(...)100%1818100%
TryGet(...)100%44100%
Remove(...)100%88100%
Retain(...)100%1414100%
Find(...)100%88100%
RemoveGroup(...)100%44100%

File(s)

/home/runner/work/Gravitas/Gravitas/src/Gravitas/CollisionHandling/Contacts/2D/ContactWarmStartCache2D.cs

#LineLine coverage
 1//=======================================================================
 2// ContactWarmStartCache2D.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 FixedMathSharp;
 9
 10namespace Gravitas.CollisionHandling;
 11
 12
 13
 14/// <summary>Retained pair-owned warm starts with independent two-point groups.</summary>
 15internal struct ContactWarmStartCache2D
 16{
 17    private ContactWarmStartGroup2D first;
 18    private SwiftCollections.SwiftList<ContactWarmStartGroup2D>? additional;
 19    internal int GroupCount { get; private set; }
 20    public int Count
 21    {
 22        get
 23        {
 19624            int count = 0;
 95625            for (int i = 0; i < GroupCount; i++) count += i == 0 ? first.Count : additional!.InnerArray[i - 1].Count;
 19626            return count;
 27        }
 28    }
 29
 30    internal ContactWarmStartGroup2D GetGroup(int index) =>
 19731        index == 0 ? first : additional!.InnerArray[index - 1];
 32
 33    private void SetGroup(int index, in ContactWarmStartGroup2D value)
 34    {
 693235        if (index == 0) first = value;
 29636        else additional!.InnerArray[index - 1] = value;
 29637    }
 38
 39    public void Clear()
 40    {
 155841        GroupCount = 0;
 155842        first = default;
 155843        additional?.FastClear();
 23444    }
 45
 46    internal void Set(in ContactGroupKey key, ulong contactId, Fixed64 normalImpulse, Fixed64 tangentImpulse)
 47    {
 3359848        int index = 0, order = -1;
 2030049        while (index < GroupCount)
 50        {
 51            // Key lookup must not copy the group's two retained impulse rows.
 1671052            ref readonly ContactWarmStartGroup2D candidate = ref (index == 0 ? ref first : ref additional!.InnerArray[in
 1671053            order = candidate.Key.CompareTo(key);
 1671054            if (order >= 0) break;
 350155            index++;
 56        }
 1679957        if (index == GroupCount || order != 0)
 58        {
 359859            if (GroupCount > 0)
 60            {
 28661                additional ??= new SwiftCollections.SwiftList<ContactWarmStartGroup2D>();
 28662                additional.Add(default);
 60863                for (int i = GroupCount; i > index; i--) SetGroup(i, GetGroup(i - 1));
 64            }
 359865            SetGroup(index, new ContactWarmStartGroup2D { Key = key });
 359866            GroupCount++;
 67        }
 1679968        ref ContactWarmStartGroup2D current = ref (index == 0 ? ref first : ref additional!.InnerArray[index - 1]);
 1679969        current.Set(contactId, normalImpulse, tangentImpulse);
 1679970    }
 71
 72    internal bool TryGet(in ContactGroupKey key, ulong contactId, out ContactWarmStartImpulse impulse)
 73    {
 1782974        int index = Find(key);
 1782975        if (index >= 0)
 1355376            return index == 0 ? first.TryGet(contactId, out impulse)
 1355377                : additional!.InnerArray[index - 1].TryGet(contactId, out impulse);
 427678        impulse = default;
 427679        return false;
 80    }
 81
 82    internal bool Remove(in ContactGroupKey key, ulong contactId)
 83    {
 1584        int index = Find(key);
 2085        if (index < 0) return false;
 1086        ref ContactWarmStartGroup2D current = ref (index == 0 ? ref first : ref additional!.InnerArray[index - 1]);
 1187        if (!current.Remove(contactId)) return false;
 1588        if (current.Count == 0) RemoveGroup(index);
 989        return true;
 90    }
 91
 92    internal void Retain(ContactManifold2D manifold)
 93    {
 3844094        for (int group = GroupCount - 1; group >= 0; group--)
 95        {
 940596            ref ContactWarmStartGroup2D cached = ref (group == 0 ? ref first : ref additional!.InnerArray[group - 1]);
 940597            int current = manifold.FindGroup(cached.Key);
 940598            if (current < 0)
 99            {
 9100                RemoveGroup(group);
 9101                continue;
 102            }
 9396103            ref ContactGroup2D admitted = ref manifold.GetGroup(current);
 45928104            for (int point = cached.Count - 1; point >= 0; point--)
 105            {
 13568106                ulong id = cached.GetContactId(point);
 13568107                bool present = false;
 92854108                for (int i = 0; i < admitted.Count; i++) present |= admitted[i].ContactId == id;
 16548109                if (!present) cached.Remove(id);
 110            }
 12254111            if (cached.Count == 0) RemoveGroup(group);
 112        }
 9815113    }
 114
 115    private int Find(in ContactGroupKey key)
 116    {
 35688117        int low = 0, high = GroupCount - 1;
 20435118        while (low <= high)
 119        {
 16154120            int middle = low + ((high - low) >> 1);
 16154121            ref readonly ContactWarmStartGroup2D current = ref (middle == 0 ? ref first : ref additional!.InnerArray[mid
 16154122            int order = current.Key.CompareTo(key);
 29717123            if (order == 0) return middle;
 4459124            if (order < 0) low = middle + 1;
 723125            else high = middle - 1;
 126        }
 4281127        return -1;
 128    }
 129
 130    private void RemoveGroup(int index)
 131    {
 5758132        for (int i = index; i < GroupCount - 1; i++) SetGroup(i, GetGroup(i + 1));
 3034133        if (GroupCount > 1) additional!.RemoveAt(additional.Count - 1);
 2712134        else first = default;
 2873135        GroupCount--;
 2873136    }
 137
 138}