File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -56,6 +56,47 @@ extension Cycle: Sequence {
56
56
57
57
extension Cycle : LazySequenceProtocol where Base: LazySequenceProtocol { }
58
58
59
+
60
+ /// A collection wrapper that repeats the elements of a base collection for a finite number of times.
61
+ public struct FiniteCycle < Base: Collection > {
62
+ /// The collection to repeat.
63
+ public let base : Base
64
+
65
+ /// The number of times to repeat the collection.
66
+ public let times : Int
67
+
68
+ @usableFromInline
69
+ internal init ( base: Base , times: Int ) {
70
+ self . base = base
71
+ self . times = times
72
+ }
73
+ }
74
+
75
+ extension FiniteCycle : Sequence {
76
+ /// The iterator for a `FiniteCycle` sequence.
77
+ public struct Iterator : IteratorProtocol {
78
+ @usableFromInline
79
+ var productIterator : Product2 < Range < Int > , Base > . Iterator
80
+
81
+ @usableFromInline
82
+ internal init ( _ product: Product2 < Range < Int > , Base > ) {
83
+ self . productIterator = product. makeIterator ( )
84
+ }
85
+
86
+ @inlinable
87
+ public mutating func next( ) -> Base . Element ? {
88
+ self . productIterator. next ( ) ? . 1
89
+ }
90
+ }
91
+
92
+ public func makeIterator( ) -> Iterator {
93
+ let iterations : Range < Int > = 0 ..< times
94
+ return Iterator ( Product2 ( iterations, base) )
95
+ }
96
+ }
97
+
98
+ extension FiniteCycle : LazySequenceProtocol where Base: LazySequenceProtocol { }
99
+
59
100
//===----------------------------------------------------------------------===//
60
101
// cycled()
61
102
//===----------------------------------------------------------------------===//
You can’t perform that action at this time.
0 commit comments