Skip to content

Commit f65f130

Browse files
committed
Refactor typealias Index into struct, and remove Iterator.
1 parent 6073193 commit f65f130

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

Sources/Algorithms/Cycle.swift

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -69,69 +69,64 @@ public struct FiniteCycle<Base: Collection> {
6969
}
7070
}
7171

72-
extension FiniteCycle {
73-
/// The iterator for a `FiniteCycle` sequence.
74-
public struct Iterator : IteratorProtocol {
72+
extension FiniteCycle: LazySequenceProtocol, LazyCollectionProtocol
73+
where Base: LazyCollectionProtocol { }
74+
75+
extension FiniteCycle: Collection {
76+
77+
public typealias Element = Base.Element
78+
79+
public struct Index: Comparable {
80+
/// The index corresponding to the Product2 index at this position.
7581
@usableFromInline
76-
var productIterator: Product2<Range<Int>, Base>.Iterator
82+
internal let productIndex: Product2<Range<Int>, Base>.Index
7783

7884
@inlinable
79-
internal init(product: Product2<Range<Int>, Base>) {
80-
self.productIterator = product.makeIterator()
85+
internal init(_ productIndex: Product2<Range<Int>, Base>.Index) {
86+
self.productIndex = productIndex
8187
}
8288

8389
@inlinable
84-
public mutating func next() -> Base.Element? {
85-
self.productIterator.next()?.element2
90+
public static func == (lhs: Index, rhs: Index) -> Bool {
91+
lhs.productIndex == rhs.productIndex
8692
}
87-
}
88-
89-
@inlinable
90-
public func makeIterator() -> Iterator {
91-
return Iterator(product: product)
92-
}
93-
}
94-
95-
extension FiniteCycle: LazySequenceProtocol, LazyCollectionProtocol
96-
where Base: LazyCollectionProtocol { }
97-
98-
extension FiniteCycle: Collection {
99-
100-
public typealias Index = Product2<Range<Int>, Base>.Index
10193

102-
@inlinable
103-
public var count: Int {
104-
product.count
94+
@inlinable
95+
public static func < (lhs: Index, rhs: Index) -> Bool {
96+
lhs.productIndex < rhs.productIndex
97+
}
10598
}
10699

107100
@inlinable
108101
public var startIndex: Index {
109-
product.startIndex
102+
Index(product.startIndex)
110103
}
111104

112105
@inlinable
113106
public var endIndex: Index {
114-
product.endIndex
107+
Index(product.endIndex)
115108
}
116109

117110
@inlinable
118111
public subscript(_ index: Index) -> Element {
119-
product[index].element2
112+
product[index.productIndex].element2
120113
}
121114

122115
@inlinable
123116
public func index(after i: Index) -> Index {
124-
product.index(after: i)
117+
let productIndex = product.index(after: i.productIndex)
118+
return Index(productIndex)
125119
}
126120

127121
@inlinable
128122
public func distance(from start: Index, to end: Index) -> Int {
129-
product.distance(from: start, to: end)
123+
product.distance(from: start.productIndex, to: end.productIndex)
130124
}
131125

132126
@inlinable
133127
public func index(_ i: Index, offsetBy distance: Int) -> Index {
134-
product.index(i, offsetBy: distance)
128+
let productIndex = product.index(i.productIndex, offsetBy: distance)
129+
return Index(productIndex)
135130
}
136131

137132
@inlinable
@@ -140,15 +135,26 @@ extension FiniteCycle: Collection {
140135
offsetBy distance: Int,
141136
limitedBy limit: Index
142137
) -> Index? {
143-
product.index(i, offsetBy: distance, limitedBy: limit)
138+
guard let productIndex = product.index(i.productIndex,
139+
offsetBy: distance,
140+
limitedBy: limit.productIndex) else {
141+
return nil
142+
}
143+
return Index(productIndex)
144+
}
145+
146+
@inlinable
147+
public var count: Int {
148+
product.count
144149
}
145150
}
146151

147152
extension FiniteCycle: BidirectionalCollection
148153
where Base: BidirectionalCollection {
149154
@inlinable
150155
public func index(before i: Index) -> Index {
151-
product.index(before: i)
156+
let productIndex = product.index(before: i.productIndex)
157+
return Index(productIndex)
152158
}
153159
}
154160

0 commit comments

Comments
 (0)