mirror of
https://github.com/GayPizzaSpecifications/darwin-apk.git
synced 2025-08-09 08:01:33 +00:00
Factor out resolution responsibilities
This commit is contained in:
@ -8,12 +8,6 @@ import Foundation
|
||||
public struct ApkIndex: Sendable {
|
||||
public let packages: [ApkIndexPackage]
|
||||
public typealias Index = Array<ApkIndexPackage>.Index
|
||||
|
||||
lazy var providers: [(ApkIndexProvides, Index)] = {
|
||||
self.packages.enumerated().flatMap { index, pkg in
|
||||
[ (.specific(name: pkg.name, version: pkg.version), index) ] + pkg.provides.map { ($0, index) }
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
public extension ApkIndex {
|
||||
@ -28,24 +22,8 @@ public extension ApkIndex {
|
||||
$0.name == name
|
||||
}
|
||||
}
|
||||
|
||||
mutating func resolve(requirement: ApkVersionRequirement) -> ApkIndexPackage? {
|
||||
self.providers.filter { prv in prv.0.satisfies(requirement) }
|
||||
.map { self.packages[$1] }.max()
|
||||
}
|
||||
|
||||
mutating func resolveIndex(requirement: ApkVersionRequirement) -> Index? {
|
||||
self.providers.filter { prv in prv.0.satisfies(requirement) }
|
||||
.max { self.packages[$0.1] < self.packages[$1.1] }?.1
|
||||
}
|
||||
}
|
||||
|
||||
extension ApkIndexPackage: Comparable {
|
||||
public static func < (lhs: Self, rhs: Self) -> Bool {
|
||||
// Prefer highest declared provider priority
|
||||
lhs.providerPriority ?? 0 < rhs.providerPriority ?? 0
|
||||
}
|
||||
}
|
||||
|
||||
public extension ApkIndex {
|
||||
static func merge<S: Sequence>(_ tables: S) -> Self where S.Element == Self {
|
||||
|
37
Sources/apk/Index/ApkIndexProviderCache.swift
Normal file
37
Sources/apk/Index/ApkIndexProviderCache.swift
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* darwin-apk © 2025 Gay Pizza Specifications
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import Foundation
|
||||
|
||||
public struct ApkIndexProviderCache {
|
||||
private var _providers = [(ApkIndexProvides, ApkIndex.Index)]()
|
||||
//private var _installIfCache = [(ApkIndexInstallIf, Set<ApkIndex.Index>)]()
|
||||
|
||||
public init(index pkgIndex: ApkIndex) {
|
||||
for (index, pkg) in pkgIndex.packages.enumerated() {
|
||||
self._providers.append((.specific(name: pkg.name, version: pkg.version), index))
|
||||
for provision in pkg.provides {
|
||||
self._providers.append((provision, index))
|
||||
}
|
||||
//for installIf in pkg.installIf {
|
||||
// self._installIfCache.append((installIf, index))
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension ApkIndexProviderCache {
|
||||
func resolve(index pkgIndex: ApkIndex, requirement: ApkVersionRequirement) -> ApkIndex.Index? {
|
||||
self._providers.filter { prv in prv.0.satisfies(requirement) }
|
||||
.max { pkgIndex.packages[$0.1] < pkgIndex.packages[$1.1] }?.1
|
||||
}
|
||||
}
|
||||
|
||||
extension ApkIndexPackage: Comparable {
|
||||
public static func < (lhs: Self, rhs: Self) -> Bool {
|
||||
// Prefer highest declared provider priority
|
||||
lhs.providerPriority ?? 0 < rhs.providerPriority ?? 0
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user