Skip to content

Commit 13d9171

Browse files
Implementing hashable conditional conformance because synthetization is not possible
1 parent dcade20 commit 13d9171

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

Sources/Algorithms/Chunked.swift

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -248,18 +248,18 @@ extension Collection {
248248
}
249249

250250
//===----------------------------------------------------------------------===//
251-
// chunks(of:)
251+
// chunks(ofCount:)
252252
//===----------------------------------------------------------------------===//
253253

254254
/// A collection that presents the elements of its base collection
255-
/// in `SubSequence` chunks of any given size.
255+
/// in `SubSequence` chunks of any given count.
256256
///
257257
/// A `ChunkedByCount` is a lazy view on the base Collection, but it does not implicitly confer
258258
/// laziness on algorithms applied to its result. In other words, for ordinary collections `c`:
259259
///
260-
/// * `c.chunks(of: 3)` does not create new storage
261-
/// * `c.chunks(of: 3).map(f)` maps eagerly and returns a new array
262-
/// * `c.lazy.chunks(of: 3).map(f)` maps lazily and returns a `LazyMapCollection`
260+
/// * `c.chunks(ofCount: 3)` does not create new storage
261+
/// * `c.chunks(ofCount: 3).map(f)` maps eagerly and returns a new array
262+
/// * `c.lazy.chunks(ofCount: 3).map(f)` maps lazily and returns a `LazyMapCollection`
263263
public struct ChunkedByCount<Base: Collection> {
264264

265265
public typealias Element = Base.SubSequence
@@ -274,7 +274,7 @@ public struct ChunkedByCount<Base: Collection> {
274274
internal var computedStartIndex: Index
275275

276276
/// Creates a view instance that presents the elements of `base`
277-
/// in `SubSequence` chunks of the given size.
277+
/// in `SubSequence` chunks of the given count.
278278
///
279279
/// - Complexity: O(n)
280280
@inlinable
@@ -305,11 +305,13 @@ extension ChunkedByCount: Collection {
305305
}
306306
}
307307

308+
/// - Complexity: O(n)
308309
public var startIndex: Index { computedStartIndex }
309310
public var endIndex: Index {
310311
Index(_baseRange: base.endIndex..<base.endIndex)
311312
}
312313

314+
/// - Complexity: O(n)
313315
public subscript(i: Index) -> Element {
314316
base[i.baseRange]
315317
}
@@ -376,9 +378,9 @@ where Base: RandomAccessCollection {
376378

377379
extension Collection {
378380
/// Returns a `ChunkedCollection<Self>` view presenting the elements
379-
/// in chunks with count of the given size parameter.
381+
/// in chunks with count of the given count parameter.
380382
///
381-
/// - Parameter size: The size of the chunks. If the size parameter
383+
/// - Parameter size: The size of the chunks. If the count parameter
382384
/// is evenly divided by the count of the base `Collection` all the
383385
/// chunks will have the count equals to size.
384386
/// Otherwise, the last chunk will contain the remaining elements.
@@ -400,9 +402,21 @@ extension Collection {
400402

401403
// Conditional conformances.
402404
extension ChunkedByCount: Equatable where Base: Equatable {}
403-
extension ChunkedByCount: Hashable where Base: Hashable, Base.Index: Hashable {}
405+
406+
// Since we have another stored property of type `Index` on the
407+
// collection, synthetization of hashble conformace would require
408+
// a `Base.Index: Hashable` constraint, so we implement the hasher
409+
// only in terms of base. Since the computed index is based on it,
410+
// it should make a difference here.
411+
extension ChunkedByCount: Hashable where Base: Hashable {
412+
public func hash(into hasher: inout Hasher) {
413+
hasher.combine(base)
414+
}
415+
}
404416
extension ChunkedByCount.Index: Hashable where Base.Index: Hashable {}
405417

406418
// Lazy conditional conformance.
407-
extension ChunkedByCount: LazySequenceProtocol where Base: LazySequenceProtocol {}
408-
extension ChunkedByCount: LazyCollectionProtocol where Base: LazyCollectionProtocol {}
419+
extension ChunkedByCount: LazySequenceProtocol
420+
where Base: LazySequenceProtocol {}
421+
extension ChunkedByCount: LazyCollectionProtocol
422+
where Base: LazyCollectionProtocol {}

0 commit comments

Comments
 (0)