@@ -2455,64 +2455,6 @@ collections = [
2455
2455
]
2456
2456
}%
2457
2457
2458
- /// A wrapper around a bitmap storage with room for at least `bitCount` bits.
2459
- internal struct _BitMap {
2460
- internal let values: UnsafeMutablePointer<UInt>
2461
- internal let bitCount: Int
2462
-
2463
- // Note: We use UInt here to get unsigned math (shifts).
2464
- internal static func wordIndex(_ i: UInt) -> UInt {
2465
- return i / UInt._sizeInBits
2466
- }
2467
-
2468
- internal static func bitIndex(_ i: UInt) -> UInt {
2469
- return i % UInt._sizeInBits
2470
- }
2471
-
2472
- internal static func wordsFor(_ bitCount: Int) -> Int {
2473
- return bitCount + Int._sizeInBytes - 1 / Int._sizeInBytes
2474
- }
2475
-
2476
- internal init(storage: UnsafeMutablePointer<UInt>, bitCount: Int) {
2477
- self.bitCount = bitCount
2478
- self.values = storage
2479
- }
2480
-
2481
- internal var numberOfWords: Int {
2482
- get {
2483
- return _BitMap.wordsFor(bitCount)
2484
- }
2485
- }
2486
-
2487
- internal func initializeToZero() {
2488
- for i in 0 ..< numberOfWords {
2489
- (values + i).initialize(with: 0)
2490
- }
2491
- }
2492
-
2493
- internal subscript(i: Int) -> Bool {
2494
- get {
2495
- _sanityCheck(i < Int(bitCount) && i >= 0, "index out of bounds")
2496
- let idx = UInt(i)
2497
- let word = values[Int(_BitMap.wordIndex(idx))]
2498
- let bit = word & (1 << _BitMap.bitIndex(idx))
2499
- return bit != 0
2500
- }
2501
- nonmutating set {
2502
- _sanityCheck(i < Int(bitCount) && i >= 0, "index out of bounds")
2503
- let idx = UInt(i)
2504
- let wordIdx = _BitMap.wordIndex(idx)
2505
- if newValue {
2506
- values[Int(wordIdx)] =
2507
- values[Int(wordIdx)] | (1 << _BitMap.bitIndex(idx))
2508
- } else {
2509
- values[Int(wordIdx)] =
2510
- values[Int(wordIdx)] & ~(1 << _BitMap.bitIndex(idx))
2511
- }
2512
- }
2513
- }
2514
- }
2515
-
2516
2458
/// Header part of the native storage.
2517
2459
internal struct _HashedContainerStorageHeader {
2518
2460
internal init(capacity: Int) {
@@ -2548,8 +2490,8 @@ final internal class _Native${Self}StorageImpl<${TypeParameters}> :
2548
2490
/// Returns the bytes necessary to store a bit map of 'capacity' bytes and
2549
2491
/// padding to align the start to word alignment.
2550
2492
internal static func bytesForBitMap(capacity: Int) -> Int {
2551
- let numWords = _BitMap.wordsFor( capacity)
2552
- return numWords * sizeof (UInt) + alignof(UInt)
2493
+ let numWords = _UnsafeBitMap.sizeInWords(forSizeInBits: capacity)
2494
+ return numWords * strideof (UInt) + alignof(UInt)
2553
2495
}
2554
2496
2555
2497
/// Returns the bytes necessary to store 'capacity' keys and padding to align
@@ -2560,12 +2502,11 @@ final internal class _Native${Self}StorageImpl<${TypeParameters}> :
2560
2502
return strideof(Key.self) * capacity + padding
2561
2503
}
2562
2504
2505
+ %if Self == 'Dictionary':
2563
2506
/// Returns the bytes necessary to store 'capacity' values and padding to
2564
2507
/// align the start to the alignment of the 'Value' type assuming a base
2565
2508
/// address aligned to the maximum of the alignment of the 'Key' type and the
2566
2509
/// alignment of a word.
2567
-
2568
- %if Self == 'Dictionary':
2569
2510
internal static func bytesForValues(capacity: Int) -> Int {
2570
2511
let maxPrevAlignment = max(alignof(Key.self), alignof(UInt))
2571
2512
let padding = max(0, alignof(Value.self) - maxPrevAlignment)
@@ -2574,9 +2515,7 @@ final internal class _Native${Self}StorageImpl<${TypeParameters}> :
2574
2515
%end
2575
2516
2576
2517
internal var buffer: BufferPointer {
2577
- get {
2578
- return BufferPointer(self)
2579
- }
2518
+ return BufferPointer(self)
2580
2519
}
2581
2520
2582
2521
// All underscored functions are unsafe and need a _fixLifetime in the caller.
@@ -2591,9 +2530,7 @@ final internal class _Native${Self}StorageImpl<${TypeParameters}> :
2591
2530
2592
2531
@_versioned
2593
2532
internal var _capacity: Int {
2594
- get {
2595
- return _body.capacity
2596
- }
2533
+ return _body.capacity
2597
2534
}
2598
2535
2599
2536
@_versioned
@@ -2607,45 +2544,30 @@ final internal class _Native${Self}StorageImpl<${TypeParameters}> :
2607
2544
}
2608
2545
2609
2546
internal var _maxLoadFactorInverse : Double {
2610
- get {
2611
- return _body.maxLoadFactorInverse
2612
- }
2547
+ return _body.maxLoadFactorInverse
2613
2548
}
2614
2549
2615
2550
internal
2616
2551
var _initializedHashtableEntriesBitMapStorage: UnsafeMutablePointer<UInt> {
2617
- get {
2618
- let start = UInt(Builtin.ptrtoint_Word(buffer._elementPointer._rawValue))
2619
- let alignment = UInt(alignof(UInt))
2620
- let alignMask = alignment &- UInt(1)
2621
- return UnsafeMutablePointer<UInt>(
2622
- bitPattern:(start &+ alignMask) & ~alignMask)!
2623
- }
2552
+ return _roundUp(buffer._elementPointer, toAlignmentOf: UInt.self)
2624
2553
}
2625
2554
2626
2555
internal var _keys: UnsafeMutablePointer<Key> {
2627
- get {
2628
- let start =
2629
- UInt(Builtin.ptrtoint_Word(
2630
- _initializedHashtableEntriesBitMapStorage._rawValue)) &+
2631
- UInt(_BitMap.wordsFor(_capacity)) &* UInt(strideof(UInt))
2632
- let alignment = UInt(alignof(Key))
2633
- let alignMask = alignment &- UInt(1)
2634
- return UnsafeMutablePointer<Key>(
2635
- bitPattern:(start &+ alignMask) & ~alignMask)!
2636
- }
2556
+ let bitMapSizeInBytes =
2557
+ _unsafeMultiply(
2558
+ _UnsafeBitMap.sizeInWords(forSizeInBits: _capacity),
2559
+ strideof(UInt))
2560
+ let start =
2561
+ UnsafeMutablePointer<UInt8>(_initializedHashtableEntriesBitMapStorage)
2562
+ + bitMapSizeInBytes
2563
+ return _roundUp(start, toAlignmentOf: Key.self)
2637
2564
}
2638
2565
2639
2566
%if Self == 'Dictionary':
2640
2567
internal var _values: UnsafeMutablePointer<Value> {
2641
- get {
2642
- let start = UInt(Builtin.ptrtoint_Word(_keys._rawValue)) &+
2643
- UInt(_capacity) &* UInt(strideof(Key.self))
2644
- let alignment = UInt(alignof(Value))
2645
- let alignMask = alignment &- UInt(1)
2646
- return UnsafeMutablePointer<Value>(
2647
- bitPattern:(start &+ alignMask) & ~alignMask)!
2648
- }
2568
+ let keysSizeInBytes = _unsafeMultiply(_capacity, strideof(Key.self))
2569
+ let start = UnsafeMutablePointer<UInt8>(_keys) + keysSizeInBytes
2570
+ return _roundUp(start, toAlignmentOf: Value.self)
2649
2571
}
2650
2572
%end
2651
2573
@@ -2655,14 +2577,14 @@ final internal class _Native${Self}StorageImpl<${TypeParameters}> :
2655
2577
let requiredCapacity =
2656
2578
bytesForBitMap(capacity: capacity) + bytesForKeys(capacity: capacity)
2657
2579
%if Self == 'Dictionary':
2658
- + bytesForValues(capacity: capacity)
2580
+ + bytesForValues(capacity: capacity)
2659
2581
%end
2660
2582
2661
2583
let r = super.create(minimumCapacity: requiredCapacity) { _ in
2662
2584
return _HashedContainerStorageHeader(capacity: capacity)
2663
2585
}
2664
2586
let storage = r as! StorageImpl
2665
- let initializedEntries = _BitMap (
2587
+ let initializedEntries = _UnsafeBitMap (
2666
2588
storage: storage._initializedHashtableEntriesBitMapStorage,
2667
2589
bitCount: capacity)
2668
2590
initializedEntries.initializeToZero()
@@ -2671,7 +2593,7 @@ final internal class _Native${Self}StorageImpl<${TypeParameters}> :
2671
2593
2672
2594
deinit {
2673
2595
let capacity = _capacity
2674
- let initializedEntries = _BitMap (
2596
+ let initializedEntries = _UnsafeBitMap (
2675
2597
storage: _initializedHashtableEntriesBitMapStorage, bitCount: capacity)
2676
2598
let keys = _keys
2677
2599
%if Self == 'Dictionary':
@@ -2721,17 +2643,17 @@ struct _Native${Self}Storage<${TypeParametersDecl}> :
2721
2643
2722
2644
internal let buffer: StorageImpl
2723
2645
2724
- internal let initializedEntries: _BitMap
2646
+ internal let initializedEntries: _UnsafeBitMap
2725
2647
internal let keys: UnsafeMutablePointer<Key>
2726
2648
%if Self == 'Dictionary':
2727
2649
internal let values: UnsafeMutablePointer<Value>
2728
2650
%end
2729
2651
2730
2652
internal init(capacity: Int) {
2731
2653
buffer = StorageImpl.create(capacity: capacity)
2732
- initializedEntries = _BitMap (
2733
- storage: buffer._initializedHashtableEntriesBitMapStorage,
2734
- bitCount: capacity)
2654
+ initializedEntries = _UnsafeBitMap (
2655
+ storage: buffer._initializedHashtableEntriesBitMapStorage,
2656
+ bitCount: capacity)
2735
2657
keys = buffer._keys
2736
2658
%if Self == 'Dictionary':
2737
2659
values = buffer._values
@@ -2754,11 +2676,9 @@ struct _Native${Self}Storage<${TypeParametersDecl}> :
2754
2676
@_transparent
2755
2677
public // @testable
2756
2678
var capacity: Int {
2757
- get {
2758
- let result = buffer._capacity
2759
- _fixLifetime(buffer)
2760
- return result
2761
- }
2679
+ let result = buffer._capacity
2680
+ _fixLifetime(buffer)
2681
+ return result
2762
2682
}
2763
2683
2764
2684
@_versioned
@@ -2777,11 +2697,9 @@ struct _Native${Self}Storage<${TypeParametersDecl}> :
2777
2697
2778
2698
@_transparent
2779
2699
internal var maxLoadFactorInverse: Double {
2780
- get {
2781
- let result = buffer._maxLoadFactorInverse
2782
- _fixLifetime(buffer)
2783
- return result
2784
- }
2700
+ let result = buffer._maxLoadFactorInverse
2701
+ _fixLifetime(buffer)
2702
+ return result
2785
2703
}
2786
2704
2787
2705
@_versioned
@@ -3139,16 +3057,15 @@ internal struct _BridgedNative${Self}Storage {
3139
3057
internal typealias SequenceElement = ${AnySequenceType}
3140
3058
3141
3059
internal let buffer: StorageImpl
3142
- internal let initializedEntries: _BitMap
3060
+ internal let initializedEntries: _UnsafeBitMap
3143
3061
internal let keys: UnsafeMutablePointer<AnyObject>
3144
3062
%if Self == 'Dictionary':
3145
3063
internal let values: UnsafeMutablePointer<AnyObject>
3146
3064
%end
3147
3065
3148
-
3149
3066
internal init(buffer: StorageImpl) {
3150
3067
self.buffer = buffer
3151
- initializedEntries = _BitMap (
3068
+ initializedEntries = _UnsafeBitMap (
3152
3069
storage: buffer._initializedHashtableEntriesBitMapStorage,
3153
3070
bitCount: buffer._capacity)
3154
3071
keys = buffer._keys
@@ -3160,11 +3077,9 @@ internal struct _BridgedNative${Self}Storage {
3160
3077
3161
3078
@_transparent
3162
3079
internal var capacity: Int {
3163
- get {
3164
- let result = buffer._capacity
3165
- _fixLifetime(buffer)
3166
- return result
3167
- }
3080
+ let result = buffer._capacity
3081
+ _fixLifetime(buffer)
3082
+ return result
3168
3083
}
3169
3084
3170
3085
@_versioned
0 commit comments