@@ -69,69 +69,64 @@ public struct FiniteCycle<Base: Collection> {
69
69
}
70
70
}
71
71
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.
75
81
@usableFromInline
76
- var productIterator : Product2 < Range < Int > , Base > . Iterator
82
+ internal let productIndex : Product2 < Range < Int > , Base > . Index
77
83
78
84
@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
81
87
}
82
88
83
89
@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
86
92
}
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
101
93
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
+ }
105
98
}
106
99
107
100
@inlinable
108
101
public var startIndex : Index {
109
- product. startIndex
102
+ Index ( product. startIndex)
110
103
}
111
104
112
105
@inlinable
113
106
public var endIndex : Index {
114
- product. endIndex
107
+ Index ( product. endIndex)
115
108
}
116
109
117
110
@inlinable
118
111
public subscript( _ index: Index ) -> Element {
119
- product [ index] . element2
112
+ product [ index. productIndex ] . element2
120
113
}
121
114
122
115
@inlinable
123
116
public func index( after i: Index ) -> Index {
124
- product. index ( after: i)
117
+ let productIndex = product. index ( after: i. productIndex)
118
+ return Index ( productIndex)
125
119
}
126
120
127
121
@inlinable
128
122
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 )
130
124
}
131
125
132
126
@inlinable
133
127
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)
135
130
}
136
131
137
132
@inlinable
@@ -140,15 +135,26 @@ extension FiniteCycle: Collection {
140
135
offsetBy distance: Int ,
141
136
limitedBy limit: Index
142
137
) -> 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
144
149
}
145
150
}
146
151
147
152
extension FiniteCycle : BidirectionalCollection
148
153
where Base: BidirectionalCollection {
149
154
@inlinable
150
155
public func index( before i: Index ) -> Index {
151
- product. index ( before: i)
156
+ let productIndex = product. index ( before: i. productIndex)
157
+ return Index ( productIndex)
152
158
}
153
159
}
154
160
0 commit comments