12
12
/// A sequence that presents the elements of a base sequence of elements
13
13
/// with a separator between each of those elements.
14
14
public 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
+ }
17
26
}
18
27
19
28
extension Intersperse : Sequence {
20
29
/// The iterator for an `Intersperse` sequence.
21
30
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
26
47
enum State {
27
48
case start
28
49
case element( Base . Element )
29
50
case separator
30
51
}
31
52
53
+ @inlinable
32
54
public mutating func next( ) -> Base . Element ? {
33
55
// After the start, the state flips between element and separator. Before
34
56
// returning a separator, a check is made for the next element as a
@@ -49,6 +71,7 @@ extension Intersperse: Sequence {
49
71
}
50
72
}
51
73
74
+ @inlinable
52
75
public func makeIterator( ) -> Intersperse < Base > . Iterator {
53
76
Iterator ( iterator: base. makeIterator ( ) , separator: separator)
54
77
}
@@ -57,12 +80,16 @@ extension Intersperse: Sequence {
57
80
extension Intersperse : Collection where Base: Collection {
58
81
/// A position in an `Intersperse` collection.
59
82
public struct Index : Comparable {
83
+ @usableFromInline
60
84
enum Representation : Equatable {
61
85
case element( Base . Index )
62
86
case separator( next: Base . Index )
63
87
}
64
- let representation : Representation
88
+
89
+ @usableFromInline
90
+ internal let representation : Representation
65
91
92
+ @inlinable
66
93
public static func < ( lhs: Index , rhs: Index ) -> Bool {
67
94
switch ( lhs. representation, rhs. representation) {
68
95
case let ( . element( li) , . element( ri) ) ,
@@ -73,24 +100,29 @@ extension Intersperse: Collection where Base: Collection {
73
100
return li <= ri
74
101
}
75
102
}
76
-
103
+
104
+ @usableFromInline
77
105
static func element( _ index: Base . Index ) -> Self {
78
106
Self ( representation: . element( index) )
79
107
}
80
108
109
+ @usableFromInline
81
110
static func separator( next: Base . Index ) -> Self {
82
111
Self ( representation: . separator( next: next) )
83
112
}
84
113
}
85
114
115
+ @inlinable
86
116
public var startIndex : Index {
87
117
base. startIndex == base. endIndex ? endIndex : . element( base. startIndex)
88
118
}
89
119
120
+ @inlinable
90
121
public var endIndex : Index {
91
122
. separator( next: base. endIndex)
92
123
}
93
124
125
+ @inlinable
94
126
public func index( after i: Index ) -> Index {
95
127
precondition ( i != endIndex, " Can't advance past endIndex " )
96
128
switch i. representation {
@@ -101,13 +133,15 @@ extension Intersperse: Collection where Base: Collection {
101
133
}
102
134
}
103
135
136
+ @inlinable
104
137
public subscript( position: Index ) -> Element {
105
138
switch position. representation {
106
139
case . element( let index) : return base [ index]
107
140
case . separator: return separator
108
141
}
109
142
}
110
-
143
+
144
+ @inlinable
111
145
public func index( _ i: Index , offsetBy distance: Int ) -> Index {
112
146
switch ( i. representation, distance. isMultiple ( of: 2 ) ) {
113
147
case ( let . element( index) , true ) :
@@ -122,7 +156,7 @@ extension Intersperse: Collection where Base: Collection {
122
156
}
123
157
124
158
// TODO: Implement index(_:offsetBy:limitedBy:)
125
-
159
+ @ inlinable
126
160
public func distance( from start: Index , to end: Index ) -> Int {
127
161
switch ( start. representation, end. representation) {
128
162
case let ( . element( element) , . separator( next: separator) ) :
@@ -139,6 +173,7 @@ extension Intersperse: Collection where Base: Collection {
139
173
extension Intersperse : BidirectionalCollection
140
174
where Base: BidirectionalCollection
141
175
{
176
+ @inlinable
142
177
public func index( before i: Index ) -> Index {
143
178
precondition ( i != startIndex, " Can't move before startIndex " )
144
179
switch i. representation {
@@ -189,6 +224,7 @@ extension Sequence {
189
224
/// - Returns: The interspersed sequence of elements.
190
225
///
191
226
/// - Complexity: O(1)
227
+ @inlinable
192
228
public func interspersed( with separator: Element ) -> Intersperse < Self > {
193
229
Intersperse ( base: self , separator: separator)
194
230
}
0 commit comments