Skip to content

Commit ff8b573

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

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

Sources/Algorithms/Compacted.swift

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public struct CompactedCollection<Base: Collection, Element>: Collection
8989

9090
@inlinable
9191
public func index(after i: Index) -> Index {
92-
guard i != endIndex else {
92+
guard i < endIndex else {
9393
fatalError("Index out of bounds")
9494
}
9595
let baseIdx = base.index(after: i.base)
@@ -104,13 +104,14 @@ extension CompactedCollection: BidirectionalCollection
104104

105105
@inlinable
106106
public func index(before i: Index) -> Index {
107-
guard i != startIndex else {
107+
guard i > startIndex else {
108108
fatalError("Index out of bounds")
109109
}
110110

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) }
111+
guard let idx =
112+
base[startIndex.base..<i.base]
113+
.lastIndex(where: { $0 != nil })
114+
else { fatalError("Index out of bounds") }
114115
return Index(base: idx)
115116
}
116117
}
@@ -150,46 +151,42 @@ extension CompactedCollection: LazyCollectionProtocol
150151
// Hashable and Equatable conformance are based on each non-nil
151152
// element on base collection.
152153
extension CompactedSequence: Equatable
153-
where Base: Equatable, Base.Element: Equatable {
154+
where Base.Element: Equatable {
154155

155156
@inlinable
156157
public static func ==(lhs: CompactedSequence,
157158
rhs: CompactedSequence) -> Bool {
158-
lhs.compactMap({ $0 })
159-
.elementsEqual(rhs.compactMap({ $0 }))
159+
lhs.elementsEqual(rhs)
160160
}
161161
}
162162

163163
extension CompactedSequence: Hashable
164-
where Base: Hashable, Base.Element: Hashable {
164+
where Element: Hashable {
165165
@inlinable
166166
public func hash(into hasher: inout Hasher) {
167-
for element in base {
168-
guard let unwrapped = element else { continue }
169-
hasher.combine(unwrapped)
167+
for element in self {
168+
hasher.combine(element)
170169
}
171170
}
172171
}
173172

174173
extension CompactedCollection: Equatable
175-
where Base: Equatable, Base.Element: Equatable {
174+
where Base.Element: Equatable {
176175

177176
@inlinable
178177
public static func ==(lhs: CompactedCollection,
179178
rhs: CompactedCollection) -> Bool {
180-
lhs.compactMap({ $0 })
181-
.elementsEqual(rhs.compactMap({ $0 }))
179+
lhs.elementsEqual(rhs)
182180
}
183181
}
184182

185183
extension CompactedCollection: Hashable
186-
where Base: Hashable, Base.Element: Hashable {
184+
where Element: Hashable {
187185

188186
@inlinable
189187
public func hash(into hasher: inout Hasher) {
190-
for element in base {
191-
guard let unwrapped = element else { continue }
192-
hasher.combine(unwrapped)
188+
for element in self {
189+
hasher.combine(element)
193190
}
194191
}
195192
}

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)