< Summary

Class:GDX.Mathematics.FibonacciHash
Assembly:GDX
File(s):D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/Mathematics/FibonacciHash.cs
Covered lines:0
Uncovered lines:20
Coverable lines:20
Total lines:65
Line coverage:0% (0 of 20)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:3
Method coverage:0% (0 of 3)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GetRightShiftFromPow2Length(...)0%2100%
GetIndexFromHash(...)0%2100%
GetIndexFromHash(...)0%2100%

File(s)

D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/Mathematics/FibonacciHash.cs

#LineLine coverage
 1// Copyright (c) 2020-2022 dotBunny Inc.
 2// dotBunny licenses this file to you under the BSL-1.0 license.
 3// See the LICENSE file in the project root for more information.
 4
 5using System.Runtime.CompilerServices;
 6
 7namespace GDX.Mathematics
 8{
 9    public static class FibonacciHash
 10    {
 11        /// <summary>
 12        /// Takes a 32-bit length equal to a power of two,
 13        /// and returns how many spaces another 32-bit int would need to shift in order to be a valid index within an ar
 14        /// </summary>
 15        /// <param name="pow2Length">A 32-bit int equal to a power of two.</param>
 16        /// <returns>How many spaces a 32-bit int would need to shift in order to be a valid index within <paramref name
 17        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 18        public static byte GetRightShiftFromPow2Length(int pow2Length)
 019        {
 20            LongDoubleConversionUnion u;
 021            u.DoubleValue = 0.0;
 022            u.LongValue = 0x4330000000000000L + pow2Length;
 023            u.DoubleValue -= 4503599627370496.0;
 024            int index = (int)(u.LongValue >> 52) - 0x3FF;
 25
 026            return (byte)(32 - index);
 027        }
 28
 29        /// <summary>
 30        /// Takes the <paramref name="hash" /> and multiplies it by 2^32 divided by the golden ratio,
 31        /// then right shifts it by <paramref name="shift" /> to fit within a given power-of-two size.
 32        /// </summary>
 33        /// <param name="hash">The key to find an index for.</param>
 34        /// <param name="shift">How far to right shift in order to fit within a given power-of-two size.</param>
 35        /// <returns>The index to store the <paramref name="hash" />.</returns>
 36        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 37        public static int GetIndexFromHash(int hash, byte shift)
 038        {
 039            int fibonacci = unchecked((int)(unchecked((uint)hash) * 2654435769));
 040            return fibonacci >> shift;
 041        }
 42
 43        /// <summary>
 44        /// Takes the <paramref name="hash" /> and finds an index within the provided <paramref name="pow2Length" /> ran
 45        /// </summary>
 46        /// <param name="hash">The hash to find an index for.</param>
 47        /// <param name="pow2Length">The power-of-two array length to find an index within.</param>
 48        /// <returns></returns>
 49        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 50        public static int GetIndexFromHash(int hash, int pow2Length)
 051        {
 52            LongDoubleConversionUnion u;
 053            u.DoubleValue = 0.0;
 054            u.LongValue = 0x4330000000000000L + pow2Length;
 055            u.DoubleValue -= 4503599627370496.0;
 056            int index = (int)(u.LongValue >> 52) - 0x3FF;
 57
 058            int shift = 32 - index;
 59
 060            int fibonacci = unchecked((int)(unchecked((uint)hash) * 2654435769));
 061            return fibonacci >> shift;
 062        }
 63    }
 64}
 65