@@ -315,7 +315,144 @@ public struct Array<Element>: _DestructorSafeContainer {
315
315
internal init ( _buffer: _Buffer ) {
316
316
self . _buffer = _buffer
317
317
}
318
+ }
319
+
320
+ //===--- private helpers---------------------------------------------------===//
321
+ extension Array {
322
+ /// Returns `true` if the array is native and does not need a deferred
323
+ /// type check. May be hoisted by the optimizer, which means its
324
+ /// results may be stale by the time they are used if there is an
325
+ /// inout violation in user code.
326
+ @inlinable
327
+ @_semantics ( " array.props.isNativeTypeChecked " )
328
+ public // @testable
329
+ func _hoistableIsNativeTypeChecked( ) -> Bool {
330
+ return _buffer. arrayPropertyIsNativeTypeChecked
331
+ }
332
+
333
+ @inlinable
334
+ @_semantics ( " array.get_count " )
335
+ internal func _getCount( ) -> Int {
336
+ return _buffer. count
337
+ }
338
+
339
+ @inlinable
340
+ @_semantics ( " array.get_capacity " )
341
+ internal func _getCapacity( ) -> Int {
342
+ return _buffer. capacity
343
+ }
344
+
345
+ @inlinable
346
+ @_semantics ( " array.make_mutable " )
347
+ internal mutating func _makeMutableAndUnique( ) {
348
+ if _slowPath ( !_buffer. isMutableAndUniquelyReferenced ( ) ) {
349
+ _buffer = _Buffer ( copying: _buffer)
350
+ }
351
+ }
318
352
353
+ /// Check that the given `index` is valid for subscripting, i.e.
354
+ /// `0 ≤ index < count`.
355
+ @inlinable
356
+ @inline ( __always)
357
+ internal func _checkSubscript_native( _ index: Int ) {
358
+ _ = _checkSubscript ( index, wasNativeTypeChecked: true )
359
+ }
360
+
361
+ /// Check that the given `index` is valid for subscripting, i.e.
362
+ /// `0 ≤ index < count`.
363
+ @inlinable
364
+ @_semantics ( " array.check_subscript " )
365
+ public // @testable
366
+ func _checkSubscript(
367
+ _ index: Int , wasNativeTypeChecked: Bool
368
+ ) -> _DependenceToken {
369
+ #if _runtime(_ObjC)
370
+ _buffer. _checkInoutAndNativeTypeCheckedBounds (
371
+ index, wasNativeTypeChecked: wasNativeTypeChecked)
372
+ #else
373
+ _buffer. _checkValidSubscript ( index)
374
+ #endif
375
+ return _DependenceToken ( )
376
+ }
377
+
378
+ /// Check that the specified `index` is valid, i.e. `0 ≤ index ≤ count`.
379
+ @inlinable
380
+ @_semantics ( " array.check_index " )
381
+ internal func _checkIndex( _ index: Int ) {
382
+ _precondition ( index <= endIndex, " Array index is out of range " )
383
+ _precondition ( index >= startIndex, " Negative Array index is out of range " )
384
+ }
385
+
386
+ @_semantics ( " array.get_element " )
387
+ @inline ( __always)
388
+ public // @testable
389
+ func _getElement(
390
+ _ index: Int ,
391
+ wasNativeTypeChecked: Bool ,
392
+ matchingSubscriptCheck: _DependenceToken
393
+ ) -> Element {
394
+ #if _runtime(_ObjC)
395
+ return _buffer. getElement ( index, wasNativeTypeChecked: wasNativeTypeChecked)
396
+ #else
397
+ return _buffer. getElement ( index)
398
+ #endif
399
+ }
400
+
401
+ @inlinable
402
+ @_semantics ( " array.get_element_address " )
403
+ internal func _getElementAddress( _ index: Int ) -> UnsafeMutablePointer < Element > {
404
+ return _buffer. subscriptBaseAddress + index
405
+ }
406
+ }
407
+
408
+ extension Array : _ArrayProtocol {
409
+ /// The total number of elements that the array can contain without
410
+ /// allocating new storage.
411
+ ///
412
+ /// Every array reserves a specific amount of memory to hold its contents.
413
+ /// When you add elements to an array and that array begins to exceed its
414
+ /// reserved capacity, the array allocates a larger region of memory and
415
+ /// copies its elements into the new storage. The new storage is a multiple
416
+ /// of the old storage's size. This exponential growth strategy means that
417
+ /// appending an element happens in constant time, averaging the performance
418
+ /// of many append operations. Append operations that trigger reallocation
419
+ /// have a performance cost, but they occur less and less often as the array
420
+ /// grows larger.
421
+ ///
422
+ /// The following example creates an array of integers from an array literal,
423
+ /// then appends the elements of another collection. Before appending, the
424
+ /// array allocates new storage that is large enough store the resulting
425
+ /// elements.
426
+ ///
427
+ /// var numbers = [10, 20, 30, 40, 50]
428
+ /// // numbers.count == 5
429
+ /// // numbers.capacity == 5
430
+ ///
431
+ /// numbers.append(contentsOf: stride(from: 60, through: 100, by: 10))
432
+ /// // numbers.count == 10
433
+ /// // numbers.capacity == 12
434
+ @inlinable
435
+ public var capacity : Int {
436
+ return _getCapacity ( )
437
+ }
438
+
439
+ /// An object that guarantees the lifetime of this array's elements.
440
+ @inlinable
441
+ public // @testable
442
+ var _owner : AnyObject ? {
443
+ @inline ( __always)
444
+ get {
445
+ return _buffer. owner
446
+ }
447
+ }
448
+
449
+ /// If the elements are stored contiguously, a pointer to the first
450
+ /// element. Otherwise, `nil`.
451
+ @inlinable
452
+ public var _baseAddressIfContiguous : UnsafeMutablePointer < Element > ? {
453
+ @inline ( __always) // FIXME(TODO: JIRA): Hack around test failure
454
+ get { return _buffer. firstElementAddressIfContiguous }
455
+ }
319
456
}
320
457
321
458
extension Array : RandomAccessCollection , MutableCollection {
@@ -620,93 +757,11 @@ extension Array: RandomAccessCollection, MutableCollection {
620
757
}
621
758
}
622
759
}
623
- }
624
-
625
- //===--- private helpers---------------------------------------------------===//
626
- extension Array {
627
- /// Returns `true` if the array is native and does not need a deferred
628
- /// type check. May be hoisted by the optimizer, which means its
629
- /// results may be stale by the time they are used if there is an
630
- /// inout violation in user code.
631
- @inlinable
632
- @_semantics ( " array.props.isNativeTypeChecked " )
633
- public // @testable
634
- func _hoistableIsNativeTypeChecked( ) -> Bool {
635
- return _buffer. arrayPropertyIsNativeTypeChecked
636
- }
637
-
638
- @inlinable
639
- @_semantics ( " array.get_count " )
640
- internal func _getCount( ) -> Int {
641
- return _buffer. count
642
- }
643
-
644
- @inlinable
645
- @_semantics ( " array.get_capacity " )
646
- internal func _getCapacity( ) -> Int {
647
- return _buffer. capacity
648
- }
649
-
650
- @inlinable
651
- @_semantics ( " array.make_mutable " )
652
- internal mutating func _makeMutableAndUnique( ) {
653
- if _slowPath ( !_buffer. isMutableAndUniquelyReferenced ( ) ) {
654
- _buffer = _Buffer ( copying: _buffer)
655
- }
656
- }
657
-
658
- /// Check that the given `index` is valid for subscripting, i.e.
659
- /// `0 ≤ index < count`.
660
- @inlinable
661
- @inline ( __always)
662
- internal func _checkSubscript_native( _ index: Int ) {
663
- _ = _checkSubscript ( index, wasNativeTypeChecked: true )
664
- }
665
-
666
- /// Check that the given `index` is valid for subscripting, i.e.
667
- /// `0 ≤ index < count`.
668
- @inlinable
669
- @_semantics ( " array.check_subscript " )
670
- public // @testable
671
- func _checkSubscript(
672
- _ index: Int , wasNativeTypeChecked: Bool
673
- ) -> _DependenceToken {
674
- #if _runtime(_ObjC)
675
- _buffer. _checkInoutAndNativeTypeCheckedBounds (
676
- index, wasNativeTypeChecked: wasNativeTypeChecked)
677
- #else
678
- _buffer. _checkValidSubscript ( index)
679
- #endif
680
- return _DependenceToken ( )
681
- }
682
-
683
- /// Check that the specified `index` is valid, i.e. `0 ≤ index ≤ count`.
684
- @inlinable
685
- @_semantics ( " array.check_index " )
686
- internal func _checkIndex( _ index: Int ) {
687
- _precondition ( index <= endIndex, " Array index is out of range " )
688
- _precondition ( index >= startIndex, " Negative Array index is out of range " )
689
- }
690
-
691
- @_semantics ( " array.get_element " )
692
- @inline ( __always)
693
- public // @testable
694
- func _getElement(
695
- _ index: Int ,
696
- wasNativeTypeChecked: Bool ,
697
- matchingSubscriptCheck: _DependenceToken
698
- ) -> Element {
699
- #if _runtime(_ObjC)
700
- return _buffer. getElement ( index, wasNativeTypeChecked: wasNativeTypeChecked)
701
- #else
702
- return _buffer. getElement ( index)
703
- #endif
704
- }
705
-
760
+
761
+ /// The number of elements in the array.
706
762
@inlinable
707
- @_semantics ( " array.get_element_address " )
708
- internal func _getElementAddress( _ index: Int ) -> UnsafeMutablePointer < Element > {
709
- return _buffer. subscriptBaseAddress + index
763
+ public var count : Int {
764
+ return _getCount ( )
710
765
}
711
766
}
712
767
@@ -731,7 +786,7 @@ extension Array: ExpressibleByArrayLiteral {
731
786
}
732
787
}
733
788
734
- extension Array : RangeReplaceableCollection , ArrayProtocol {
789
+ extension Array : RangeReplaceableCollection {
735
790
/// Creates a new, empty array.
736
791
///
737
792
/// This is equivalent to initializing with an empty array literal.
@@ -785,9 +840,7 @@ extension Array: RangeReplaceableCollection, ArrayProtocol {
785
840
///
786
841
/// - Parameter s: The sequence of elements to turn into an array.
787
842
@inlinable
788
- public init < S: Sequence > ( _ s: S )
789
- where S. Element == Element {
790
-
843
+ public init < S: Sequence > ( _ s: S ) where S. Element == Element {
791
844
self = Array (
792
845
_buffer: _Buffer (
793
846
_buffer: s. _copyToContiguousArray ( ) . _buffer,
@@ -889,60 +942,6 @@ extension Array: RangeReplaceableCollection, ArrayProtocol {
889
942
_buffer. count = 0
890
943
}
891
944
892
- /// The number of elements in the array.
893
- @inlinable
894
- public var count : Int {
895
- return _getCount ( )
896
- }
897
-
898
- /// The total number of elements that the array can contain without
899
- /// allocating new storage.
900
- ///
901
- /// Every array reserves a specific amount of memory to hold its contents.
902
- /// When you add elements to an array and that array begins to exceed its
903
- /// reserved capacity, the array allocates a larger region of memory and
904
- /// copies its elements into the new storage. The new storage is a multiple
905
- /// of the old storage's size. This exponential growth strategy means that
906
- /// appending an element happens in constant time, averaging the performance
907
- /// of many append operations. Append operations that trigger reallocation
908
- /// have a performance cost, but they occur less and less often as the array
909
- /// grows larger.
910
- ///
911
- /// The following example creates an array of integers from an array literal,
912
- /// then appends the elements of another collection. Before appending, the
913
- /// array allocates new storage that is large enough store the resulting
914
- /// elements.
915
- ///
916
- /// var numbers = [10, 20, 30, 40, 50]
917
- /// // numbers.count == 5
918
- /// // numbers.capacity == 5
919
- ///
920
- /// numbers.append(contentsOf: stride(from: 60, through: 100, by: 10))
921
- /// // numbers.count == 10
922
- /// // numbers.capacity == 12
923
- @inlinable
924
- public var capacity : Int {
925
- return _getCapacity ( )
926
- }
927
-
928
- /// An object that guarantees the lifetime of this array's elements.
929
- @inlinable
930
- public // @testable
931
- var _owner : AnyObject ? {
932
- @inline ( __always)
933
- get {
934
- return _buffer. owner
935
- }
936
- }
937
-
938
- /// If the elements are stored contiguously, a pointer to the first
939
- /// element. Otherwise, `nil`.
940
- @inlinable
941
- public var _baseAddressIfContiguous : UnsafeMutablePointer < Element > ? {
942
- @inline ( __always) // FIXME(TODO: JIRA): Hack around test failure
943
- get { return _buffer. firstElementAddressIfContiguous }
944
- }
945
-
946
945
//===--- basic mutations ------------------------------------------------===//
947
946
948
947
0 commit comments