|
| 1 | +// This source file is part of the Swift.org open source project |
| 2 | +// |
| 3 | +// Copyright (c) 2020 Apple Inc. and the Swift project authors |
| 4 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 5 | +// |
| 6 | +// See http://swift.org/LICENSE.txt for license information |
| 7 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 8 | + |
| 9 | + |
| 10 | +/// Combine two sequences which are already ordered with respect to |
| 11 | +/// `compare` into a single ordered sequence with the items from each |
| 12 | +/// sequence, in order. |
| 13 | +/// |
| 14 | +/// - Parameters: |
| 15 | +/// - lhs: The left-hand side sequence. |
| 16 | +/// - rhs: The right-hand side sequence. |
| 17 | +/// - areInIncreasingOrder: A predicate that returns true if its first |
| 18 | +/// argument should be ordered before its second argument; otherwise, |
| 19 | +/// false. Each sequence *MUST* already be in order with respect to this |
| 20 | +/// predicate (this is checked, in debug builds). |
| 21 | +/// - Returns: A list of pairs, ordered with respect to `compare`. The first |
| 22 | +/// element in the pair will come from the LHS sequence, and the second will |
| 23 | +/// come from the RHS sequence, and equal elements appear in both lists will be |
| 24 | +/// returned together. |
| 25 | +@inlinable |
| 26 | +public func orderedZip<S: Sequence>( |
| 27 | + _ lhs: S, |
| 28 | + _ rhs: S, |
| 29 | + by areInIncreasingOrder: (S.Element, S.Element) -> Bool |
| 30 | +) -> [(S.Element?, S.Element?)] { |
| 31 | + var result: [(S.Element?, S.Element?)] = [] |
| 32 | + result.reserveCapacity(max(lhs.underestimatedCount, rhs.underestimatedCount)) |
| 33 | + |
| 34 | + // Initialize. |
| 35 | + var lhsIt = lhs.makeIterator() |
| 36 | + var rhsIt = rhs.makeIterator() |
| 37 | + var lhsItem = lhsIt.next() |
| 38 | + var rhsItem = rhsIt.next() |
| 39 | + |
| 40 | + // While each list has items... |
| 41 | + while let a = lhsItem, let b = rhsItem { |
| 42 | + // If a < b, take a. |
| 43 | + if areInIncreasingOrder(a, b) { |
| 44 | + result.append((a, nil)) |
| 45 | + lhsItem = lhsIt.next() |
| 46 | + assert(lhsItem == nil || !areInIncreasingOrder(lhsItem!, a)) |
| 47 | + continue |
| 48 | + } |
| 49 | + |
| 50 | + // If b < a, take b. |
| 51 | + if areInIncreasingOrder(b, a) { |
| 52 | + result.append((nil, b)) |
| 53 | + rhsItem = rhsIt.next() |
| 54 | + assert(rhsItem == nil || !areInIncreasingOrder(rhsItem!, b)) |
| 55 | + continue |
| 56 | + } |
| 57 | + |
| 58 | + // Otherwise, a == b, take them both. |
| 59 | + result.append((a, b)) |
| 60 | + lhsItem = lhsIt.next() |
| 61 | + assert(lhsItem == nil || !areInIncreasingOrder(lhsItem!, a)) |
| 62 | + rhsItem = rhsIt.next() |
| 63 | + assert(rhsItem == nil || !areInIncreasingOrder(rhsItem!, b)) |
| 64 | + } |
| 65 | + |
| 66 | + // Add an remaining items from either list (only one of these can actually be true). |
| 67 | + while let a = lhsItem { |
| 68 | + result.append((a, nil)) |
| 69 | + lhsItem = lhsIt.next() |
| 70 | + assert(lhsItem == nil || !areInIncreasingOrder(lhsItem!, a)) |
| 71 | + } |
| 72 | + while let b = rhsItem { |
| 73 | + result.append((nil, b)) |
| 74 | + rhsItem = rhsIt.next() |
| 75 | + assert(rhsItem == nil || !areInIncreasingOrder(rhsItem!, b)) |
| 76 | + } |
| 77 | + |
| 78 | + return result |
| 79 | +} |
| 80 | + |
| 81 | +/// Combine a list of sequences which are already ordered with respect to |
| 82 | +/// `compare` into a single ordered sequence with the items from each sequence, |
| 83 | +/// in order. |
| 84 | +/// |
| 85 | +/// - Parameters: |
| 86 | +/// - sequences: The list of sequences. |
| 87 | +/// - areInIncreasingOrder: A predicate that returns true if its first |
| 88 | +/// argument should be ordered before its second argument; otherwise, |
| 89 | +/// false. Each sequence *MUST* already be in order with respect to this |
| 90 | +/// predicate (this is checked, in debug builds). |
| 91 | +/// - Returns: A sequence of arrays, ordered with respect to `compare`. Each row |
| 92 | +/// in the result will have exactly `sequences.count` entries, and each Nth item |
| 93 | +/// either be nil or the equivalently ordered item from the Nth sequence. |
| 94 | +@inlinable |
| 95 | +public func orderedZip<S: Sequence>( |
| 96 | + sequences: [S], |
| 97 | + by areInIncreasingOrder: (S.Element, S.Element) -> Bool |
| 98 | +) -> [[S.Element?]] { |
| 99 | + var result: [[S.Element?]] = [] |
| 100 | + result.reserveCapacity(sequences.map{ $0.underestimatedCount }.max() ?? 0) |
| 101 | + |
| 102 | + // Initialize iterators. |
| 103 | + var iterators = sequences.map{ $0.makeIterator() } |
| 104 | + |
| 105 | + // We keep a "current" item for each iterator. |
| 106 | + // |
| 107 | + // This strategy is not particularly efficient if we have many many |
| 108 | + // sequences with highly varied lengths, but is simple. |
| 109 | + var items: [S.Element?] = [] |
| 110 | + for i in 0 ..< iterators.count { |
| 111 | + items.append(iterators[i].next()) |
| 112 | + } |
| 113 | + |
| 114 | + // Iterate... |
| 115 | + while true { |
| 116 | + // Find the smallest item. |
| 117 | + var maybeSmallest: S.Element? |
| 118 | + var smallestIndex: Int = -1 |
| 119 | + for (i, itemOpt) in items.enumerated() { |
| 120 | + if let item = itemOpt, maybeSmallest == nil || areInIncreasingOrder(item, maybeSmallest!) { |
| 121 | + maybeSmallest = item |
| 122 | + smallestIndex = i |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // If there was no smallest, we have reached the end of all lists. |
| 127 | + guard let smallest = maybeSmallest else { |
| 128 | + // We are done. |
| 129 | + break |
| 130 | + } |
| 131 | + |
| 132 | + // Now, we have to take all items equivalent to the smallest. Since we |
| 133 | + // have already taken the smallest from each list, we can find this by |
| 134 | + // reversing the equivalence (assuming a proper predicate). We do this |
| 135 | + // in lieu of tracking equivalence while we iterate through the items. |
| 136 | + var row: [S.Element?] = [] |
| 137 | + for (i, itemOpt) in items.enumerated() { |
| 138 | + // If this is the smallest item, or is not greater than the smallest |
| 139 | + // (and thus is equal), it should go in the list. |
| 140 | + if let item = itemOpt, i == smallestIndex || !areInIncreasingOrder(smallest, item) { |
| 141 | + // Walk the item (and validate individual sequence ordering, in debug mode). |
| 142 | + let next = iterators[i].next() |
| 143 | + assert(next == nil || !areInIncreasingOrder(next!, item)) |
| 144 | + items[i] = next |
| 145 | + row.append(item) |
| 146 | + } else { |
| 147 | + // Otherwise, no entry for this sequence. |
| 148 | + row.append(nil) |
| 149 | + } |
| 150 | + } |
| 151 | + result.append(row) |
| 152 | + } |
| 153 | + |
| 154 | + return result |
| 155 | +} |
0 commit comments