SwiftImpl // Add overlap checker for Array and Set.
- This is at least usable for macOS 10.9; If macOS 10.10 and later, Swift has built-in functions to achieve this.
This commit is contained in:
parent
a22eed4578
commit
c33c50d2d8
|
@ -274,3 +274,35 @@ public extension String {
|
||||||
return result?.isEmpty ?? true ? nil : result
|
return result?.isEmpty ?? true ? nil : result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: - Overlap Checker (for two sets)
|
||||||
|
|
||||||
|
public extension Set where Element: Hashable {
|
||||||
|
func isOverlapped(with target: Set<Element>) -> Bool {
|
||||||
|
guard !target.isEmpty, !isEmpty else { return false }
|
||||||
|
var container: (Set<Element>, Set<Element>)
|
||||||
|
if target.count <= count {
|
||||||
|
container = (target, self)
|
||||||
|
} else {
|
||||||
|
container = (self, target)
|
||||||
|
}
|
||||||
|
for neta in container.0 {
|
||||||
|
guard !container.1.contains(neta) else { return true }
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func isOverlapped(with target: [Element]) -> Bool {
|
||||||
|
isOverlapped(with: Set(target))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public extension Array where Element: Hashable {
|
||||||
|
func isOverlapped(with target: [Element]) -> Bool {
|
||||||
|
Set(self).isOverlapped(with: Set(target))
|
||||||
|
}
|
||||||
|
|
||||||
|
func isOverlapped(with target: Set<Element>) -> Bool {
|
||||||
|
Set(self).isOverlapped(with: target)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue