Private/Utils/ConvertTo-PolicyHash.ps1

function ConvertTo-PolicyHash {
    <#
    .SYNOPSIS
        Convert policy array to hashtable for O(1) lookup performance.
     
    .DESCRIPTION
        Creates hashtable indexed by specified key property for fast lookups.
        Alternative to Group-Object -AsHashTable with better control over key selection.
     
    .PARAMETER Policies
        Array of policy objects to convert.
     
    .PARAMETER KeyProperty
        Property name to use as hashtable key (e.g., "PolicyDefinitionId", "Id").
     
    .EXAMPLE
        $policyHash = ConvertTo-PolicyHash -Policies $assignments -KeyProperty "PolicyDefinitionId"
        $policy = $policyHash["/providers/Microsoft.Authorization/policyDefinitions/123"]
     
    .OUTPUTS
        Hashtable with key property values as keys and policy objects as values
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [object[]]$Policies,
        
        [Parameter(Mandatory = $true)]
        [string]$KeyProperty
    )
    
    $hashtable = @{}
    
    foreach ($policy in $Policies) {
        $key = $policy.$KeyProperty
        
        if ([string]::IsNullOrWhiteSpace($key)) {
            continue
        }
        
        # Handle collisions by storing arrays
        if (-not $hashtable.ContainsKey($key)) {
            $hashtable[$key] = @()
        }
        
        $hashtable[$key] += $policy
    }
    
    return $hashtable
}