-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathNSSet.swift
450 lines (378 loc) · 13.5 KB
/
NSSet.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
import CoreFoundation
extension Set : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
public static func _getObjectiveCType() -> Any.Type {
return NSSet.self
}
public func _bridgeToObjectiveC() -> NSSet {
let buffer = UnsafeMutablePointer<AnyObject?>.alloc(count)
for (idx, obj) in enumerate() {
buffer.advancedBy(idx).initialize(_NSObjectRepresentableBridge(obj))
}
let set = NSSet(objects: buffer, count: count)
buffer.destroy(count)
buffer.dealloc(count)
return set
}
public static func _forceBridgeFromObjectiveC(x: NSSet, inout result: Set?) {
var set = Set<Element>()
var failedConversion = false
if x.dynamicType == NSSet.self || x.dynamicType == NSMutableSet.self {
x.enumerateObjectsUsingBlock() { obj, stop in
if let o = obj as? Element {
set.insert(o)
} else {
failedConversion = true
stop.memory = true
}
}
} else if x.dynamicType == _NSCFSet.self {
let cf = x._cfObject
let cnt = CFSetGetCount(cf)
let objs = UnsafeMutablePointer<UnsafePointer<Void>>.alloc(cnt)
CFSetGetValues(cf, objs)
for var idx = 0; idx < cnt; idx++ {
let obj = unsafeBitCast(objs.advancedBy(idx), AnyObject.self)
if let o = obj as? Element {
set.insert(o)
} else {
failedConversion = true
break
}
}
objs.destroy(cnt)
objs.dealloc(cnt)
}
if !failedConversion {
result = set
}
}
public static func _conditionallyBridgeFromObjectiveC(x: NSSet, inout result: Set?) -> Bool {
self._forceBridgeFromObjectiveC(x, result: &result)
return true
}
}
public class NSSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCoding {
private let _cfinfo = _CFInfo(typeID: CFSetGetTypeID())
internal var _storage = Set<NSObject>()
public var count: Int {
get {
if self.dynamicType === NSSet.self || self.dynamicType === NSMutableSet.self {
return _storage.count
} else {
NSRequiresConcreteImplementation()
}
}
}
public func member(object: AnyObject) -> AnyObject? {
if self.dynamicType === NSSet.self || self.dynamicType === NSMutableSet.self {
if let obj = object as? NSObject {
if _storage.contains(obj) {
return obj // this is not exactly the same behavior, but it is reasonably close
}
}
return nil
} else {
NSRequiresConcreteImplementation()
}
}
public func objectEnumerator() -> NSEnumerator {
if self.dynamicType === NSSet.self || self.dynamicType === NSMutableSet.self {
return NSGeneratorEnumerator(_storage.generate())
} else {
NSRequiresConcreteImplementation()
}
}
public convenience override init() {
self.init(objects: UnsafePointer<AnyObject?>(), count: 0)
}
public init(objects: UnsafePointer<AnyObject?>, count cnt: Int) {
super.init()
for idx in 0..<cnt {
let obj = objects[idx] as! NSObject
_storage.insert(obj)
}
}
public required convenience init?(coder aDecoder: NSCoder) {
NSUnimplemented()
}
public func encodeWithCoder(aCoder: NSCoder) {
NSUnimplemented()
}
public func copyWithZone(zone: NSZone) -> AnyObject {
NSUnimplemented()
}
public func mutableCopyWithZone(zone: NSZone) -> AnyObject {
NSUnimplemented()
}
public static func supportsSecureCoding() -> Bool {
return true
}
public func descriptionWithLocale(locale: AnyObject?) -> String { NSUnimplemented() }
override internal var _cfTypeID: CFTypeID {
return CFSetGetTypeID()
}
}
extension NSSet {
public convenience init(object: AnyObject) {
self.init(array: [object])
}
// public convenience init(set: Set<NSObject>) {
// self.init(set: set, copyItems: false)
// }
// public convenience init(set: Set<NSObject>, copyItems flag: Bool) {
// var array = Array<NSObject>()
// for object in set {
// var value = object
// if flag {
// value = object.copy() as! NSObject
// }
// array.append(value)
// }
// self.init(array: array)
// }
public convenience init(array: [AnyObject]) {
let buffer = UnsafeMutablePointer<AnyObject?>.alloc(array.count)
for var idx = 0; idx < array.count; idx++ {
buffer.advancedBy(idx).initialize(array[idx])
}
self.init(objects: buffer, count: array.count)
buffer.destroy(array.count)
buffer.dealloc(array.count)
}
}
extension NSSet {
public var allObjects: [AnyObject] {
get {
if self.dynamicType === NSSet.self || self.dynamicType === NSMutableSet.self {
return _storage.map { $0 }
} else {
var objects = [AnyObject]()
let enumerator = objectEnumerator()
while let obj = enumerator.nextObject() {
objects.append(obj)
}
return objects
}
}
}
public func anyObject() -> AnyObject? {
return objectEnumerator().nextObject()
}
public func containsObject(anObject: AnyObject) -> Bool {
return member(anObject) != nil
}
public func intersectsSet(otherSet: Set<NSObject>) -> Bool {
if count < otherSet.count {
for obj in allObjects {
if otherSet.contains(obj as! NSObject) {
return true
}
}
} else {
for obj in otherSet {
if containsObject(obj) {
return true
}
}
}
return false
}
public func isEqualToSet(otherSet: Set<NSObject>) -> Bool {
if count != otherSet.count {
return false
}
for obj in otherSet where !containsObject(obj) {
return false
}
return true
}
public func isSubsetOfSet(otherSet: Set<NSObject>) -> Bool {
for case let obj as NSObject in allObjects where !otherSet.contains(obj) {
return false
}
return true
}
public func setByAddingObject(anObject: AnyObject) -> Set<NSObject> {
return self.setByAddingObjectsFromArray([anObject])
}
public func setByAddingObjectsFromSet(other: Set<NSObject>) -> Set<NSObject> {
var result: Set<NSObject>
if self.dynamicType === NSSet.self || self.dynamicType === NSMutableSet.self {
result = _storage
} else {
result = Set<NSObject>()
for case let obj as NSObject in self {
result.insert(obj)
}
}
return result.union(other)
}
public func setByAddingObjectsFromArray(other: [AnyObject]) -> Set<NSObject> {
var result: Set<NSObject>
if self.dynamicType === NSSet.self || self.dynamicType === NSMutableSet.self {
result = _storage
} else {
result = Set<NSObject>()
for case let obj as NSObject in self {
result.insert(obj)
}
}
for case let obj as NSObject in other {
result.insert(obj)
}
return result
}
public func enumerateObjectsUsingBlock(block: (AnyObject, UnsafeMutablePointer<ObjCBool>) -> Void) {
enumerateObjectsWithOptions([], usingBlock: block)
}
public func enumerateObjectsWithOptions(opts: NSEnumerationOptions, usingBlock block: (AnyObject, UnsafeMutablePointer<ObjCBool>) -> Void) {
var stop : ObjCBool = false
for obj in self {
withUnsafeMutablePointer(&stop) { stop in
block(obj, stop)
}
if stop {
break
}
}
}
public func objectsPassingTest(predicate: (AnyObject, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Set<NSObject> {
return objectsWithOptions([], passingTest: predicate)
}
public func objectsWithOptions(opts: NSEnumerationOptions, passingTest predicate: (AnyObject, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Set<NSObject> {
var result = Set<NSObject>()
enumerateObjectsWithOptions(opts) { obj, stopp in
if predicate(obj, stopp) {
result.insert(obj as! NSObject)
}
}
return result
}
}
extension NSSet : _CFBridgable, _SwiftBridgable {
internal var _cfObject: CFSetRef { return unsafeBitCast(self, CFSetRef.self) }
internal var _swiftObject: Set<NSObject> {
var set: Set<NSObject>?
Set._forceBridgeFromObjectiveC(self, result: &set)
return set!
}
}
extension CFSetRef : _NSBridgable, _SwiftBridgable {
internal var _nsObject: NSSet { return unsafeBitCast(self, NSSet.self) }
internal var _swiftObject: Set<NSObject> { return _nsObject._swiftObject }
}
extension Set : _NSBridgable, _CFBridgable {
internal var _nsObject: NSSet { return _bridgeToObjectiveC() }
internal var _cfObject: CFSetRef { return _nsObject._cfObject }
}
extension NSSet : SequenceType {
public typealias Generator = NSEnumerator.Generator
public func generate() -> Generator {
return self.objectEnumerator().generate()
}
}
public class NSMutableSet : NSSet {
public func addObject(object: AnyObject) {
if self.dynamicType === NSMutableSet.self {
_storage.insert(object as! NSObject)
} else {
NSRequiresConcreteImplementation()
}
}
public func removeObject(object: AnyObject) {
if self.dynamicType === NSMutableSet.self {
if let obj = object as? NSObject {
_storage.remove(obj)
}
} else {
NSRequiresConcreteImplementation()
}
}
override public init(objects: UnsafePointer<AnyObject?>, count cnt: Int) {
super.init(objects: objects, count: cnt)
}
public convenience init() {
self.init(capacity: 0)
}
public required init(capacity numItems: Int) {
super.init(objects: nil, count: 0)
}
public required convenience init?(coder: NSCoder) {
NSUnimplemented()
}
public func addObjectsFromArray(array: [AnyObject]) {
if self.dynamicType === NSMutableSet.self {
for case let obj as NSObject in array {
_storage.insert(obj)
}
} else {
array.forEach(addObject)
}
}
public func intersectSet(otherSet: Set<NSObject>) {
if self.dynamicType === NSMutableSet.self {
_storage.intersectInPlace(otherSet)
} else {
for case let obj as NSObject in self where !otherSet.contains(obj) {
removeObject(obj)
}
}
}
public func minusSet(otherSet: Set<NSObject>) {
if self.dynamicType === NSMutableSet.self {
_storage.subtractInPlace(otherSet)
} else {
otherSet.forEach(removeObject)
}
}
public func removeAllObjects() {
if self.dynamicType === NSMutableSet.self {
_storage.removeAll()
} else {
forEach(removeObject)
}
}
public func unionSet(otherSet: Set<NSObject>) {
if self.dynamicType === NSMutableSet.self {
_storage.unionInPlace(otherSet)
} else {
otherSet.forEach(addObject)
}
}
public func setSet(otherSet: Set<NSObject>) {
if self.dynamicType === NSMutableSet.self {
_storage = otherSet
} else {
removeAllObjects()
unionSet(otherSet)
}
}
}
/**************** Counted Set ****************/
public class NSCountedSet : NSMutableSet {
public required init(capacity numItems: Int) { NSUnimplemented() }
// public convenience init(array: [AnyObject]) { NSUnimplemented() }
public convenience init(set: Set<NSObject>) { NSUnimplemented() }
public required convenience init?(coder: NSCoder) { NSUnimplemented() }
public func countForObject(object: AnyObject) -> Int { NSUnimplemented() }
public override func objectEnumerator() -> NSEnumerator { NSUnimplemented() }
public override func addObject(object: AnyObject) { NSUnimplemented() }
public override func removeObject(object: AnyObject) { NSUnimplemented() }
}
extension Set : Bridgeable {
public func bridge() -> NSSet { return _nsObject }
}
extension NSSet : Bridgeable {
public func bridge() -> Set<NSObject> { return _swiftObject }
}