@@ -41,7 +41,7 @@ open class NSOrderedSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
41
41
guard aCoder. allowsKeyedCoding else {
42
42
preconditionFailure ( " Unkeyed coding is unsupported. " )
43
43
}
44
- for idx in 0 ..< self . count {
44
+ for idx in _indices {
45
45
aCoder. encode ( __SwiftValue. store ( self . object ( at: idx) ) , forKey: " NS.object. \( idx) " )
46
46
}
47
47
}
@@ -67,6 +67,7 @@ open class NSOrderedSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
67
67
}
68
68
69
69
open func object( at idx: Int ) -> Any {
70
+ _validateSubscript ( idx)
70
71
return __SwiftValue. fetch ( nonOptional: _orderedStorage [ idx] )
71
72
}
72
73
@@ -120,11 +121,21 @@ open class NSOrderedSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
120
121
if type ( of: self ) === NSOrderedSet . self || type ( of: self ) === NSMutableOrderedSet . self {
121
122
return _orderedStorage. map { __SwiftValue. fetch ( nonOptional: $0) }
122
123
} else {
123
- return ( 0 ..< count ) . map { idx in
124
+ return _indices . map { idx in
124
125
return self [ idx]
125
126
}
126
127
}
127
128
}
129
+
130
+ /// The range of indices that are valid for subscripting the ordered set.
131
+ internal var _indices : Range < Int > {
132
+ return 0 ..< count
133
+ }
134
+
135
+ /// Checks that an index is valid for subscripting: 0 ≤ `index` < `count`.
136
+ internal func _validateSubscript( _ index: Int , file: StaticString = #file, line: UInt = #line) {
137
+ precondition ( _indices. contains ( index) , " \( self ) : Index out of bounds " , file: file, line: line)
138
+ }
128
139
}
129
140
130
141
extension NSOrderedSet : Sequence {
@@ -148,9 +159,6 @@ extension NSOrderedSet {
148
159
open func objects( at indexes: IndexSet ) -> [ Any ] {
149
160
var entries = [ Any] ( )
150
161
for idx in indexes {
151
- guard idx < count && idx >= 0 else {
152
- fatalError ( " \( self ) : Index out of bounds " )
153
- }
154
162
entries. append ( object ( at: idx) )
155
163
}
156
164
return entries
@@ -177,7 +185,7 @@ extension NSOrderedSet {
177
185
return false
178
186
}
179
187
180
- for idx in 0 ..< count {
188
+ for idx in _indices {
181
189
if let value1 = object ( at: idx) as? AnyHashable ,
182
190
let value2 = other. object ( at: idx) as? AnyHashable {
183
191
if value1 != value2 {
@@ -338,9 +346,7 @@ extension NSOrderedSet {
338
346
open class NSMutableOrderedSet : NSOrderedSet {
339
347
340
348
open func insert( _ object: Any , at idx: Int ) {
341
- guard idx <= count && idx >= 0 else {
342
- fatalError ( " \( self ) : Index out of bounds " )
343
- }
349
+ precondition ( idx <= count && idx >= 0 , " \( self ) : Index out of bounds " )
344
350
345
351
let value = __SwiftValue. store ( object)
346
352
@@ -353,15 +359,12 @@ open class NSMutableOrderedSet : NSOrderedSet {
353
359
}
354
360
355
361
open func removeObject( at idx: Int ) {
362
+ _validateSubscript ( idx)
356
363
_storage. remove ( _orderedStorage [ idx] )
357
364
_orderedStorage. remove ( at: idx)
358
365
}
359
366
360
367
open func replaceObject( at idx: Int , with obj: Any ) {
361
- guard idx < count && idx >= 0 else {
362
- fatalError ( " \( self ) : Index out of bounds " )
363
- }
364
-
365
368
let value = __SwiftValue. store ( obj)
366
369
let objectToReplace = __SwiftValue. store ( object ( at: idx) )
367
370
_orderedStorage [ idx] = value
@@ -420,10 +423,6 @@ extension NSMutableOrderedSet {
420
423
}
421
424
422
425
open func exchangeObject( at idx1: Int , withObjectAt idx2: Int ) {
423
- guard idx1 < count && idx1 >= 0 && idx2 < count && idx2 >= 0 else {
424
- fatalError ( " \( self ) : Index out of bounds " )
425
- }
426
-
427
426
let object1 = self . object ( at: idx1)
428
427
let object2 = self . object ( at: idx2)
429
428
_orderedStorage [ idx1] = __SwiftValue. store ( object2)
@@ -450,13 +449,18 @@ extension NSMutableOrderedSet {
450
449
}
451
450
}
452
451
452
+ /// Sets the object at the specified index of the mutable ordered set.
453
+ ///
454
+ /// - Parameters:
455
+ /// - obj: The object to be set.
456
+ /// - idx: The index. If the index is equal to `count`, then it appends
457
+ /// the object. Otherwise it replaces the object at the index with the
458
+ /// given object.
453
459
open func setObject( _ obj: Any , at idx: Int ) {
454
- let object = __SwiftValue. store ( obj)
455
- _storage. insert ( object)
456
- if idx == _orderedStorage. count {
457
- _orderedStorage. append ( object)
460
+ if idx == count {
461
+ insert ( obj, at: idx)
458
462
} else {
459
- _orderedStorage [ idx] = object
463
+ replaceObject ( at : idx, with : obj )
460
464
}
461
465
}
462
466
0 commit comments