- Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathAsyncChunkedOnProjectionSequence.swift
115 lines (97 loc) · 3.55 KB
/
AsyncChunkedOnProjectionSequence.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Async Algorithms open source project
//
// Copyright (c) 2021 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
//
//===----------------------------------------------------------------------===//
@available(AsyncAlgorithms 1.0,*)
extensionAsyncSequence{
/// Creates an asynchronous sequence that creates chunks of a given `RangeReplaceableCollection` type on the uniqueness of a given subject.
@available(AsyncAlgorithms 1.0,*)
@inlinable
publicfunc chunked<Subject:Equatable, Collected:RangeReplaceableCollection>(
into:Collected.Type,
on projection:@escaping@Sendable(Element)->Subject
)->AsyncChunkedOnProjectionSequence<Self,Subject,Collected>{
AsyncChunkedOnProjectionSequence(self, projection: projection)
}
/// Creates an asynchronous sequence that creates chunks on the uniqueness of a given subject.
@available(AsyncAlgorithms 1.0,*)
@inlinable
publicfunc chunked<Subject:Equatable>(
on projection:@escaping@Sendable(Element)->Subject
)->AsyncChunkedOnProjectionSequence<Self,Subject,[Element]>{
chunked(into:[Element].self, on: projection)
}
}
/// An `AsyncSequence` that chunks on a subject when it differs from the last element.
@available(AsyncAlgorithms 1.0,*)
publicstructAsyncChunkedOnProjectionSequence<
Base:AsyncSequence,
Subject:Equatable,
Collected:RangeReplaceableCollection
>:AsyncSequencewhere Collected.Element ==Base.Element{
publictypealiasElement=(Subject,Collected)
/// The iterator for a `AsyncChunkedOnProjectionSequence` instance.
@frozen
publicstructIterator:AsyncIteratorProtocol{
@usableFromInline
varbase:Base.AsyncIterator
@usableFromInline
letprojection:@Sendable(Base.Element)->Subject
@usableFromInline
init(base:Base.AsyncIterator, projection:@escaping@Sendable(Base.Element)->Subject){
self.base = base
self.projection = projection
}
@usableFromInline
varhangingNext:(Subject,Base.Element)?
@inlinable
publicmutatingfunc next()asyncrethrows->(Subject,Collected)?{
varfirstOpt= hangingNext
if firstOpt ==nil{
letnextOpt=tryawait base.next()
iflet next = nextOpt {
firstOpt =(projection(next), next)
}
}else{
hangingNext =nil
}
guardlet first = firstOpt else{
returnnil
}
varresult:Collected=.init()
result.append(first.1)
whilelet next =tryawait base.next(){
letsubj=projection(next)
guard subj == first.0else{
hangingNext =(subj, next)
break
}
result.append(next)
}
return (first.0, result)
}
}
@usableFromInline
letbase:Base
@usableFromInline
letprojection:@Sendable(Base.Element)->Subject
@usableFromInline
init(_ base:Base, projection:@escaping@Sendable(Base.Element)->Subject){
self.base = base
self.projection = projection
}
@inlinable
publicfunc makeAsyncIterator()->Iterator{
Iterator(base: base.makeAsyncIterator(), projection: projection)
}
}
@available(AsyncAlgorithms 1.0,*)
extensionAsyncChunkedOnProjectionSequence:Sendablewhere Base:Sendable, Base.Element:Sendable{}
@available(*, unavailable)
extensionAsyncChunkedOnProjectionSequence.Iterator:Sendable{}