Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassRegistry/DependencyGraph.swift8// Module : SPGlassRegistry9// Repository : https://git.spboucher.ai/sp-glasskit10// Project Website : https://glass.spboucher.ai11// Created : 2026-12-0212// Last Modified : 2026-12-0213//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation4243/// Dependency resolution over item identifiers.44public enum DependencyGraph {45 /// Returns `roots` and everything they depend on, dependencies first.46 ///47 /// The order is deterministic: dependencies are visited in the order they are declared. A cycle is48 /// reported with the full path that closes it.49 ///50 /// - Parameter dependencies: returns the direct dependencies of an identifier.51 public static func installationOrder<E: Error>(52 roots: [String],53 dependencies: (String) throws(E) -> [String]54 ) throws -> [String] {55 var order: [String] = []56 var done: Set<String> = []57 var onPath: Set<String> = []58 // Explicit stack instead of recursion: the depth of a registry graph must not be able to exhaust the59 // call stack.60 var path: [(id: String, pending: [String], next: Int)] = []6162 for root in roots where !done.contains(root) {63 path.append((root, try dependencies(root), 0))64 onPath.insert(root)65 while let frame = path.last {66 if frame.next < frame.pending.count {67 let dependency = frame.pending[frame.next]68 path[path.count - 1].next += 169 if done.contains(dependency) { continue }70 if onPath.contains(dependency) {71 let ids = path.map(\.id)72 let start = ids.firstIndex(of: dependency) ?? 073 throw RegistryError.dependencyCycle(Array(ids[start...]) + [dependency])74 }75 path.append((dependency, try dependencies(dependency), 0))76 onPath.insert(dependency)77 } else {78 path.removeLast()79 onPath.remove(frame.id)80 done.insert(frame.id)81 order.append(frame.id)82 }83 }84 }85 return order86 }8788 /// The identifiers in `installed` that depend, directly or not, on `id`.89 public static func dependents(of id: String, in installed: [String: [String]]) -> [String] {90 var result: Set<String> = []91 var frontier = [id]92 while let current = frontier.popLast() {93 for (candidate, dependencies) in installed where dependencies.contains(current) && !result.contains(candidate) && candidate != id {94 result.insert(candidate)95 frontier.append(candidate)96 }97 }98 return result.sorted()99 }100}101