From c33c50d2d8d7419970495e7bc8e4720d343279a5 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sat, 13 May 2023 16:29:11 +0800 Subject: [PATCH] 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. --- .../SwiftExtension/SwiftExtension.swift | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/Packages/vChewing_SwiftExtension/Sources/SwiftExtension/SwiftExtension.swift b/Packages/vChewing_SwiftExtension/Sources/SwiftExtension/SwiftExtension.swift index 9fdca53d..fc738707 100644 --- a/Packages/vChewing_SwiftExtension/Sources/SwiftExtension/SwiftExtension.swift +++ b/Packages/vChewing_SwiftExtension/Sources/SwiftExtension/SwiftExtension.swift @@ -274,3 +274,35 @@ public extension String { return result?.isEmpty ?? true ? nil : result } } + +// MARK: - Overlap Checker (for two sets) + +public extension Set where Element: Hashable { + func isOverlapped(with target: Set) -> Bool { + guard !target.isEmpty, !isEmpty else { return false } + var container: (Set, Set) + 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) -> Bool { + Set(self).isOverlapped(with: target) + } +}