Skip to content

Commit 1ee6a11

Browse files
committed
Fill out remaining intersection functions
1 parent 4da580c commit 1ee6a11

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Foundation/NSOrderedSet.swift

+15-2
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,21 @@ extension NSOrderedSet {
144144
return false
145145
}
146146

147-
public func intersectsOrderedSet(other: NSOrderedSet) -> Bool { NSUnimplemented() }
148-
public func intersectsSet(set: Set<NSObject>) -> Bool { NSUnimplemented() }
147+
public func intersectsOrderedSet(other: NSOrderedSet) -> Bool {
148+
if count < other.count {
149+
return contains { obj in other.containsObject(obj as! NSObject) }
150+
} else {
151+
return other.contains { obj in containsObject(obj) }
152+
}
153+
}
154+
155+
public func intersectsSet(set: Set<NSObject>) -> Bool {
156+
if count < set.count {
157+
return contains { obj in set.contains(obj as! NSObject) }
158+
} else {
159+
return set.contains { obj in containsObject(obj) }
160+
}
161+
}
149162

150163
public func isSubsetOfOrderedSet(other: NSOrderedSet) -> Bool {
151164
return !self.contains { obj in

TestFoundation/TestNSOrderedSet.swift

+5
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,19 @@ class TestNSOrderedSet : XCTestCase {
250250
func test_Intersection() {
251251
let set = NSMutableOrderedSet(arrayLiteral: "foo".bridge(), "bar".bridge(), "baz".bridge())
252252
let otherSet = NSOrderedSet(array: ["foo", "baz"].bridge().bridge())
253+
XCTAssert(set.intersectsOrderedSet(otherSet))
253254
let otherOtherSet = Set(["foo".bridge(), "123".bridge()])
255+
XCTAssert(set.intersectsSet(otherOtherSet))
254256
set.intersectOrderedSet(otherSet)
255257
XCTAssertEqual(set.count, 2)
256258
XCTAssertEqual(set[0] as? NSString, "foo")
257259
XCTAssertEqual(set[1] as? NSString, "baz")
258260
set.intersectSet(otherOtherSet)
259261
XCTAssertEqual(set.count, 1)
260262
XCTAssertEqual(set[0] as? NSString, "foo")
263+
264+
let nonIntersectingSet = Set(["asdf".bridge()])
265+
XCTAssertFalse(set.intersectsSet(nonIntersectingSet))
261266
}
262267

263268
func test_Subtraction() {

0 commit comments

Comments
 (0)