|
| 1 | +// RUN: %target-swift-frontend -emit-ir %s |
| 2 | + |
| 3 | +public protocol DefaultInitializable { |
| 4 | + init() |
| 5 | +} |
| 6 | + |
| 7 | +public protocol AdjacencyEdge_ { |
| 8 | + associatedtype VertexID: Equatable |
| 9 | + associatedtype Properties |
| 10 | + |
| 11 | + var destination: VertexID { get set } |
| 12 | + var properties: Properties { get set } |
| 13 | +} |
| 14 | + |
| 15 | +public struct AdjacencyEdge<VertexID: Equatable, Properties>: AdjacencyEdge_ { |
| 16 | + public var destination: VertexID |
| 17 | + public var properties: Properties |
| 18 | +} |
| 19 | + |
| 20 | +public protocol AdjacencyVertex_ { |
| 21 | + associatedtype Edges: Collection where Edges.Element: AdjacencyEdge_ |
| 22 | + associatedtype Properties |
| 23 | + |
| 24 | + var edges: Edges { get set } |
| 25 | + var properties: Properties { get set } |
| 26 | +} |
| 27 | + |
| 28 | +public struct AdjacencyVertex<Edges: Collection, Properties> : AdjacencyVertex_ |
| 29 | + where Edges.Element: AdjacencyEdge_ |
| 30 | +{ |
| 31 | + public var edges: Edges |
| 32 | + public var properties: Properties |
| 33 | +} |
| 34 | + |
| 35 | +public protocol BinaryFunction { |
| 36 | + associatedtype Parameter0 |
| 37 | + associatedtype Parameter1 |
| 38 | + associatedtype Result |
| 39 | + |
| 40 | + func callAsFunction(_: Parameter0, _: Parameter1) -> Result |
| 41 | +} |
| 42 | + |
| 43 | +public struct GeneralAdjacencyList< |
| 44 | + Spine: Collection, VertexIDToIndex: BinaryFunction |
| 45 | +> |
| 46 | + where Spine.Element : AdjacencyVertex_, |
| 47 | + VertexIDToIndex.Parameter0 == Spine, |
| 48 | + VertexIDToIndex.Parameter1 == Spine.Element.Edges.Element.VertexID, |
| 49 | + VertexIDToIndex.Result == Spine.Index |
| 50 | +{ |
| 51 | + public let vertexIDToIndex: VertexIDToIndex |
| 52 | + public var spine: Spine |
| 53 | +} |
| 54 | + |
| 55 | +public struct IdentityVertexIDToIndex<Spine: Collection>: BinaryFunction |
| 56 | + where Spine.Element : AdjacencyVertex_, |
| 57 | + Spine.Element.Edges.Element.VertexID == Spine.Index |
| 58 | +{ |
| 59 | + public func callAsFunction(_: Spine, _ id: Spine.Index) -> Spine.Index { |
| 60 | + return id |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +public extension GeneralAdjacencyList { |
| 65 | + typealias VertexID = VertexIDToIndex.Parameter1 |
| 66 | + typealias VertexProperties = Spine.Element.Properties |
| 67 | + typealias EdgeProperties = Spine.Element.Edges.Element.Properties |
| 68 | + |
| 69 | + struct EdgeID: Equatable { |
| 70 | + /// The source vertex. |
| 71 | + let source: VertexIDToIndex.Parameter1 |
| 72 | + /// The position of the edge in `source`'s list of edges. |
| 73 | + let targetIndex: Spine.Index |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +public extension GeneralAdjacencyList |
| 78 | + where VertexIDToIndex == IdentityVertexIDToIndex<Spine>, |
| 79 | + Spine: RangeReplaceableCollection, |
| 80 | + Spine.Element.Edges: RangeReplaceableCollection |
| 81 | + & BidirectionalCollection // Because https://bugs.swift.org/browse/SR-12810 |
| 82 | +{ |
| 83 | + func addVertex(storing properties: VertexProperties) -> VertexID { |
| 84 | + return spine.indices.first! |
| 85 | + } |
| 86 | +} |
| 87 | + |
0 commit comments