mirror of
https://github.com/apple/swift-collections
synced 2026-11-29 05:14:20 +00:00
Add UnfoldProducer
This commit is contained in:
3 files changed
+91
No files matched your search
@@ -69,6 +69,7 @@ target_sources(${module_name} PRIVATE
|
||||
"Protocols/Producer+Filter.swift"
|
||||
"Protocols/Producer+Map.swift"
|
||||
"Protocols/Producer+Reduce.swift"
|
||||
"Protocols/Producer+Unfold.swift"
|
||||
"Types/InputSpan.swift"
|
||||
"Types/MutableRef.swift"
|
||||
"Types/OutputSpan+Helpers.swift"
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
|
||||
@_lifetime(copy state)
|
||||
public func producer<
|
||||
State: ~Copyable & ~Escapable,
|
||||
Failure: Error,
|
||||
Element: ~Copyable
|
||||
>(
|
||||
state: consuming State,
|
||||
next: @escaping (inout State) throws(Failure) -> Element?
|
||||
) -> UnfoldProducer<State, Failure, Element> {
|
||||
.init(state: state, next: next)
|
||||
}
|
||||
|
||||
@frozen
|
||||
public struct UnfoldProducer<
|
||||
State: ~Copyable & ~Escapable,
|
||||
Failure: Error,
|
||||
Element: ~Copyable
|
||||
>: ~Copyable, ~Escapable {
|
||||
@usableFromInline
|
||||
internal let _next: (inout State) throws(Failure) -> Element?
|
||||
|
||||
@usableFromInline
|
||||
internal var _state: State
|
||||
|
||||
@inlinable
|
||||
@_lifetime(copy state)
|
||||
public init(
|
||||
state: consuming State,
|
||||
next: @escaping (inout State) throws(Failure) -> Element?
|
||||
) {
|
||||
self._next = next
|
||||
self._state = state
|
||||
}
|
||||
}
|
||||
|
||||
extension UnfoldProducer: Producer
|
||||
where
|
||||
State: ~Copyable & ~Escapable,
|
||||
Element: ~Copyable
|
||||
{
|
||||
@inlinable
|
||||
public var underestimatedCount: Int { 0 }
|
||||
|
||||
@inlinable
|
||||
@discardableResult
|
||||
@_lifetime(target: copy target)
|
||||
@_lifetime(self: copy self)
|
||||
public mutating func generate(
|
||||
into target: inout OutputSpan<Element>
|
||||
) throws(Failure) -> Bool {
|
||||
var c = 0
|
||||
while !target.isFull {
|
||||
guard let next = try _next(&self._state) else { break }
|
||||
target.append(next)
|
||||
c += 1
|
||||
}
|
||||
return c > 0
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@_lifetime(self: copy self)
|
||||
public mutating func next() throws(Failure) -> Element? {
|
||||
try _next(&_state)
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -65,6 +65,7 @@
|
||||
7D2A779C2F27F2BF005780C7 /* Deque+ExpressibleByArrayLiteral.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A775C2F27F2BF005780C7 /* Deque+ExpressibleByArrayLiteral.swift */; };
|
||||
7D2A77A02F27F367005780C7 /* RefTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A779D2F27F367005780C7 /* RefTests.swift */; };
|
||||
7D2F598E2FAC229B00D43FE7 /* MutableRefTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2F598D2FAC229B00D43FE7 /* MutableRefTests.swift */; };
|
||||
7D3F1F7B3007177100AD924D /* Producer+Unfold.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D3F1F7A3007176C00AD924D /* Producer+Unfold.swift */; };
|
||||
7D479CB02F450E4600A8CB59 /* RigidSet+BorrowingSequence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D479CAF2F450E3F00A8CB59 /* RigidSet+BorrowingSequence.swift */; };
|
||||
7D479CB32F45296A00A8CB59 /* UniqueSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D479CB12F45296000A8CB59 /* UniqueSet.swift */; };
|
||||
7D479CB52F452D6900A8CB59 /* UniqueSetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D479CB42F452D6700A8CB59 /* UniqueSetTests.swift */; };
|
||||
@@ -691,6 +692,7 @@
|
||||
7D2A77762F27F2BF005780C7 /* UniqueDeque+Replacements.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueDeque+Replacements.swift"; sourceTree = "<group>"; };
|
||||
7D2A779D2F27F367005780C7 /* RefTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RefTests.swift; sourceTree = "<group>"; };
|
||||
7D2F598D2FAC229B00D43FE7 /* MutableRefTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MutableRefTests.swift; sourceTree = "<group>"; };
|
||||
7D3F1F7A3007176C00AD924D /* Producer+Unfold.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Producer+Unfold.swift"; sourceTree = "<group>"; };
|
||||
7D479CAF2F450E3F00A8CB59 /* RigidSet+BorrowingSequence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RigidSet+BorrowingSequence.swift"; sourceTree = "<group>"; };
|
||||
7D479CB12F45296000A8CB59 /* UniqueSet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UniqueSet.swift; sourceTree = "<group>"; };
|
||||
7D479CB42F452D6700A8CB59 /* UniqueSetTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UniqueSetTests.swift; sourceTree = "<group>"; };
|
||||
@@ -1364,6 +1366,7 @@
|
||||
7D57EDE62F64913C0029D5E3 /* Producer+Filter.swift */,
|
||||
7D57EDD12F647DB10029D5E3 /* Producer+Map.swift */,
|
||||
7D57EDD22F647DB10029D5E3 /* Producer+Reduce.swift */,
|
||||
7D3F1F7A3007176C00AD924D /* Producer+Unfold.swift */,
|
||||
);
|
||||
path = Protocols;
|
||||
sourceTree = "<group>";
|
||||
@@ -2825,6 +2828,7 @@
|
||||
7D479CF32F47FB4100A8CB59 /* _HTable+Removal.swift in Sources */,
|
||||
7DE9203529CA70F3004483EB /* OrderedSet+CustomReflectable.swift in Sources */,
|
||||
7DEB31582F4E762200F9AF28 /* RigidDictionary+Equatable.swift in Sources */,
|
||||
7D3F1F7B3007177100AD924D /* Producer+Unfold.swift in Sources */,
|
||||
7DE920C929CA70F4004483EB /* BitSet+BidirectionalCollection.swift in Sources */,
|
||||
7DE920ED29CA70F4004483EB /* BitArray+Extras.swift in Sources */,
|
||||
7DE9206829CA70F4004483EB /* BigString+Chunk+Append and Insert.swift in Sources */,
|
||||
|
||||
Reference in new issue
Block a user