|
| 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 | +// windows(size:) |
| 14 | +//===----------------------------------------------------------------------===// |
| 15 | + |
| 16 | +extension Collection { |
| 17 | + /// Returns a collection for all contiguous windows of length size. The windows overlap. |
| 18 | + /// If the slice is shorter than `size`, the collection returns an empty subsequence. |
| 19 | + public func windows(size: Int) -> WindowsSubSequence<Self> { |
| 20 | + WindowsSubSequence(base: self, size: size) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +public struct WindowsSubSequence<Base: Collection> { |
| 25 | + |
| 26 | + public struct Index: Comparable { |
| 27 | + |
| 28 | + internal var lowerBound: Base.Index |
| 29 | + internal var upperBound: Base.Index |
| 30 | + |
| 31 | + public static func == (lhs: Index, rhs: Index) -> Bool { |
| 32 | + lhs.lowerBound == rhs.upperBound && lhs.upperBound == rhs.upperBound |
| 33 | + } |
| 34 | + |
| 35 | + public static func < (lhs: Index, rhs: Index) -> Bool { |
| 36 | + lhs.upperBound < rhs.upperBound |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public let base: Base |
| 41 | + public let size: Int |
| 42 | + |
| 43 | + public let startIndex: Index |
| 44 | + public let endIndex: Index |
| 45 | + |
| 46 | + public init(base: Base, size: Int) { |
| 47 | + precondition(size > 0, "Windows size must be greater than zero") |
| 48 | + self.base = base |
| 49 | + self.size = size |
| 50 | + let limit = base.count - size |
| 51 | + if limit > 0, let firstUpperBound = base.index(base.startIndex, offsetBy: size, limitedBy: base.endIndex) { |
| 52 | + startIndex = Index(lowerBound: base.startIndex, upperBound: firstUpperBound) |
| 53 | + endIndex = Index(lowerBound: base.endIndex, upperBound: base.endIndex) |
| 54 | + } else { |
| 55 | + startIndex = Index(lowerBound: base.startIndex, upperBound: base.startIndex) |
| 56 | + endIndex = startIndex |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +extension WindowsSubSequence: Collection { |
| 63 | + |
| 64 | + public subscript(index: Index) -> Base.SubSequence { |
| 65 | + base[index.lowerBound..<index.upperBound] |
| 66 | + } |
| 67 | + |
| 68 | + public func index(after index: Index) -> Index { |
| 69 | + guard index.upperBound < base.endIndex else { return endIndex } |
| 70 | + return Index(lowerBound: base.index(after: index.lowerBound), upperBound: base.index(after: index.upperBound)) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +extension WindowsSubSequence: BidirectionalCollection where Base: BidirectionalCollection { |
| 75 | + |
| 76 | + public func index(before index: Index) -> Index { |
| 77 | + guard let lowerBound = base.index(index.lowerBound, offsetBy: -size, limitedBy: base.startIndex) else { return startIndex } |
| 78 | + return Index(lowerBound: lowerBound, upperBound: index == endIndex ? index.upperBound : base.index(before: index.upperBound)) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +extension WindowsSubSequence: RandomAccessCollection where Base: RandomAccessCollection {} |
| 83 | + |
| 84 | +extension WindowsSubSequence: Equatable where Base: Equatable {} |
| 85 | +extension WindowsSubSequence: Hashable where Base: Hashable, Base.Index: Hashable {} |
| 86 | +extension WindowsSubSequence.Index: Hashable where Base.Index: Hashable {} |
0 commit comments