1212/// A sequence that presents the elements of a base sequence of elements
1313/// with a separator between each of those elements.
1414public struct Intersperse < Base: Sequence > {
15- let base : Base
16- let separator : Base . Element
15+ @usableFromInline
16+ internal let base : Base
17+
18+ @usableFromInline
19+ internal let separator : Base . Element
20+
21+ @usableFromInline
22+ internal init ( base: Base , separator: Base . Element ) {
23+ self . base = base
24+ self . separator = separator
25+ }
1726}
1827
1928extension Intersperse : Sequence {
2029 /// The iterator for an `Intersperse` sequence.
2130 public struct Iterator : IteratorProtocol {
22- var iterator : Base . Iterator
23- let separator : Base . Element
24- var state = State . start
25-
31+ @usableFromInline
32+ internal var iterator : Base . Iterator
33+
34+ @usableFromInline
35+ internal let separator : Base . Element
36+
37+ @usableFromInline
38+ internal var state = State . start
39+
40+ @usableFromInline
41+ internal init ( iterator: Base . Iterator , separator: Base . Element ) {
42+ self . iterator = iterator
43+ self . separator = separator
44+ }
45+
46+ @usableFromInline
2647 enum State {
2748 case start
2849 case element( Base . Element )
2950 case separator
3051 }
3152
53+ @inlinable
3254 public mutating func next( ) -> Base . Element ? {
3355 // After the start, the state flips between element and separator. Before
3456 // returning a separator, a check is made for the next element as a
@@ -49,6 +71,7 @@ extension Intersperse: Sequence {
4971 }
5072 }
5173
74+ @inlinable
5275 public func makeIterator( ) -> Intersperse < Base > . Iterator {
5376 Iterator ( iterator: base. makeIterator ( ) , separator: separator)
5477 }
@@ -57,12 +80,16 @@ extension Intersperse: Sequence {
5780extension Intersperse : Collection where Base: Collection {
5881 /// A position in an `Intersperse` collection.
5982 public struct Index : Comparable {
83+ @usableFromInline
6084 enum Representation : Equatable {
6185 case element( Base . Index )
6286 case separator( next: Base . Index )
6387 }
64- let representation : Representation
88+
89+ @usableFromInline
90+ internal let representation : Representation
6591
92+ @inlinable
6693 public static func < ( lhs: Index , rhs: Index ) -> Bool {
6794 switch ( lhs. representation, rhs. representation) {
6895 case let ( . element( li) , . element( ri) ) ,
@@ -73,24 +100,29 @@ extension Intersperse: Collection where Base: Collection {
73100 return li <= ri
74101 }
75102 }
76-
103+
104+ @usableFromInline
77105 static func element( _ index: Base . Index ) -> Self {
78106 Self ( representation: . element( index) )
79107 }
80108
109+ @usableFromInline
81110 static func separator( next: Base . Index ) -> Self {
82111 Self ( representation: . separator( next: next) )
83112 }
84113 }
85114
115+ @inlinable
86116 public var startIndex : Index {
87117 base. startIndex == base. endIndex ? endIndex : . element( base. startIndex)
88118 }
89119
120+ @inlinable
90121 public var endIndex : Index {
91122 . separator( next: base. endIndex)
92123 }
93124
125+ @inlinable
94126 public func index( after i: Index ) -> Index {
95127 precondition ( i != endIndex, " Can't advance past endIndex " )
96128 switch i. representation {
@@ -101,13 +133,15 @@ extension Intersperse: Collection where Base: Collection {
101133 }
102134 }
103135
136+ @inlinable
104137 public subscript( position: Index ) -> Element {
105138 switch position. representation {
106139 case . element( let index) : return base [ index]
107140 case . separator: return separator
108141 }
109142 }
110-
143+
144+ @inlinable
111145 public func index( _ i: Index , offsetBy distance: Int ) -> Index {
112146 switch ( i. representation, distance. isMultiple ( of: 2 ) ) {
113147 case ( let . element( index) , true ) :
@@ -122,7 +156,7 @@ extension Intersperse: Collection where Base: Collection {
122156 }
123157
124158 // TODO: Implement index(_:offsetBy:limitedBy:)
125-
159+ @ inlinable
126160 public func distance( from start: Index , to end: Index ) -> Int {
127161 switch ( start. representation, end. representation) {
128162 case let ( . element( element) , . separator( next: separator) ) :
@@ -139,6 +173,7 @@ extension Intersperse: Collection where Base: Collection {
139173extension Intersperse : BidirectionalCollection
140174 where Base: BidirectionalCollection
141175{
176+ @inlinable
142177 public func index( before i: Index ) -> Index {
143178 precondition ( i != startIndex, " Can't move before startIndex " )
144179 switch i. representation {
@@ -189,6 +224,7 @@ extension Sequence {
189224 /// - Returns: The interspersed sequence of elements.
190225 ///
191226 /// - Complexity: O(1)
227+ @inlinable
192228 public func interspersed( with separator: Element ) -> Intersperse < Self > {
193229 Intersperse ( base: self , separator: separator)
194230 }
0 commit comments