@@ -503,8 +503,8 @@ extension _Bitset: Sequence {
503
503
return wordIndex * Word. capacity + v
504
504
}
505
505
guard let storage = self . storage else { return nil }
506
- while wordIndex < storage. _wordCount {
507
- word = storage. _words [ wordIndex]
506
+ while wordIndex < storage. wordCount {
507
+ word = storage [ word : wordIndex]
508
508
// Note that _wordIndex is offset by 1 due to word0;
509
509
// this is why the index needs to be incremented at exactly this point.
510
510
wordIndex += 1
@@ -520,18 +520,23 @@ extension _Bitset: Sequence {
520
520
////////////////////////////////////////////////////////////////////////////////
521
521
522
522
extension _Bitset {
523
- /// A simple bitmap storage class with room for a specific number of
524
- /// tail-allocated bits.
525
523
@_fixed_layout
526
524
@usableFromInline
527
- internal final class Storage {
525
+ internal struct StorageHeader {
528
526
@usableFromInline
529
- internal fileprivate ( set ) var _wordCount : Int
527
+ internal var wordCount : Int
530
528
531
- internal init ( _doNotCall: ( ) ) {
532
- _internalInvariantFailure ( " This class cannot be directly initialized " )
529
+ @inlinable
530
+ internal init ( wordCount: Int ) {
531
+ self . wordCount = wordCount
533
532
}
534
533
}
534
+
535
+ /// A simple bitmap storage class with room for a specific number of
536
+ /// tail-allocated bits.
537
+ @_fixed_layout
538
+ @usableFromInline
539
+ internal final class Storage : ManagedBuffer < StorageHeader , Word > { }
535
540
}
536
541
537
542
extension _Bitset . Storage {
@@ -543,44 +548,63 @@ extension _Bitset.Storage {
543
548
internal static func allocateUninitialized(
544
549
wordCount: Int
545
550
) -> _Bitset . Storage {
546
- let storage = Builtin . allocWithTailElems_1 (
547
- _Bitset. Storage. self,
548
- wordCount. _builtinWordValue, Word . self)
549
- storage. _wordCount = wordCount
550
- return storage
551
+ let storage = create (
552
+ minimumCapacity: wordCount,
553
+ makingHeaderWith: { storage in
554
+ _Bitset. StorageHeader ( wordCount: wordCount)
555
+ } )
556
+ return unsafeDowncast ( storage, to: _Bitset. Storage. self)
557
+ }
558
+
559
+ @inlinable
560
+ @inline ( __always)
561
+ internal var wordCount : Int {
562
+ return header. wordCount
551
563
}
552
564
553
565
@inlinable
554
566
internal func clear( ) {
555
- _words . assign ( repeating: . empty, count: _wordCount )
567
+ firstElementAddress . assign ( repeating: . empty, count: wordCount )
556
568
}
557
569
558
570
@inlinable
559
571
internal func copy( ) -> _Bitset . Storage {
560
- let storage = _Bitset. Storage. allocateUninitialized ( wordCount: _wordCount )
572
+ let storage = _Bitset. Storage. allocateUninitialized ( wordCount: wordCount )
561
573
storage. copy ( contentsOf: self . bitset)
562
574
return storage
563
575
}
564
576
565
577
@inlinable
566
578
func copy( contentsOf bitset: _UnsafeBitset ) {
567
- _internalInvariant ( bitset. wordCount == self . _wordCount )
568
- self . _words . assign ( from: bitset. words, count: bitset. wordCount)
579
+ _internalInvariant ( bitset. wordCount == self . wordCount )
580
+ firstElementAddress . assign ( from: bitset. words, count: bitset. wordCount)
569
581
}
570
582
571
583
@inlinable
572
- internal var _words : UnsafeMutablePointer < Word > {
584
+ internal subscript ( word index : Int ) -> Word {
573
585
@inline ( __always)
574
586
get {
575
- let addr = Builtin . projectTailElems ( self , Word . self)
576
- return UnsafeMutablePointer ( addr)
587
+ _internalInvariant ( index >= 0 && index < wordCount)
588
+ return firstElementAddress [ index]
589
+ }
590
+ @inline ( __always)
591
+ set {
592
+ _internalInvariant ( index >= 0 && index < wordCount)
593
+ firstElementAddress [ index] = newValue
594
+ }
595
+ @inline ( __always)
596
+ _modify {
597
+ _internalInvariant ( index >= 0 && index < wordCount)
598
+ yield & firstElementAddress[ index]
577
599
}
578
600
}
579
601
580
602
@inlinable
581
603
internal var bitset : _UnsafeBitset {
582
604
@inline ( __always) get {
583
- return _UnsafeBitset ( words: _words, wordCount: _wordCount)
605
+ return _UnsafeBitset (
606
+ words: firstElementAddress,
607
+ wordCount: wordCount)
584
608
}
585
609
}
586
610
}
0 commit comments