File tree Expand file tree Collapse file tree 3 files changed +35
-26
lines changed Expand file tree Collapse file tree 3 files changed +35
-26
lines changed Original file line number Diff line number Diff line change @@ -79,21 +79,11 @@ extension PlatformEntity {
79
79
return nearestAncestor
80
80
}
81
81
82
- func descendantsBetween( _ bottomEntity: Base , and topEntity: Base ) -> [ Base ] {
83
- var result : [ Base ] = [ ]
84
- var entered = false
85
-
86
- for descendant in self . allDescendants {
87
- if descendant === bottomEntity {
88
- entered = true
89
- } else if descendant === topEntity {
90
- break
91
- } else if entered {
92
- result. append ( descendant)
93
- }
94
- }
95
-
96
- return result
82
+ func allDescendants( between bottomEntity: Base , and topEntity: Base ) -> some Sequence < Base > {
83
+ self . allDescendants
84
+ . lazy
85
+ . drop ( while: { $0 !== bottomEntity } )
86
+ . prefix ( while: { $0 !== topEntity } )
97
87
}
98
88
99
89
func receiver< PlatformSpecificEntity: PlatformEntity > (
@@ -102,14 +92,14 @@ extension PlatformEntity {
102
92
) -> PlatformSpecificEntity ? {
103
93
let frontEntity = self
104
94
guard
105
- let backEntity = Array ( frontEntity. ancestors) . last? . entityWithTag ( anchorID. hashValue) ,
95
+ let backEntity = ContiguousArray ( frontEntity. ancestors) . last? . entityWithTag ( anchorID. hashValue) ,
106
96
let commonAncestor = backEntity. nearestCommonAncestor ( with: frontEntity~ )
107
97
else {
108
98
return nil
109
99
}
110
100
111
101
return commonAncestor
112
- . descendantsBetween ( backEntity~ , and: frontEntity~ )
102
+ . allDescendants ( between : backEntity~ , and: frontEntity~ )
113
103
. compactMap { $0 as? PlatformSpecificEntity }
114
104
. first
115
105
}
Original file line number Diff line number Diff line change
1
+ postfix operator ~
2
+
3
+ postfix func ~ < LHS, T> ( lhs: LHS ) -> T {
4
+ lhs as! T
5
+ }
6
+
7
+ postfix func ~ < LHS, T> ( lhs: LHS ? ) -> T ? {
8
+ lhs as? T
9
+ }
10
+
11
+ func recursiveSequence< S: Sequence > ( _ sequence: S , children: @escaping ( S . Element ) -> S ) -> AnySequence < S . Element > {
12
+ AnySequence {
13
+ var mainIterator = sequence. makeIterator ( )
14
+ // Current generator, or `nil` if all sequences are exhausted:
15
+ var iterator : AnyIterator < S . Element > ?
16
+
17
+ return AnyIterator {
18
+ guard let iterator, let element = iterator. next ( ) else {
19
+ if let element = mainIterator. next ( ) {
20
+ iterator = recursiveSequence ( children ( element) , children: children) . makeIterator ( )
21
+ return element
22
+ }
23
+ return nil
24
+ }
25
+ return element
26
+ }
27
+ }
28
+ }
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments