< Summary

Information
Class: Trailblazer.Pathing.PathRequestHashBuilder
Assembly: Trailblazer
File(s): /home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Pathing/Search/Request/PathRequestHashBuilder.cs
Line coverage
91%
Covered lines: 21
Uncovered lines: 2
Coverable lines: 23
Total lines: 86
Line coverage: 91.3%
Branch coverage
83%
Covered branches: 5
Total branches: 6
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Create()100%11100%
Add(...)100%22100%
Add(...)100%11100%
AddOrdinalString(...)75%4475%
ToHashCode()100%11100%
Mix(...)100%11100%

File(s)

/home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Pathing/Search/Request/PathRequestHashBuilder.cs

#LineLine coverage
 1using System.Runtime.CompilerServices;
 2
 3namespace Trailblazer.Pathing;
 4
 5/// <summary>
 6/// Deterministic allocation-free hash combiner for path request cache keys.
 7/// </summary>
 8internal struct PathRequestHashBuilder
 9{
 10    private const int Seed = 5381;
 11    private const int Shift1 = 16;
 12    private const int Shift2 = 5;
 13    private const int Shift3 = 27;
 14    private const int Factor3 = 1566083941;
 15
 16    private int _hash1;
 17    private int _hash2;
 18    private int _count;
 19
 20    private PathRequestHashBuilder(int hash1, int hash2)
 21    {
 819122        _hash1 = hash1;
 819123        _hash2 = hash2;
 819124        _count = 0;
 819125    }
 26
 27    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 28    public static PathRequestHashBuilder Create()
 29    {
 819130        int hash = (Seed << Shift1) + Seed;
 819131        return new PathRequestHashBuilder(hash, hash);
 32    }
 33
 34    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 35    public void Add(int value)
 36    {
 6838137        if ((_count & 1) == 0)
 3563538            _hash1 = Mix(_hash1, value);
 39        else
 3274640            _hash2 = Mix(_hash2, value);
 41
 6838142        _count++;
 6838143    }
 44
 45    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 46    public void Add(bool value)
 47    {
 1384748        Add(value ? 1 : 0);
 1384749    }
 50
 51    public void AddOrdinalString(string? value)
 52    {
 453        if (string.IsNullOrEmpty(value))
 54        {
 055            Add(0);
 056            return;
 57        }
 58
 59        unchecked
 60        {
 461            int hash = 17;
 8862            for (int i = 0; i < value.Length; i++)
 4063                hash = (hash * 31) + value[i];
 64
 465            Add(hash);
 66        }
 467    }
 68
 69    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 70    public int ToHashCode()
 71    {
 72        unchecked
 73        {
 819174            return _hash1 + (_hash2 * Factor3);
 75        }
 76    }
 77
 78    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 79    private static int Mix(int hash, int itemHash)
 80    {
 81        unchecked
 82        {
 6838183            return ((hash << Shift2) + hash + (hash >> Shift3)) ^ itemHash;
 84        }
 85    }
 86}