< Summary

Information
Class: Gravitas.Constraints.ConstraintEndpointJointIndex<T>
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/Constraints/ConstraintEndpointJointIndex.cs
Line coverage
100%
Covered lines: 86
Uncovered lines: 0
Coverable lines: 86
Total lines: 183
Line coverage: 100%
Branch coverage
100%
Covered branches: 16
Total branches: 16
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
Add(...)100%11100%
Remove(...)100%11100%
TryGetLast(...)100%22100%
TryGetPrevious(...)100%11100%
Clear()100%11100%
Append(...)100%22100%
RemoveEndpoint(...)100%66100%
GetEndpoint(...)100%22100%
EnsureJointCapacity(...)100%44100%
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%

File(s)

/home/runner/work/Gravitas/Gravitas/src/Gravitas/Constraints/ConstraintEndpointJointIndex.cs

#LineLine coverage
 1//=======================================================================
 2// ConstraintEndpointJointIndex.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 System;
 9using SwiftCollections;
 10
 11namespace Gravitas.Constraints;
 12
 13/// <summary>
 14/// Tracks registered joint IDs in stable endpoint order without scanning a context's peak joint range.
 15/// </summary>
 16internal sealed class ConstraintEndpointJointIndex<TBody>
 17    where TBody : class
 18{
 19    private const int DefaultJointCapacity = 64;
 20
 2038221    private readonly SwiftDictionary<TBody, EndpointChain> _chainsByBody = new();
 2038222    private JointEndpoints[] _endpointsByJoint = new JointEndpoints[DefaultJointCapacity];
 23
 24    internal void Add(TBody bodyA, TBody bodyB, int jointId)
 25    {
 58626        ChronicleGuard.ThrowIfTrue(
 58627            ReferenceEquals(bodyA, bodyB),
 58628            nameof(bodyB),
 58629            "A joint endpoint index requires two distinct bodies.");
 58630        EnsureJointCapacity(jointId + 1);
 58631        ref JointEndpoints endpoints = ref _endpointsByJoint[jointId];
 58632        ChronicleGuard.ThrowIfTrue(
 58633            endpoints.IsRegistered,
 58634            nameof(jointId),
 58635            "Joint endpoint ownership is already registered.");
 36
 58637        endpoints = new JointEndpoints(bodyA, bodyB);
 58638        Append(bodyA, jointId, ref endpoints.EndpointA);
 58639        Append(bodyB, jointId, ref endpoints.EndpointB);
 58640        endpoints.IsRegistered = true;
 58641    }
 42
 43    internal void Remove(int jointId)
 44    {
 5845        ref JointEndpoints endpoints = ref _endpointsByJoint[jointId];
 5846        RemoveEndpoint(jointId, ref endpoints.EndpointA);
 5847        RemoveEndpoint(jointId, ref endpoints.EndpointB);
 5848        endpoints = default;
 5849    }
 50
 51    internal bool TryGetLast(TBody body, out int jointId)
 52    {
 232253        if (_chainsByBody.TryGetValue(body, out EndpointChain chain))
 54        {
 8055            jointId = chain.LastJointId;
 8056            return true;
 57        }
 58
 224259        jointId = -1;
 224260        return false;
 61    }
 62
 63    internal bool TryGetPrevious(TBody body, int jointId, out int previousJointId)
 64    {
 5865        previousJointId = GetEndpoint(jointId, body).PreviousJointId;
 5866        return previousJointId != 0;
 67    }
 68
 69    internal void Clear()
 70    {
 2060871        _chainsByBody.Clear();
 2060872        Array.Clear(_endpointsByJoint, 0, _endpointsByJoint.Length);
 2060873    }
 74
 75    private void Append(TBody body, int jointId, ref EndpointLink endpoint)
 76    {
 117277        endpoint.Body = body;
 117278        if (!_chainsByBody.TryGetValue(body, out EndpointChain chain))
 79        {
 68280            _chainsByBody.Add(body, new EndpointChain(jointId, jointId, 1));
 68281            return;
 82        }
 83
 49084        endpoint.PreviousJointId = chain.LastJointId;
 49085        ref EndpointLink previous = ref GetEndpoint(chain.LastJointId, body);
 49086        previous.NextJointId = jointId;
 49087        chain.LastJointId = jointId;
 49088        chain.Count++;
 49089        _chainsByBody[body] = chain;
 49090    }
 91
 92    private void RemoveEndpoint(int jointId, ref EndpointLink endpoint)
 93    {
 11694        TBody body = endpoint.Body!;
 11695        ChronicleGuard.ThrowIfTrue(
 11696            !_chainsByBody.TryGetValue(body, out EndpointChain chain),
 11697            nameof(jointId),
 11698            "Joint endpoint chain is missing its body.");
 99
 116100        if (endpoint.PreviousJointId == 0)
 100101            chain.FirstJointId = endpoint.NextJointId;
 102        else
 16103            GetEndpoint(endpoint.PreviousJointId, body).NextJointId = endpoint.NextJointId;
 104
 116105        if (endpoint.NextJointId == 0)
 101106            chain.LastJointId = endpoint.PreviousJointId;
 107        else
 15108            GetEndpoint(endpoint.NextJointId, body).PreviousJointId = endpoint.PreviousJointId;
 109
 116110        chain.Count--;
 116111        if (chain.Count == 0)
 89112            _chainsByBody.Remove(body);
 113        else
 27114            _chainsByBody[body] = chain;
 27115    }
 116
 117    private ref EndpointLink GetEndpoint(int jointId, TBody body)
 118    {
 579119        ref JointEndpoints endpoints = ref _endpointsByJoint[jointId];
 579120        if (ReferenceEquals(endpoints.EndpointA.Body, body))
 262121            return ref endpoints.EndpointA;
 122
 317123        ChronicleGuard.ThrowIfTrue(
 317124            !ReferenceEquals(endpoints.EndpointB.Body, body),
 317125            nameof(body),
 317126            "Joint endpoint chain references an unrelated body.");
 317127        return ref endpoints.EndpointB;
 128    }
 129
 130    private void EnsureJointCapacity(int required)
 131    {
 586132        if (required <= _endpointsByJoint.Length)
 584133            return;
 134
 2135        int newSize = _endpointsByJoint.Length;
 4136        while (newSize < required)
 2137            newSize *= 2;
 138
 2139        Array.Resize(ref _endpointsByJoint, newSize);
 2140    }
 141
 142    private struct EndpointChain
 143    {
 144        internal EndpointChain(int firstJointId, int lastJointId, int count)
 145        {
 682146            FirstJointId = firstJointId;
 682147            LastJointId = lastJointId;
 682148            Count = count;
 682149        }
 150
 151        internal int FirstJointId;
 152        internal int LastJointId;
 153        internal int Count;
 154    }
 155
 156    private struct JointEndpoints
 157    {
 158        internal JointEndpoints(TBody bodyA, TBody bodyB)
 159        {
 586160            EndpointA = new EndpointLink(bodyA);
 586161            EndpointB = new EndpointLink(bodyB);
 586162            IsRegistered = false;
 586163        }
 164
 165        internal EndpointLink EndpointA;
 166        internal EndpointLink EndpointB;
 167        internal bool IsRegistered;
 168    }
 169
 170    private struct EndpointLink
 171    {
 172        internal EndpointLink(TBody body)
 173        {
 1172174            Body = body;
 1172175            PreviousJointId = 0;
 1172176            NextJointId = 0;
 1172177        }
 178
 179        internal TBody? Body;
 180        internal int PreviousJointId;
 181        internal int NextJointId;
 182    }
 183}