Skip to content

Commit 5e776e5

Browse files
author
Alexis Beingessner
committed
cleanup initializers in HashedCollections
1 parent 428cdac commit 5e776e5

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

stdlib/public/core/HashedCollections.swift.gyb

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ public struct Set<Element : Hashable> :
475475
public init(minimumCapacity: Int) {
476476
_variantBuffer =
477477
_VariantBuffer.native(
478-
_NativeBuffer.create(minimumCapacity: minimumCapacity))
478+
_NativeBuffer(minimumCapacity: minimumCapacity))
479479
}
480480

481481
/// Private initializer.
@@ -1667,7 +1667,7 @@ public struct Dictionary<Key : Hashable, Value> :
16671667
/// allocate buffer for in the new dictionary.
16681668
public init(minimumCapacity: Int) {
16691669
_variantBuffer =
1670-
.native(_NativeBuffer.create(minimumCapacity: minimumCapacity))
1670+
.native(_NativeBuffer(minimumCapacity: minimumCapacity))
16711671
}
16721672

16731673
internal init(_nativeBuffer: _NativeDictionaryBuffer<Key, Value>) {
@@ -2534,6 +2534,8 @@ internal class _RawNative${Self}Storage:
25342534
return storage
25352535
}()
25362536

2537+
// This type is made with allocWithTailElems, so no init is ever called.
2538+
// But we still need to have an init to satisfy the compiler.
25372539
internal init(_doNotCallMe: ()) {
25382540
_sanityCheckFailure("Only create this by using the `empty` singleton")
25392541
}
@@ -2664,8 +2666,10 @@ internal class _TypedNative${Self}Storage<${TypeParameters}> :
26642666
_fixLifetime(self)
26652667
}
26662668

2669+
// This type is made with allocWithTailElems, so no init is ever called.
2670+
// But we still need to have an init to satisfy the compiler.
26672671
override internal init(_doNotCallMe: ()) {
2668-
_sanityCheckFailure("Only create this by calling Buffer.createUnhashable")
2672+
_sanityCheckFailure("Only create this by calling Buffer's inits")
26692673
}
26702674

26712675
#if _runtime(_ObjC)
@@ -2696,8 +2700,10 @@ final internal class _HashableTypedNative${Self}Storage<${TypeParametersDecl}> :
26962700
internal typealias FullContainer = ${Self}<${TypeParameters}>
26972701
internal typealias Buffer = _Native${Self}Buffer<${TypeParameters}>
26982702

2703+
// This type is made with allocWithTailElems, so no init is ever called.
2704+
// But we still need to have an init to satisfy the compiler.
26992705
override internal init(_doNotCallMe: ()) {
2700-
_sanityCheckFailure("Only create this by calling Buffer.create")
2706+
_sanityCheckFailure("Only create this by calling Buffer's inits'")
27012707
}
27022708

27032709

@@ -2867,8 +2873,8 @@ internal struct _Native${Self}Buffer<${TypeParameters}> {
28672873
internal var _storage: RawStorage
28682874

28692875
/// Creates a Buffer with a storage that is typed, but doesn't understand
2870-
/// Hashing. Mostly for bridging; prefer `create(capacity:)`.
2871-
internal static func createUnhashable(capacity: Int) -> Buffer {
2876+
/// Hashing. Mostly for bridging; prefer `init(capacity:)`.
2877+
internal init(capacity: Int, unhashable: ()) {
28722878
let numWordsForBitmap = _UnsafeBitMap.sizeInWords(forSizeInBits: capacity)
28732879
%if Self == 'Dictionary':
28742880
let storage = Builtin.allocWithTailElems_3(TypedStorage.self,
@@ -2880,31 +2886,29 @@ internal struct _Native${Self}Buffer<${TypeParameters}> {
28802886
numWordsForBitmap._builtinWordValue, UInt.self,
28812887
capacity._builtinWordValue, Key.self)
28822888
%end
2883-
return create(capacity: capacity, storage: storage)
2889+
self.init(capacity: capacity, storage: storage)
28842890
}
28852891

28862892
/// Given a capacity and uninitialized RawStorage, completes the
28872893
/// initialization and returns a Buffer.
2888-
internal static func create(capacity: Int, storage: RawStorage) -> Buffer {
2894+
internal init(capacity: Int, storage: RawStorage) {
28892895
// We can initialize by assignment because _HashedContainerBufferHeader
28902896
// is a trivial type, i.e. contains no references.
28912897
storage._body = BufferHeader(capacity: capacity)
28922898

2893-
var buffer = Buffer(_storage: storage)
2899+
self.init(_storage: storage)
28942900

28952901
let initializedEntries = _UnsafeBitMap(
2896-
storage: buffer._initializedHashtableEntriesBitMapBuffer,
2902+
storage: _initializedHashtableEntriesBitMapBuffer,
28972903
bitCount: capacity)
28982904
initializedEntries.initializeToZero()
28992905

29002906
// Initialize header
2901-
buffer._body.initializedEntries = initializedEntries
2902-
buffer._body.keys = UnsafeMutableRawPointer(buffer._keys)
2907+
_body.initializedEntries = initializedEntries
2908+
_body.keys = UnsafeMutableRawPointer(_keys)
29032909
%if Self == 'Dictionary':
2904-
buffer._body.values = UnsafeMutableRawPointer(buffer._values)
2910+
_body.values = UnsafeMutableRawPointer(_values)
29052911
%end
2906-
2907-
return buffer
29082912
}
29092913

29102914
/// A convenience forwarding the _storage's body
@@ -3161,19 +3165,19 @@ extension _Native${Self}Buffer
31613165
internal typealias SequenceElement = ${Sequence}
31623166

31633167
@inline(__always)
3164-
internal static func create(minimumCapacity: Int) -> Buffer {
3168+
internal init(minimumCapacity: Int) {
31653169
// Make sure there's a representable power of 2 >= minimumCapacity
31663170
_sanityCheck(minimumCapacity <= (Int.max >> 1) + 1)
31673171
var capacity = 2
31683172
while capacity < minimumCapacity {
31693173
capacity <<= 1
31703174
}
3171-
return create(capacity: capacity)
3175+
self.init(capacity: capacity)
31723176
}
31733177

31743178
/// Create a buffer instance with room for at least 'capacity'
31753179
/// entries and all entries marked invalid.
3176-
internal static func create(capacity: Int) -> Buffer {
3180+
internal init(capacity: Int) {
31773181
let numWordsForBitmap = _UnsafeBitMap.sizeInWords(forSizeInBits: capacity)
31783182
%if Self == 'Dictionary':
31793183
let storage = Builtin.allocWithTailElems_3(HashTypedStorage.self,
@@ -3185,7 +3189,7 @@ extension _Native${Self}Buffer
31853189
numWordsForBitmap._builtinWordValue, UInt.self,
31863190
capacity._builtinWordValue, Key.self)
31873191
%end
3188-
return create(capacity: capacity, storage: storage)
3192+
self.init(capacity: capacity, storage: storage)
31893193
}
31903194

31913195
#if _runtime(_ObjC)
@@ -3395,7 +3399,7 @@ extension _Native${Self}Buffer
33953399
Buffer.minimumCapacity(
33963400
minimumCount: elements.count,
33973401
maxLoadFactorInverse: _hashContainerDefaultMaxLoadFactorInverse)
3398-
var nativeBuffer = Buffer.create(minimumCapacity: requiredCapacity)
3402+
var nativeBuffer = Buffer(minimumCapacity: requiredCapacity)
33993403

34003404
%if Self == 'Set':
34013405

@@ -3517,7 +3521,7 @@ final internal class _SwiftDeferredNS${Self}<${TypeParametersDecl}>
35173521
%end
35183522

35193523
internal init(minimumCapacity: Int = 2) {
3520-
nativeBuffer = NativeBuffer.create(minimumCapacity: minimumCapacity)
3524+
nativeBuffer = NativeBuffer(minimumCapacity: minimumCapacity)
35213525
super.init()
35223526
}
35233527

@@ -3646,8 +3650,7 @@ final internal class _SwiftDeferredNS${Self}<${TypeParametersDecl}>
36463650
}
36473651

36483652
// Create buffer for bridged data.
3649-
let bridged = BridgedBuffer.createUnhashable(
3650-
capacity: nativeBuffer.capacity)
3653+
let bridged = BridgedBuffer(capacity: nativeBuffer.capacity, unhashable: ())
36513654

36523655
// Bridge everything.
36533656
for i in 0..<nativeBuffer.capacity {
@@ -4014,7 +4017,7 @@ internal enum _Variant${Self}Buffer<${TypeParametersDecl}> : _HashBuffer {
40144017
}
40154018

40164019
let oldNativeBuffer = asNative
4017-
var newNativeBuffer = NativeBuffer.create(minimumCapacity: minimumCapacity)
4020+
var newNativeBuffer = NativeBuffer(minimumCapacity: minimumCapacity)
40184021
let newCapacity = newNativeBuffer.capacity
40194022
for i in 0..<oldCapacity {
40204023
if oldNativeBuffer.isInitializedEntry(at: i) {
@@ -4047,7 +4050,7 @@ internal enum _Variant${Self}Buffer<${TypeParametersDecl}> : _HashBuffer {
40474050
case .cocoa(let cocoaBuffer):
40484051
#if _runtime(_ObjC)
40494052
let cocoa${Self} = cocoaBuffer.cocoa${Self}
4050-
var newNativeBuffer = NativeBuffer.create(minimumCapacity: minimumCapacity)
4053+
var newNativeBuffer = NativeBuffer(minimumCapacity: minimumCapacity)
40514054
let oldCocoaIterator = _Cocoa${Self}Iterator(cocoa${Self})
40524055
%if Self == 'Set':
40534056
while let key = oldCocoaIterator.next() {
@@ -4572,7 +4575,7 @@ internal enum _Variant${Self}Buffer<${TypeParametersDecl}> : _HashBuffer {
45724575
}
45734576

45744577
if !keepCapacity {
4575-
self = .native(NativeBuffer.create(minimumCapacity: 2))
4578+
self = .native(NativeBuffer(minimumCapacity: 2))
45764579
return
45774580
}
45784581

@@ -4586,7 +4589,7 @@ internal enum _Variant${Self}Buffer<${TypeParametersDecl}> : _HashBuffer {
45864589
nativeRemoveAll()
45874590
case .cocoa(let cocoaBuffer):
45884591
#if _runtime(_ObjC)
4589-
self = .native(NativeBuffer.create(minimumCapacity: cocoaBuffer.count))
4592+
self = .native(NativeBuffer(minimumCapacity: cocoaBuffer.count))
45904593
#else
45914594
_sanityCheckFailure("internal error: unexpected cocoa ${Self}")
45924595
#endif

0 commit comments

Comments
 (0)