-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathNSArray.swift
871 lines (744 loc) · 29.9 KB
/
NSArray.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 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
open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCoding {
private let _cfinfo = _CFInfo(typeID: CFArrayGetTypeID())
internal var _storage = [AnyObject]()
open var count: Int {
guard type(of: self) === NSArray.self || type(of: self) === NSMutableArray.self else {
NSRequiresConcreteImplementation()
}
return _storage.count
}
open func object(at index: Int) -> Any {
guard type(of: self) === NSArray.self || type(of: self) === NSMutableArray.self else {
NSRequiresConcreteImplementation()
}
return _SwiftValue.fetch(_storage[index])
}
public convenience override init() {
self.init(objects: [], count:0)
}
public required init(objects: UnsafePointer<AnyObject>!, count cnt: Int) {
_storage.reserveCapacity(cnt)
for idx in 0..<cnt {
_storage.append(objects[idx])
}
}
required public convenience init(arrayLiteral elements: Any...) {
self.init(array: elements)
}
public required convenience init?(coder aDecoder: NSCoder) {
if !aDecoder.allowsKeyedCoding {
var cnt: UInt32 = 0
// We're stuck with (int) here (rather than unsigned int)
// because that's the way the code was originally written, unless
// we go to a new version of the class, which has its own problems.
withUnsafeMutablePointer(to: &cnt) { (ptr: UnsafeMutablePointer<UInt32>) -> Void in
aDecoder.decodeValue(ofObjCType: "i", at: UnsafeMutableRawPointer(ptr))
}
let objects = UnsafeMutablePointer<AnyObject>.allocate(capacity: Int(cnt))
for idx in 0..<cnt {
// If conversion to NSObject fails then we really can't hold it anyway
objects.advanced(by: Int(idx)).initialize(to: aDecoder.decodeObject() as! NSObject)
}
self.init(objects: UnsafePointer<AnyObject>(objects), count: Int(cnt))
objects.deinitialize(count: Int(cnt))
objects.deallocate(capacity: Int(cnt))
} else if type(of: aDecoder) == NSKeyedUnarchiver.self || aDecoder.containsValue(forKey: "NS.objects") {
let objects = aDecoder._decodeArrayOfObjectsForKey("NS.objects")
self.init(array: objects as! [NSObject])
} else {
var objects = [AnyObject]()
var count = 0
while let object = aDecoder.decodeObject(forKey: "NS.object.\(count)") {
objects.append(object as! NSObject)
count += 1
}
self.init(array: objects)
}
}
open func encode(with aCoder: NSCoder) {
if let keyedArchiver = aCoder as? NSKeyedArchiver {
keyedArchiver._encodeArrayOfObjects(self, forKey:"NS.objects")
} else {
for object in self {
if let codable = object as? NSCoding {
codable.encode(with: aCoder)
}
}
}
}
public static var supportsSecureCoding: Bool {
return true
}
open override func copy() -> Any {
return copy(with: nil)
}
open func copy(with zone: NSZone? = nil) -> Any {
if type(of: self) === NSArray.self {
// return self for immutable type
return self
} else if type(of: self) === NSMutableArray.self {
let array = NSArray()
array._storage = self._storage
return array
}
return NSArray(array: self.allObjects)
}
open override func mutableCopy() -> Any {
return mutableCopy(with: nil)
}
open func mutableCopy(with zone: NSZone? = nil) -> Any {
if type(of: self) === NSArray.self || type(of: self) === NSMutableArray.self {
// always create and return an NSMutableArray
let mutableArray = NSMutableArray()
mutableArray._storage = self._storage
return mutableArray
}
return NSMutableArray(array: self.allObjects)
}
public convenience init(object anObject: Any) {
self.init(array: [anObject])
}
public convenience init(array: [Any]) {
self.init(array: array, copyItems: false)
}
public convenience init(array: [Any], copyItems: Bool) {
let optionalArray : [AnyObject] =
copyItems ?
array.map { return _SwiftValue.store($0).copy() as! NSObject } :
array.map { return _SwiftValue.store($0) }
// This would have been nice, but "initializer delegation cannot be nested in another expression"
// optionalArray.withUnsafeBufferPointer { ptr in
// self.init(objects: ptr.baseAddress, count: array.count)
// }
let cnt = array.count
let buffer = UnsafeMutablePointer<AnyObject>.allocate(capacity: cnt)
buffer.initialize(from: optionalArray)
self.init(objects: buffer, count: cnt)
buffer.deinitialize(count: cnt)
buffer.deallocate(capacity: cnt)
}
open override func isEqual(_ value: Any?) -> Bool {
if let other = value as? [Any] {
return self.isEqual(to: other)
} else if let other = value as? NSArray {
return self.isEqual(to: other.allObjects)
}
return false
}
open override var hash: Int {
return self.count
}
internal var allObjects: [Any] {
if type(of: self) === NSArray.self || type(of: self) === NSMutableArray.self {
return _storage.map { _SwiftValue.fetch($0) }
} else {
return (0..<count).map { idx in
return self[idx]
}
}
}
open func adding(_ anObject: Any) -> [Any] {
return allObjects + [anObject]
}
open func addingObjects(from otherArray: [Any]) -> [Any] {
return allObjects + otherArray
}
open func componentsJoined(by separator: String) -> String {
// make certain to call NSObject's description rather than asking the string interpolator for the swift description
return allObjects.map { "\($0)" }.joined(separator: separator)
}
open func contains(_ anObject: Any) -> Bool {
guard let other = anObject as? AnyHashable else {
return false
}
for idx in 0..<count {
if let obj = self[idx] as? AnyHashable {
if obj == other {
return true
}
}
}
return false
}
open func description(withLocale locale: Locale?) -> String { return description(withLocale: locale, indent: 0) }
open func description(withLocale locale: Locale?, indent level: Int) -> String {
var descriptions = [String]()
let cnt = count
for idx in 0..<cnt {
let obj = self[idx]
if let string = obj as? String {
descriptions.append(string)
} else if let array = obj as? [Any] {
descriptions.append(NSArray(array: array).description(withLocale: locale, indent: level + 1))
} else if let dict = obj as? [AnyHashable : Any] {
descriptions.append(dict._bridgeToObjectiveC().description(withLocale: locale, indent: level + 1))
} else {
descriptions.append("\(obj)")
}
}
var indent = ""
for _ in 0..<level {
indent += " "
}
var result = indent + "(\n"
for idx in 0..<cnt {
result += indent + " " + descriptions[idx]
if idx + 1 < cnt {
result += ",\n"
} else {
result += "\n"
}
}
result += indent + ")"
return result
}
open func firstObjectCommon(with otherArray: [Any]) -> Any? {
let set = otherArray.map { _SwiftValue.store($0) }
for idx in 0..<count {
let item = _SwiftValue.store(self[idx])
if set.contains(item) {
return _SwiftValue.fetch(item)
}
}
return nil
}
internal func getObjects(_ objects: inout [Any], range: NSRange) {
objects.reserveCapacity(objects.count + range.length)
if type(of: self) === NSArray.self || type(of: self) === NSMutableArray.self {
objects += _storage[range.toRange()!].map { _SwiftValue.fetch($0) }
return
}
objects += range.toCountableRange()!.map { self[$0] }
}
open func index(of anObject: Any) -> Int {
guard let val = anObject as? AnyHashable else {
return NSNotFound
}
for idx in 0..<count {
if let obj = object(at: idx) as? AnyHashable {
if val == obj {
return idx
}
}
}
return NSNotFound
}
open func index(of anObject: Any, in range: NSRange) -> Int {
guard let val = anObject as? AnyHashable else {
return NSNotFound
}
for idx in 0..<range.length {
if let obj = object(at: idx + range.location) as? AnyHashable {
if val == obj {
return idx
}
}
}
return NSNotFound
}
open func indexOfObjectIdentical(to anObject: Any) -> Int {
guard let val = anObject as? NSObject else {
return NSNotFound
}
for idx in 0..<count {
if let obj = object(at: idx) as? NSObject {
if val === obj {
return idx
}
}
}
return NSNotFound
}
open func indexOfObjectIdentical(to anObject: Any, in range: NSRange) -> Int {
guard let val = anObject as? NSObject else {
return NSNotFound
}
for idx in 0..<range.length {
if let obj = object(at: idx + range.location) as? NSObject {
if val === obj {
return idx
}
}
}
return NSNotFound
}
open func isEqual(to otherArray: [Any]) -> Bool {
if count != otherArray.count {
return false
}
for idx in 0..<count {
if let val1 = object(at: idx) as? AnyHashable,
let val2 = otherArray[idx] as? AnyHashable {
if val1 != val2 {
return false
}
}
}
return true
}
open var firstObject: Any? {
if count > 0 {
return object(at: 0)
} else {
return nil
}
}
open var lastObject: Any? {
if count > 0 {
return object(at: count - 1)
} else {
return nil
}
}
public struct Iterator : IteratorProtocol {
// TODO: Detect mutations
// TODO: Use IndexingGenerator instead?
let array : NSArray
let sentinel : Int
let reverse : Bool
var idx : Int
public mutating func next() -> Any? {
guard idx != sentinel else {
return nil
}
let result = array.object(at: reverse ? idx - 1 : idx)
idx += reverse ? -1 : 1
return result
}
init(_ array : NSArray, reverse : Bool = false) {
self.array = array
self.sentinel = reverse ? 0 : array.count
self.idx = reverse ? array.count : 0
self.reverse = reverse
}
}
open func objectEnumerator() -> NSEnumerator {
return NSGeneratorEnumerator(Iterator(self))
}
open func reverseObjectEnumerator() -> NSEnumerator {
return NSGeneratorEnumerator(Iterator(self, reverse: true))
}
open var sortedArrayHint: Data {
let size = count
let buffer = UnsafeMutablePointer<Int32>.allocate(capacity: size)
for idx in 0..<count {
let item = object(at: idx) as! NSObject
let hash = item.hash
buffer.advanced(by: idx).pointee = Int32(hash).littleEndian
}
return Data(bytesNoCopy: unsafeBitCast(buffer, to: UnsafeMutablePointer<UInt8>.self), count: count * MemoryLayout<Int>.size, deallocator: .custom({ _ in
buffer.deallocate(capacity: size)
buffer.deinitialize(count: size)
}))
}
open func sortedArray(_ comparator: (Any, Any, UnsafeMutableRawPointer?) -> Int, context: UnsafeMutableRawPointer?) -> [Any] {
return sortedArray(options: []) { lhs, rhs in
return ComparisonResult(rawValue: comparator(lhs, rhs, context))!
}
}
open func sortedArray(_ comparator: (Any, Any, UnsafeMutableRawPointer?) -> Int, context: UnsafeMutableRawPointer?, hint: Data?) -> [Any] {
return sortedArray(options: []) { lhs, rhs in
return ComparisonResult(rawValue: comparator(lhs, rhs, context))!
}
}
open func subarray(with range: NSRange) -> [Any] {
if range.length == 0 {
return []
}
var objects = [Any]()
getObjects(&objects, range: range)
return objects
}
open func write(toFile path: String, atomically useAuxiliaryFile: Bool) -> Bool { NSUnimplemented() }
open func write(to url: URL, atomically: Bool) -> Bool { NSUnimplemented() }
open func objects(at indexes: IndexSet) -> [Any] {
var objs = [Any]()
indexes.rangeView.forEach {
objs.append(contentsOf: self.subarray(with: NSRange(location: $0.lowerBound, length: $0.upperBound - $0.lowerBound)))
}
return objs
}
open subscript (idx: Int) -> Any {
guard idx < count && idx >= 0 else {
fatalError("\(self): Index out of bounds")
}
return object(at: idx)
}
open func enumerateObjects(_ block: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
self.enumerateObjects(options: [], using: block)
}
open func enumerateObjects(options opts: NSEnumerationOptions = [], using block: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
self.enumerateObjects(at: IndexSet(integersIn: 0..<count), options: opts, using: block)
}
open func enumerateObjects(at s: IndexSet, options opts: NSEnumerationOptions = [], using block: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
guard !opts.contains(.concurrent) else {
NSUnimplemented()
}
s._bridgeToObjectiveC().enumerate(options: opts) { (idx, stop) in
block(self.object(at: idx), idx, stop)
}
}
open func indexOfObject(passingTest predicate: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Int {
return indexOfObject([], passingTest: predicate)
}
open func indexOfObject(_ opts: NSEnumerationOptions = [], passingTest predicate: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Int {
return indexOfObject(at: IndexSet(integersIn: 0..<count), options: opts, passingTest: predicate)
}
open func indexOfObject(at s: IndexSet, options opts: NSEnumerationOptions = [], passingTest predicate: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Int {
var result = NSNotFound
enumerateObjects(at: s, options: opts) { (obj, idx, stop) -> Void in
if predicate(obj, idx, stop) {
result = idx
stop.pointee = true
}
}
return result
}
open func indexesOfObjects(passingTest predicate: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> IndexSet {
return indexesOfObjects(options: [], passingTest: predicate)
}
open func indexesOfObjects(options opts: NSEnumerationOptions = [], passingTest predicate: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> IndexSet {
return indexesOfObjects(at: IndexSet(integersIn: 0..<count), options: opts, passingTest: predicate)
}
open func indexesOfObjects(at s: IndexSet, options opts: NSEnumerationOptions = [], passingTest predicate: (Any, Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> IndexSet {
var result = IndexSet()
enumerateObjects(at: s, options: opts) { (obj, idx, stop) in
if predicate(obj, idx, stop) {
result.insert(idx)
}
}
return result
}
internal func sortedArray(from range: NSRange, options: NSSortOptions, usingComparator cmptr: (Any, Any) -> ComparisonResult) -> [Any] {
// The sort options are not available. We use the Array's sorting algorithm. It is not stable neither concurrent.
guard options.isEmpty else {
NSUnimplemented()
}
let count = self.count
if range.length == 0 || count == 0 {
return []
}
let swiftRange = range.toRange()!
return allObjects[swiftRange].sorted { lhs, rhs in
return cmptr(lhs, rhs) == .orderedAscending
}
}
open func sortedArray(comparator cmptr: (Any, Any) -> ComparisonResult) -> [Any] {
return sortedArray(from: NSMakeRange(0, count), options: [], usingComparator: cmptr)
}
open func sortedArray(options opts: NSSortOptions = [], usingComparator cmptr: (Any, Any) -> ComparisonResult) -> [Any] {
return sortedArray(from: NSMakeRange(0, count), options: opts, usingComparator: cmptr)
}
open func index(of obj: Any, inSortedRange r: NSRange, options opts: NSBinarySearchingOptions = [], usingComparator cmp: (Any, Any) -> ComparisonResult) -> Int {
let lastIndex = r.location + r.length - 1
// argument validation
guard lastIndex < count else {
let bounds = count == 0 ? "for empty array" : "[0 .. \(count - 1)]"
NSInvalidArgument("range \(r) extends beyond bounds \(bounds)")
}
if opts.contains(.firstEqual) && opts.contains(.lastEqual) {
NSInvalidArgument("both NSBinarySearching.firstEqual and NSBinarySearching.lastEqual options cannot be specified")
}
let searchForInsertionIndex = opts.contains(.insertionIndex)
// fringe cases
if r.length == 0 {
return searchForInsertionIndex ? r.location : NSNotFound
}
let leastObj = object(at: r.location)
if cmp(obj, leastObj) == .orderedAscending {
return searchForInsertionIndex ? r.location : NSNotFound
}
let greatestObj = object(at: lastIndex)
if cmp(obj, greatestObj) == .orderedDescending {
return searchForInsertionIndex ? lastIndex + 1 : NSNotFound
}
// common processing
let firstEqual = opts.contains(.firstEqual)
let lastEqual = opts.contains(.lastEqual)
let anyEqual = !(firstEqual || lastEqual)
var result = NSNotFound
var indexOfLeastGreaterThanObj = NSNotFound
var start = r.location
var end = lastIndex
loop: while start <= end {
let middle = start + (end - start) / 2
let item = object(at: middle)
switch cmp(item, obj) {
case .orderedSame where anyEqual:
result = middle
break loop
case .orderedSame where lastEqual:
result = middle
fallthrough
case .orderedAscending:
start = middle + 1
case .orderedSame where firstEqual:
result = middle
fallthrough
case .orderedDescending:
indexOfLeastGreaterThanObj = middle
end = middle - 1
default:
fatalError("Implementation error.")
}
}
if !searchForInsertionIndex {
return result
}
if result == NSNotFound {
return indexOfLeastGreaterThanObj
}
return lastEqual ? result + 1 : result
}
public convenience init?(contentsOfFile path: String) { NSUnimplemented() }
public convenience init?(contentsOfURL url: URL) { NSUnimplemented() }
override open var _cfTypeID: CFTypeID {
return CFArrayGetTypeID()
}
}
extension NSArray : _CFBridgeable, _SwiftBridgeable {
internal var _cfObject: CFArray { return unsafeBitCast(self, to: CFArray.self) }
internal var _swiftObject: [AnyObject] { return Array._unconditionallyBridgeFromObjectiveC(self) }
}
extension NSMutableArray {
internal var _cfMutableObject: CFMutableArray { return unsafeBitCast(self, to: CFMutableArray.self) }
}
extension CFArray : _NSBridgeable, _SwiftBridgeable {
internal var _nsObject: NSArray { return unsafeBitCast(self, to: NSArray.self) }
internal var _swiftObject: Array<Any> { return _nsObject._swiftObject }
}
extension CFArray {
/// Bridge something returned from CF to an Array<T>. Useful when we already know that a CFArray contains objects that are toll-free bridged with Swift objects, e.g. CFArray<CFURLRef>.
/// - Note: This bridging operation is unfortunately still O(n), but it only traverses the NSArray once, creating the Swift array and casting at the same time.
func _unsafeTypedBridge<T : _CFBridgeable>() -> Array<T> {
var result = Array<T>()
let count = CFArrayGetCount(self)
result.reserveCapacity(count)
for i in 0..<count {
result.append(unsafeBitCast(CFArrayGetValueAtIndex(self, i), to: T.self))
}
return result
}
}
extension Array : _NSBridgeable, _CFBridgeable {
internal var _nsObject: NSArray { return _bridgeToObjectiveC() }
internal var _cfObject: CFArray { return _nsObject._cfObject }
}
public struct NSBinarySearchingOptions : OptionSet {
public let rawValue : UInt
public init(rawValue: UInt) { self.rawValue = rawValue }
public static let firstEqual = NSBinarySearchingOptions(rawValue: 1 << 8)
public static let lastEqual = NSBinarySearchingOptions(rawValue: 1 << 9)
public static let insertionIndex = NSBinarySearchingOptions(rawValue: 1 << 10)
}
open class NSMutableArray : NSArray {
open func add(_ anObject: Any) {
insert(anObject, at: count)
}
open func insert(_ anObject: Any, at index: Int) {
guard type(of: self) === NSMutableArray.self else {
NSRequiresConcreteImplementation()
}
_storage.insert(_SwiftValue.store(anObject), at: index)
}
open func removeLastObject() {
if count > 0 {
removeObject(at: count - 1)
}
}
open func removeObject(at index: Int) {
guard type(of: self) === NSMutableArray.self else {
NSRequiresConcreteImplementation()
}
_storage.remove(at: index)
}
open func replaceObject(at index: Int, with anObject: Any) {
guard type(of: self) === NSMutableArray.self else {
NSRequiresConcreteImplementation()
}
let min = index
let max = index + 1
_storage.replaceSubrange(min..<max, with: [_SwiftValue.store(anObject) as AnyObject])
}
public convenience init() {
self.init(capacity: 0)
}
public init(capacity numItems: Int) {
super.init(objects: [], count: 0)
if type(of: self) === NSMutableArray.self {
_storage.reserveCapacity(numItems)
}
}
public required convenience init(objects: UnsafePointer<AnyObject>!, count cnt: Int) {
self.init(capacity: cnt)
for idx in 0..<cnt {
_storage.append(objects[idx])
}
}
open override subscript (idx: Int) -> Any {
get {
return object(at: idx)
}
set(newObject) {
self.replaceObject(at: idx, with: newObject)
}
}
open func addObjectsFromArray(_ otherArray: [Any]) {
if type(of: self) === NSMutableArray.self {
_storage += otherArray.map { _SwiftValue.store($0) as AnyObject }
} else {
for obj in otherArray {
add(obj)
}
}
}
open func exchangeObject(at idx1: Int, withObjectAt idx2: Int) {
if type(of: self) === NSMutableArray.self {
swap(&_storage[idx1], &_storage[idx2])
} else {
NSUnimplemented()
}
}
open func removeAllObjects() {
if type(of: self) === NSMutableArray.self {
_storage.removeAll()
} else {
while count > 0 {
removeObject(at: 0)
}
}
}
open func remove(_ anObject: Any, inRange range: NSRange) {
let idx = index(of: anObject, in: range)
if idx != NSNotFound {
removeObject(at: idx)
}
}
open func remove(_ anObject: Any) {
let idx = index(of: anObject)
if idx != NSNotFound {
removeObject(at: idx)
}
}
open func removeObject(identicalTo anObject: Any, inRange range: NSRange) {
let idx = indexOfObjectIdentical(to: anObject, in: range)
if idx != NSNotFound {
removeObject(at: idx)
}
}
open func removeObject(identicalTo anObject: Any) {
let idx = indexOfObjectIdentical(to: anObject)
if idx != NSNotFound {
removeObject(at: idx)
}
}
open func removeObjects(in otherArray: [Any]) {
let set = Set(otherArray.map { $0 as! AnyHashable } )
for idx in (0..<count).reversed() {
if let value = object(at: idx) as? AnyHashable {
if set.contains(value) {
removeObject(at: idx)
}
}
}
}
open func removeObjects(in range: NSRange) {
if type(of: self) === NSMutableArray.self {
_storage.removeSubrange(range.toRange()!)
} else {
for idx in range.toCountableRange()!.reversed() {
removeObject(at: idx)
}
}
}
open func replaceObjects(in range: NSRange, withObjectsFrom otherArray: [Any], range otherRange: NSRange) {
var list = [Any]()
otherArray._bridgeToObjectiveC().getObjects(&list, range:otherRange)
replaceObjects(in: range, withObjectsFromArray:list)
}
open func replaceObjects(in range: NSRange, withObjectsFromArray otherArray: [Any]) {
if type(of: self) === NSMutableArray.self {
_storage.reserveCapacity(count - range.length + otherArray.count)
for idx in 0..<range.length {
_storage[idx + range.location] = _SwiftValue.store(otherArray[idx])
}
for idx in range.length..<otherArray.count {
_storage.insert(_SwiftValue.store(otherArray[idx]), at: idx + range.location)
}
} else {
NSUnimplemented()
}
}
open func setArray(_ otherArray: [Any]) {
if type(of: self) === NSMutableArray.self {
_storage = otherArray.map { _SwiftValue.store($0) }
} else {
replaceObjects(in: NSMakeRange(0, count), withObjectsFromArray: otherArray)
}
}
open func insertObjects(_ objects: [Any], atIndexes indexes: IndexSet) {
precondition(objects.count == indexes.count)
if type(of: self) === NSMutableArray.self {
_storage.reserveCapacity(count + indexes.count)
}
var objectIdx = 0
for insertionIndex in indexes {
self.insert(objects[objectIdx], at: insertionIndex)
objectIdx += 1
}
}
open func removeObjects(at indexes: IndexSet) {
for range in indexes.rangeView.reversed() {
self.removeObjects(in: NSMakeRange(range.lowerBound, range.upperBound - range.lowerBound))
}
}
open func replaceObjects(at indexes: IndexSet, withObjects objects: [Any]) {
var objectIndex = 0
for countedRange in indexes.rangeView {
let range = NSMakeRange(countedRange.lowerBound, countedRange.upperBound - countedRange.lowerBound)
let subObjects = objects[objectIndex..<objectIndex + range.length]
self.replaceObjects(in: range, withObjectsFromArray: Array(subObjects))
objectIndex += range.length
}
}
open func sort(_ compare: (Any, Any, UnsafeMutableRawPointer?) -> Int, context: UnsafeMutableRawPointer?) {
self.setArray(self.sortedArray(compare, context: context))
}
open func sort(_ cmptr: Comparator) {
self.sort(options: [], usingComparator: cmptr)
}
open func sort(options opts: NSSortOptions, usingComparator cmptr: Comparator) {
self.setArray(self.sortedArray(options: opts, usingComparator: cmptr))
}
public convenience init?(contentsOfFile path: String) { NSUnimplemented() }
public convenience init?(contentsOfURL url: URL) { NSUnimplemented() }
}
extension NSArray : Sequence {
final public func makeIterator() -> Iterator {
return Iterator(self)
}
}
extension NSArray : ExpressibleByArrayLiteral {
/// Create an instance initialized with `elements`.
// required public convenience init(arrayLiteral elements: Any...) {
//
// }
}
extension NSArray : CustomReflectable {
public var customMirror: Mirror { NSUnimplemented() }
}
extension NSArray : _StructTypeBridgeable {
public typealias _StructType = Array<Any>
public func _bridgeToSwift() -> _StructType {
return _StructType._unconditionallyBridgeFromObjectiveC(self)
}
}