Files
swift-collections/Sources/ContainersPreview/Protocols/ContainerAlgorithms.swift
T
Karoy Lorentey e4a874b67a Rigid & Unique types: Add Equatable, Hashable, Custom[Debug]StringConvertible operations
Currently these are all conditional on copyable elements, and the types cannot conform to the actual protocols yet.
2026-02-04 23:24:08 -08:00

59 lines
2.1 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2024 - 2026 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
#if compiler(>=6.2) && COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
@available(SwiftStdlib 6.2, *)
extension Container where Self: ~Copyable & ~Escapable {
@_alwaysEmitIntoClient
internal func _spanwiseZip<Other: Container & ~Copyable & ~Escapable, E: Error>(
with other: borrowing Other,
by process: (Span<Element>, Span<Other.Element>) throws(E) -> Bool
) throws(E) -> (Index, Other.Index) {
var i = self.startIndex // End index of the current span in `self`
var j = other.startIndex // End index of the current span in `other`
var a: Span<Element> = .init()
var b: Span<Other.Element> = .init()
var pi = i // Start index of the original span we're currently processing in `self`
var pj = j // Start index of the original span we're currently processing in `other`
var ca = 0 // Count of the original span we're currently processing in `self`
var cb = 0 // Count of the original span we're currently processing in `other`
loop:
while true {
if a.isEmpty {
pi = i
a = self.nextSpan(after: &i)
ca = a.count
}
if b.isEmpty {
pj = j
b = other.nextSpan(after: &j)
cb = b.count
}
if a.isEmpty || b.isEmpty {
return (
(a.isEmpty ? i : self.index(pi, offsetBy: ca - a.count)),
(b.isEmpty ? j : other.index(pj, offsetBy: cb - b.count)))
}
let c = Swift.min(a.count, b.count)
guard try process(a._trim(first: c), b._trim(first: c)) else {
return (
(a.isEmpty ? i : self.index(pi, offsetBy: c)),
(b.isEmpty ? j : other.index(pj, offsetBy: c)))
}
}
}
}
#endif