Skip to content

Commit d30e7ae

Browse files
committed
Give separatorCount the more accurate name splitCount.
When omitting empty subsequences, we may skip over multiple adjacent separators, so that property wasn't counting separators in that case. It's really counting splits.
1 parent c880918 commit d30e7ae

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

Sources/Algorithms/LazySplitCollection.swift

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public struct LazySplitCollection<Base: Collection> {
3939
self._endIndex = Index(
4040
baseRange: base.endIndex..<base.endIndex,
4141
sequenceLength: Int.max,
42-
separatorCount: Int.max
42+
splitCount: Int.max
4343
)
4444

4545
/// We precalculate `startIndex`. There are three possibilities:
@@ -48,7 +48,7 @@ public struct LazySplitCollection<Base: Collection> {
4848
self._startIndex = Index(
4949
baseRange: base.startIndex..<base.startIndex,
5050
sequenceLength: 1,
51-
separatorCount: 0
51+
splitCount: 0
5252
)
5353
if base.isEmpty {
5454
if omittingEmptySubsequences {
@@ -61,7 +61,7 @@ public struct LazySplitCollection<Base: Collection> {
6161
_startIndex = indexForSubsequence(
6262
atOrAfter: base.startIndex,
6363
sequenceLength: 0,
64-
separatorCount: 0
64+
splitCount: 0
6565
)
6666
}
6767
}
@@ -75,16 +75,16 @@ extension LazySplitCollection: LazyCollectionProtocol {
7575
/// The number of subsequences up to and including this position in the
7676
/// collection.
7777
internal let sequenceLength: Int
78-
internal let separatorCount: Int
78+
internal let splitCount: Int
7979

8080
internal init(
8181
baseRange: Range<Base.Index>,
8282
sequenceLength: Int,
83-
separatorCount: Int
83+
splitCount: Int
8484
) {
8585
self.baseRange = baseRange
8686
self.sequenceLength = sequenceLength
87-
self.separatorCount = separatorCount
87+
self.splitCount = splitCount
8888
}
8989

9090
public static func == (lhs: Index, rhs: Index) -> Bool {
@@ -102,18 +102,15 @@ extension LazySplitCollection: LazyCollectionProtocol {
102102
internal func indexForSubsequence(
103103
atOrAfter lowerBound: Base.Index,
104104
sequenceLength: Int,
105-
separatorCount: Int
105+
splitCount: Int
106106
) -> Index {
107-
var newSeparatorCount = separatorCount
108107
var start = lowerBound
109108
// If we don't have any more splits to do (which we'll determine shortly),
110-
// the end of the next subsequence will be the end of the base collection.
109+
// the end of this subsequence will be the end of the base collection.
111110
var end = base.endIndex
112111

113-
// The number of separators encountered thus far is identical to the number
114-
// of splits performed thus far.
115-
if newSeparatorCount < maxSplits {
116-
// The non-inclusive end of the next subsequence is marked by the next
112+
if splitCount < maxSplits {
113+
// The non-inclusive end of this subsequence is marked by the next
117114
// separator, or the end of the base collection.
118115
end =
119116
base[start...].firstIndex(where: isSeparator)
@@ -134,14 +131,18 @@ extension LazySplitCollection: LazyCollectionProtocol {
134131
}
135132
}
136133

134+
var updatedSplitCount = splitCount
137135
if end < base.endIndex {
138-
newSeparatorCount += 1
136+
// This subsequence ends on a separator (and perhaps includes other
137+
// separators, if we're omitting empty subsequences), so we've performed
138+
// another split.
139+
updatedSplitCount += 1
139140
}
140141

141142
return Index(
142143
baseRange: start..<end,
143144
sequenceLength: sequenceLength + 1,
144-
separatorCount: newSeparatorCount
145+
splitCount: updatedSplitCount
145146
)
146147
}
147148

@@ -166,7 +167,7 @@ extension LazySplitCollection: LazyCollectionProtocol {
166167

167168
guard subsequenceStart != base.endIndex else {
168169
if !omittingEmptySubsequences
169-
&& i.sequenceLength < i.separatorCount + 1
170+
&& i.sequenceLength < i.splitCount + 1
170171
{
171172
/// The base collection ended with a separator, so we need to emit one
172173
/// more empty subsequence. This one differs from `endIndex` in its
@@ -175,7 +176,7 @@ extension LazySplitCollection: LazyCollectionProtocol {
175176
return Index(
176177
baseRange: base.endIndex..<base.endIndex,
177178
sequenceLength: i.sequenceLength + 1,
178-
separatorCount: i.separatorCount
179+
splitCount: i.splitCount
179180
)
180181
} else {
181182
return endIndex
@@ -185,7 +186,7 @@ extension LazySplitCollection: LazyCollectionProtocol {
185186
return indexForSubsequence(
186187
atOrAfter: subsequenceStart,
187188
sequenceLength: i.sequenceLength,
188-
separatorCount: i.separatorCount
189+
splitCount: i.splitCount
189190
)
190191
}
191192

0 commit comments

Comments
 (0)