Skip to content

Commit d6bf57d

Browse files
[CodeReview] More adjustments requested.
1 parent e446d44 commit d6bf57d

File tree

3 files changed

+19
-24
lines changed

3 files changed

+19
-24
lines changed

Sources/Algorithms/Compacted.swift

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,8 @@ public struct CompactedCollection<Base: Collection, Element>: Collection
8989

9090
@inlinable
9191
public func index(after i: Index) -> Index {
92-
guard i != endIndex else {
93-
fatalError("Index out of bounds")
94-
}
92+
precondition(i < endIndex, "Index out of bounds")
93+
9594
let baseIdx = base.index(after: i.base)
9695
guard let idx = base[baseIdx...].firstIndex(where: { $0 != nil })
9796
else { return endIndex }
@@ -104,13 +103,12 @@ extension CompactedCollection: BidirectionalCollection
104103

105104
@inlinable
106105
public func index(before i: Index) -> Index {
107-
guard i != startIndex else {
108-
fatalError("Index out of bounds")
109-
}
106+
precondition(i > startIndex, "Index out of bounds")
110107

111-
let baseIdx = base.index(before: i.base)
112-
guard let idx = base[...baseIdx].lastIndex(where: { $0 != nil })
113-
else { return Index(base: base.startIndex) }
108+
guard let idx =
109+
base[startIndex.base..<i.base]
110+
.lastIndex(where: { $0 != nil })
111+
else { fatalError("Index out of bounds") }
114112
return Index(base: idx)
115113
}
116114
}
@@ -150,46 +148,42 @@ extension CompactedCollection: LazyCollectionProtocol
150148
// Hashable and Equatable conformance are based on each non-nil
151149
// element on base collection.
152150
extension CompactedSequence: Equatable
153-
where Base: Equatable, Base.Element: Equatable {
151+
where Base.Element: Equatable {
154152

155153
@inlinable
156154
public static func ==(lhs: CompactedSequence,
157155
rhs: CompactedSequence) -> Bool {
158-
lhs.compactMap({ $0 })
159-
.elementsEqual(rhs.compactMap({ $0 }))
156+
lhs.elementsEqual(rhs)
160157
}
161158
}
162159

163160
extension CompactedSequence: Hashable
164-
where Base: Hashable, Base.Element: Hashable {
161+
where Element: Hashable {
165162
@inlinable
166163
public func hash(into hasher: inout Hasher) {
167-
for element in base {
168-
guard let unwrapped = element else { continue }
169-
hasher.combine(unwrapped)
164+
for element in self {
165+
hasher.combine(element)
170166
}
171167
}
172168
}
173169

174170
extension CompactedCollection: Equatable
175-
where Base: Equatable, Base.Element: Equatable {
171+
where Base.Element: Equatable {
176172

177173
@inlinable
178174
public static func ==(lhs: CompactedCollection,
179175
rhs: CompactedCollection) -> Bool {
180-
lhs.compactMap({ $0 })
181-
.elementsEqual(rhs.compactMap({ $0 }))
176+
lhs.elementsEqual(rhs)
182177
}
183178
}
184179

185180
extension CompactedCollection: Hashable
186-
where Base: Hashable, Base.Element: Hashable {
181+
where Element: Hashable {
187182

188183
@inlinable
189184
public func hash(into hasher: inout Hasher) {
190-
for element in base {
191-
guard let unwrapped = element else { continue }
192-
hasher.combine(unwrapped)
185+
for element in self {
186+
hasher.combine(element)
193187
}
194188
}
195189
}

Tests/SwiftAlgorithmsTests/CompactedTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift Algorithms open source project
44
//
5-
// Copyright (c) 2020 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2021 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information

Tests/SwiftAlgorithmsTests/TestUtilities.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct SplitMix64: RandomNumberGenerator {
4646
}
4747
}
4848

49+
// An eraser helper to any hashable sequence.
4950
struct AnyHashableSequence<Base>
5051
where Base: Sequence, Base: Hashable {
5152
var base: Base

0 commit comments

Comments
 (0)