|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift Algorithms open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | +// slidingWindows(ofCount:) |
| 14 | +//===----------------------------------------------------------------------===// |
| 15 | + |
| 16 | +extension Collection { |
| 17 | + /// A collection for all contiguous windows of length size, the |
| 18 | + /// windows overlap. |
| 19 | + /// |
| 20 | + /// - Complexity: O(*1*) if the collection conforms to |
| 21 | + /// `RandomAccessCollection`, otherwise O(*k*) where `k` is `count`. |
| 22 | + /// Access to the next window is O(*1*). |
| 23 | + /// |
| 24 | + /// - Parameter count: The number of elements in each window subsequence. |
| 25 | + /// |
| 26 | + /// - Returns: If the collection is shorter than `size` the resulting |
| 27 | + /// SlidingWindows collection will be empty. |
| 28 | + public func slidingWindows(ofCount count: Int) -> SlidingWindows<Self> { |
| 29 | + SlidingWindows(base: self, size: count) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +public struct SlidingWindows<Base: Collection> { |
| 34 | + |
| 35 | + public let base: Base |
| 36 | + public let size: Int |
| 37 | + |
| 38 | + private var firstUpperBound: Base.Index? |
| 39 | + |
| 40 | + init(base: Base, size: Int) { |
| 41 | + precondition(size > 0, "SlidingWindows size must be greater than zero") |
| 42 | + self.base = base |
| 43 | + self.size = size |
| 44 | + self.firstUpperBound = base.index(base.startIndex, offsetBy: size, limitedBy: base.endIndex) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +extension SlidingWindows: Collection { |
| 49 | + |
| 50 | + public struct Index: Comparable { |
| 51 | + internal var lowerBound: Base.Index |
| 52 | + internal var upperBound: Base.Index |
| 53 | + public static func == (lhs: Index, rhs: Index) -> Bool { |
| 54 | + lhs.lowerBound == rhs.lowerBound |
| 55 | + } |
| 56 | + public static func < (lhs: Index, rhs: Index) -> Bool { |
| 57 | + lhs.lowerBound < rhs.lowerBound |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public var startIndex: Index { |
| 62 | + if let upperBound = firstUpperBound { |
| 63 | + return Index(lowerBound: base.startIndex, upperBound: upperBound) |
| 64 | + } else { |
| 65 | + return endIndex |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public var endIndex: Index { |
| 70 | + Index(lowerBound: base.endIndex, upperBound: base.endIndex) |
| 71 | + } |
| 72 | + |
| 73 | + public subscript(index: Index) -> Base.SubSequence { |
| 74 | + precondition(index.lowerBound != index.upperBound, "SlidingWindows index is out of range") |
| 75 | + return base[index.lowerBound..<index.upperBound] |
| 76 | + } |
| 77 | + |
| 78 | + public func index(after index: Index) -> Index { |
| 79 | + precondition(index < endIndex, "Advancing past end index") |
| 80 | + guard index.upperBound < base.endIndex else { return endIndex } |
| 81 | + return Index( |
| 82 | + lowerBound: base.index(after: index.lowerBound), |
| 83 | + upperBound: base.index(after: index.upperBound) |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + // TODO: Implement distance(from:to:), index(_:offsetBy:) and |
| 88 | + // index(_:offsetBy:limitedBy:) |
| 89 | + |
| 90 | +} |
| 91 | + |
| 92 | +extension SlidingWindows: BidirectionalCollection where Base: BidirectionalCollection { |
| 93 | + public func index(before index: Index) -> Index { |
| 94 | + precondition(index > startIndex, "Incrementing past start index") |
| 95 | + if index == endIndex { |
| 96 | + return Index( |
| 97 | + lowerBound: base.index(index.lowerBound, offsetBy: -size), |
| 98 | + upperBound: index.upperBound |
| 99 | + ) |
| 100 | + } else { |
| 101 | + return Index( |
| 102 | + lowerBound: base.index(before: index.lowerBound), |
| 103 | + upperBound: base.index(before: index.upperBound) |
| 104 | + ) |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +extension SlidingWindows: RandomAccessCollection where Base: RandomAccessCollection {} |
| 110 | +extension SlidingWindows: Equatable where Base: Equatable {} |
| 111 | +extension SlidingWindows: Hashable where Base: Hashable, Base.Index: Hashable {} |
| 112 | +extension SlidingWindows.Index: Hashable where Base.Index: Hashable {} |
0 commit comments