From 638869256ad357efb7f6fb701e820e8832f19b0d Mon Sep 17 00:00:00 2001 From: Nate Cook Date: Wed, 2 Dec 2020 10:20:05 -0600 Subject: [PATCH] Add a few missing doc comments. --- Sources/Algorithms/Chain.swift | 4 +-- Sources/Algorithms/Chunked.swift | 2 ++ Sources/Algorithms/Combinations.swift | 2 ++ Sources/Algorithms/Intersperse.swift | 1 + Sources/Algorithms/Permutations.swift | 1 + Sources/Algorithms/SlidingWindows.swift | 11 ++++--- Sources/Algorithms/Stride.swift | 40 ++++++++++--------------- Sources/Algorithms/Trim.swift | 4 +-- 8 files changed, 32 insertions(+), 33 deletions(-) diff --git a/Sources/Algorithms/Chain.swift b/Sources/Algorithms/Chain.swift index 452aeef..92dbada 100644 --- a/Sources/Algorithms/Chain.swift +++ b/Sources/Algorithms/Chain.swift @@ -26,7 +26,7 @@ public struct Chain2 } extension Chain2: Sequence { - /// The iterator for a `Chain` sequence. + /// The iterator for a `Chain2` sequence. public struct Iterator: IteratorProtocol { @usableFromInline internal var iterator1: Base1.Iterator @@ -53,7 +53,7 @@ extension Chain2: Sequence { } extension Chain2: Collection where Base1: Collection, Base2: Collection { - /// A position in a `Chain` collection. + /// A position in a `Chain2` collection. public struct Index: Comparable { // The internal index representation, which can either be an index of the // first collection or the second. The `endIndex` of the first collection diff --git a/Sources/Algorithms/Chunked.swift b/Sources/Algorithms/Chunked.swift index e3dbba5..1dc35bb 100644 --- a/Sources/Algorithms/Chunked.swift +++ b/Sources/Algorithms/Chunked.swift @@ -9,6 +9,8 @@ // //===----------------------------------------------------------------------===// +/// A collection wrapper that breaks a collection into chunks based on a +/// predicate or projection. public struct LazyChunked { /// The collection that this instance provides a view onto. public let base: Base diff --git a/Sources/Algorithms/Combinations.swift b/Sources/Algorithms/Combinations.swift index 2416863..5d6450c 100644 --- a/Sources/Algorithms/Combinations.swift +++ b/Sources/Algorithms/Combinations.swift @@ -9,6 +9,7 @@ // //===----------------------------------------------------------------------===// +/// A collection wrapper that generates combinations of a base collection. public struct Combinations { /// The collection to iterate over for combinations. public let base: Base @@ -40,6 +41,7 @@ public struct Combinations { } extension Combinations: Sequence { + /// The iterator for a `Combinations` instance. public struct Iterator: IteratorProtocol { @usableFromInline internal let base: Base diff --git a/Sources/Algorithms/Intersperse.swift b/Sources/Algorithms/Intersperse.swift index f213098..acac42f 100644 --- a/Sources/Algorithms/Intersperse.swift +++ b/Sources/Algorithms/Intersperse.swift @@ -55,6 +55,7 @@ extension Intersperse: Sequence { } extension Intersperse: Collection where Base: Collection { + /// A position in an `Intersperse` collection. public struct Index: Comparable { enum Representation: Equatable { case element(Base.Index) diff --git a/Sources/Algorithms/Permutations.swift b/Sources/Algorithms/Permutations.swift index 905fdcc..6a548f9 100644 --- a/Sources/Algorithms/Permutations.swift +++ b/Sources/Algorithms/Permutations.swift @@ -32,6 +32,7 @@ public struct Permutations { } extension Permutations: Sequence { + /// The iterator for a `Permutations` instance. public struct Iterator: IteratorProtocol { @usableFromInline internal var base: Base diff --git a/Sources/Algorithms/SlidingWindows.swift b/Sources/Algorithms/SlidingWindows.swift index 8e86da5..24ef09d 100644 --- a/Sources/Algorithms/SlidingWindows.swift +++ b/Sources/Algorithms/SlidingWindows.swift @@ -30,14 +30,15 @@ extension Collection { } } +/// A collection wrapper that presents a sliding window over the elements of +/// a collection. public struct SlidingWindows { - public let base: Base public let size: Int - private var firstUpperBound: Base.Index? + internal var firstUpperBound: Base.Index? - init(base: Base, size: Int) { + internal init(base: Base, size: Int) { precondition(size > 0, "SlidingWindows size must be greater than zero") self.base = base self.size = size @@ -46,13 +47,15 @@ public struct SlidingWindows { } extension SlidingWindows: Collection { - + /// A position in a `SlidingWindows` collection. public struct Index: Comparable { internal var lowerBound: Base.Index internal var upperBound: Base.Index + public static func == (lhs: Index, rhs: Index) -> Bool { lhs.lowerBound == rhs.lowerBound } + public static func < (lhs: Index, rhs: Index) -> Bool { lhs.lowerBound < rhs.lowerBound } diff --git a/Sources/Algorithms/Stride.swift b/Sources/Algorithms/Stride.swift index a922895..93a1eb8 100644 --- a/Sources/Algorithms/Stride.swift +++ b/Sources/Algorithms/Stride.swift @@ -32,12 +32,12 @@ extension Sequence { } } +/// A wrapper that strides over a base sequence or collection. public struct Stride { + internal let base: Base + internal let stride: Int - let base: Base - let stride: Int - - init(base: Base, stride: Int) { + internal init(base: Base, stride: Int) { precondition(stride > 0, "striding must be greater than zero") self.base = base self.stride = stride @@ -51,12 +51,11 @@ extension Stride { } extension Stride: Sequence { - + /// An iterator over a `Stride` sequence. public struct Iterator: IteratorProtocol { - - var iterator: Base.Iterator - let stride: Int - var striding: Bool = false + internal var iterator: Base.Iterator + internal let stride: Int + internal var striding: Bool = false public mutating func next() -> Base.Element? { guard striding else { @@ -76,12 +75,11 @@ extension Stride: Sequence { } extension Stride: Collection where Base: Collection { - + /// A position in a `Stride` collection. public struct Index: Comparable { + internal let base: Base.Index - let base: Base.Index - - init(_ base: Base.Index) { + internal init(_ base: Base.Index) { self.base = base } @@ -192,29 +190,21 @@ extension Stride: BidirectionalCollection } } -extension Stride: RandomAccessCollection - where Base: RandomAccessCollection {} +extension Stride: RandomAccessCollection where Base: RandomAccessCollection {} -extension Stride: Equatable - where Base.Element: Equatable { - +extension Stride: Equatable where Base.Element: Equatable { public static func == (lhs: Stride, rhs: Stride) -> Bool { lhs.elementsEqual(rhs, by: ==) } - } -extension Stride: Hashable - where Base.Element: Hashable { - +extension Stride: Hashable where Base.Element: Hashable { public func hash(into hasher: inout Hasher) { hasher.combine(stride) for element in self { hasher.combine(element) } } - } -extension Stride.Index: Hashable - where Base.Index: Hashable {} +extension Stride.Index: Hashable where Base.Index: Hashable {} diff --git a/Sources/Algorithms/Trim.swift b/Sources/Algorithms/Trim.swift index 3491426..df3f9ef 100644 --- a/Sources/Algorithms/Trim.swift +++ b/Sources/Algorithms/Trim.swift @@ -10,7 +10,6 @@ //===----------------------------------------------------------------------===// extension BidirectionalCollection { - /// Returns a `SubSequence` formed by discarding all elements at the start and /// end of the collection which satisfy the given predicate. /// @@ -30,11 +29,11 @@ extension BidirectionalCollection { public func trimming( where predicate: (Element) throws -> Bool ) rethrows -> SubSequence { - // Consume elements from the front. let sliceStart = try firstIndex { try predicate($0) == false } ?? endIndex // sliceEnd is the index _after_ the last index to match the predicate. var sliceEnd = endIndex + while sliceStart != sliceEnd { let idxBeforeSliceEnd = index(before: sliceEnd) guard try predicate(self[idxBeforeSliceEnd]) else { @@ -42,6 +41,7 @@ extension BidirectionalCollection { } sliceEnd = idxBeforeSliceEnd } + // Trimmed everything. return self[Range(uncheckedBounds: (sliceStart, sliceStart))] }