Skip to content

Commit d7a08ac

Browse files
committed
[DependencyResolver] Add & adopt sequence conformances.
1 parent 570b57d commit d7a08ac

File tree

1 file changed

+46
-9
lines changed

1 file changed

+46
-9
lines changed

Sources/PackageGraph/DependencyResolver.swift

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func ==(_ lhs: BoundVersion, _ rhs: BoundVersion) -> Bool {
192192
// `PackageContainerConstraint`s. That won't work if we decide this should
193193
// eventually map based on the `Container` rather than the `Identifier`, though,
194194
// so they are separate for now.
195-
struct PackageContainerConstraintSet<C: PackageContainer> {
195+
struct PackageContainerConstraintSet<C: PackageContainer>: Collection {
196196
typealias Container = C
197197
typealias Identifier = Container.Identifier
198198

@@ -244,18 +244,37 @@ struct PackageContainerConstraintSet<C: PackageContainer> {
244244
///
245245
/// - Returns: False if the merger has made the set unsatisfiable; i.e. true
246246
/// when the resulting set is satisfiable, if it was already so.
247-
mutating func merge(_ rhs: PackageContainerConstraintSet<Container>) -> Bool {
247+
mutating func merge(_ constraints: PackageContainerConstraintSet<Container>) -> Bool {
248248
var satisfiable = true
249-
for (key, versionRequirement) in rhs.constraints {
249+
for (key, versionRequirement) in constraints {
250250
if !merge(versionRequirement: versionRequirement, for: key) {
251251
satisfiable = false
252252
}
253253
}
254254
return satisfiable
255255
}
256+
257+
// MARK: Collection Conformance
258+
259+
var startIndex: Index {
260+
return constraints.startIndex
261+
}
262+
263+
var endIndex: Index {
264+
return constraints.endIndex
265+
}
266+
267+
func index(after i: Index) -> Index {
268+
return constraints.index(after: i)
269+
}
270+
271+
subscript(position: Index) -> Element {
272+
return constraints[position]
273+
}
256274
}
257275

258-
/// A container for version assignments for a set of packages.
276+
/// A container for version assignments for a set of packages, exposed as a
277+
/// sequence of `Container` to `BoundVersion` bindings.
259278
///
260279
/// This is intended to be an efficient data structure for accumulating a set of
261280
/// version assignments along with efficient access to the derived information
@@ -267,7 +286,7 @@ struct PackageContainerConstraintSet<C: PackageContainer> {
267286
/// `constraints`, but this invariant is not explicitly enforced.
268287
//
269288
// FIXME: Actually make efficient.
270-
struct VersionAssignmentSet<C: PackageContainer> {
289+
struct VersionAssignmentSet<C: PackageContainer>: Sequence {
271290
typealias Container = C
272291
typealias Identifier = Container.Identifier
273292

@@ -320,15 +339,15 @@ struct VersionAssignmentSet<C: PackageContainer> {
320339
// The induced constraints are satisfiable, so we *can* union the
321340
// assignments without breaking our internal invariant on
322341
// satisfiability.
323-
for entry in assignment.assignments.values {
324-
if let existing = self[entry.container] {
325-
if existing != entry.binding {
342+
for (container, binding) in assignment {
343+
if let existing = self[container] {
344+
if existing != binding {
326345
// NOTE: We are returning here with the data structure
327346
// partially updated, which feels wrong. See FIXME above.
328347
return false
329348
}
330349
} else {
331-
self[entry.container] = entry.binding
350+
self[container] = binding
332351
}
333352
}
334353

@@ -410,6 +429,24 @@ struct VersionAssignmentSet<C: PackageContainer> {
410429

411430
return true
412431
}
432+
433+
// MARK: Sequence Conformance
434+
435+
// FIXME: This should really be a collection, but that takes significantly
436+
// more work given our current backing collection.
437+
438+
typealias Iterator = AnyIterator<(Container, BoundVersion)>
439+
440+
func makeIterator() -> Iterator {
441+
var it = assignments.values.makeIterator()
442+
return AnyIterator{
443+
if let next = it.next() {
444+
return (next.container, next.binding)
445+
} else {
446+
return nil
447+
}
448+
}
449+
}
413450
}
414451

415452
/// A general purpose package dependency resolver.

0 commit comments

Comments
 (0)