< Summary

Class:GDX.Mathematics.FibonacciHash
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Mathematics/FibonacciHash.cs
Covered lines:0
Uncovered lines:20
Coverable lines:20
Total lines:69
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)

./Packages/com.dotbunny.gdx/GDX/Mathematics/FibonacciHash.cs

#LineLine coverage
 1// Copyright (c) 2020-2024 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 a
 14        ///     that length.
 15        /// </summary>
 16        /// <param name="pow2Length">A 32-bit int equal to a power of two.</param>
 17        /// <returns>
 18        ///     How many spaces a 32-bit int would need to shift in order to be a valid index within
 19        ///     <paramref name="pow2Length" />.
 20        /// </returns>
 21        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 22        public static byte GetRightShiftFromPow2Length(int pow2Length)
 023        {
 24            LongDoubleConversionUnion u;
 025            u.DoubleValue = 0.0;
 026            u.LongValue = 0x4330000000000000L + pow2Length;
 027            u.DoubleValue -= 4503599627370496.0;
 028            int index = (int)(u.LongValue >> 52) - 0x3FF;
 29
 030            return (byte)(32 - index);
 031        }
 32
 33        /// <summary>
 34        ///     Takes the <paramref name="hash" /> and multiplies it by 2^32 divided by the golden ratio,
 35        ///     then right shifts it by <paramref name="shift" /> to fit within a given power-of-two size.
 36        /// </summary>
 37        /// <param name="hash">The key to find an index for.</param>
 38        /// <param name="shift">How far to right shift in order to fit within a given power-of-two size.</param>
 39        /// <returns>The index to store the <paramref name="hash" />.</returns>
 40        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 41        public static int GetIndexFromHash(int hash, byte shift)
 042        {
 043            int fibonacci = unchecked((int)(unchecked((uint)hash) * 2654435769));
 044            return fibonacci >> shift;
 045        }
 46
 47        /// <summary>
 48        ///     Takes the <paramref name="hash" /> and finds an index within the provided <paramref name="pow2Length" />
 49        ///     Fibonacci hashing.
 50        /// </summary>
 51        /// <param name="hash">The hash to find an index for.</param>
 52        /// <param name="pow2Length">The power-of-two array length to find an index within.</param>
 53        /// <returns></returns>
 54        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 55        public static int GetIndexFromHash(int hash, int pow2Length)
 056        {
 57            LongDoubleConversionUnion u;
 058            u.DoubleValue = 0.0;
 059            u.LongValue = 0x4330000000000000L + pow2Length;
 060            u.DoubleValue -= 4503599627370496.0;
 061            int index = (int)(u.LongValue >> 52) - 0x3FF;
 62
 063            int shift = 32 - index;
 64
 065            int fibonacci = unchecked((int)(unchecked((uint)hash) * 2654435769));
 066            return fibonacci >> shift;
 067        }
 68    }
 69}