func referencesForSymbols()

in Sources/SwiftDocC/Infrastructure/DocumentationContext.swift [906:960]


    func referencesForSymbols(in unifiedGraphs: [String: UnifiedSymbolGraph], bundle: DocumentationBundle) -> [SymbolGraph.Symbol.Identifier: ResolvedTopicReference] {
        /// Temporary data structure to form symbol/module pairs.
        struct SymbolInModule {
            let symbol: UnifiedSymbolGraph.Symbol
            let moduleName: String
        }
        
        var result = [SymbolGraph.Symbol.Identifier: ResolvedTopicReference]()
        
        // Keep count of how many times a path is used and for what kinds.
        var pathCount = [String: [SymbolInModule]]()
        
        // Group symbols by path from all of the available symbol graphs
        for (moduleName, symbolGraph) in unifiedGraphs {
            let symbols = Array(symbolGraph.symbols.values)
            let paths: [String] = symbols.concurrentMap { referenceFor($0, moduleName: moduleName, bundle: bundle).path.lowercased() }
            
            // Increase the index storage size as we merge more symbol graphs.
            if pathCount.capacity < paths.count {
                pathCount.reserveCapacity(paths.count)
            }
            if result.capacity < paths.count {
                result.reserveCapacity(paths.count)
            }

            for (index, symbol) in symbols.enumerated() {
                let path = paths[index]

                if let existingReferences = pathCount[path] {
                    // It's only a collision if it's a different precise identifier,
                    // otherwise it's just the same symbol.
                    if existingReferences.allSatisfy({ pair -> Bool in
                        return pair.symbol.uniqueIdentifier != symbol.uniqueIdentifier
                    }) {
                        // A collision - different symbol but same paths
                        pathCount[path]!.append(SymbolInModule(symbol: symbol, moduleName: moduleName))
                    }
                } else {
                    // First occurance of this path
                    pathCount[path] = [SymbolInModule(symbol: symbol, moduleName: moduleName)]
                }
            }
        }
        
        // Translate symbols to topic references, adjust paths where neccessary
        for collisions in pathCount.values {
            let disambiguationSuffixes = collisions.map(\.symbol).requiredDisambiguationSuffixes
            
            for (collision, disambiguationSuffix) in zip(collisions, disambiguationSuffixes) {
                result[collision.symbol.defaultIdentifier] = referenceFor(collision.symbol, moduleName: collision.moduleName, bundle: bundle, shouldAddHash: disambiguationSuffix.shouldAddIdHash, shouldAddKind: disambiguationSuffix.shouldAddKind)
            }
        }
        
        return result
    }