@@ -1813,9 +1813,7 @@ public struct Dictionary<Key : Hashable, Value> :
1813
1813
by keyForValue: (S.Element) throws -> Key
1814
1814
) rethrows where Value == [S.Element] {
1815
1815
self = [:]
1816
- for value in values {
1817
- self[try keyForValue(value), default: []].append(value)
1818
- }
1816
+ try _variantBuffer.nativeGroup(values, by: keyForValue)
1819
1817
}
1820
1818
1821
1819
internal init(_nativeBuffer: _NativeDictionaryBuffer<Key, Value>) {
@@ -5058,6 +5056,32 @@ internal enum _Variant${Self}Buffer<${TypeParametersDecl}> : _HashBuffer {
5058
5056
#endif
5059
5057
}
5060
5058
}
5059
+
5060
+ internal mutating func nativeGroup<S: Sequence>(
5061
+ _ values: S,
5062
+ by keyForValue: (S.Element) throws -> Key
5063
+ ) rethrows where Value == [S.Element] {
5064
+ defer { _fixLifetime(asNative) }
5065
+ for value in values {
5066
+ let key = try keyForValue(value)
5067
+ var (i, found) = asNative._find(key, startBucket: asNative._bucket(key))
5068
+ if found {
5069
+ asNative.values[i.offset].append(value)
5070
+ } else {
5071
+ let minCapacity = NativeBuffer.minimumCapacity(
5072
+ minimumCount: asNative.count + 1,
5073
+ maxLoadFactorInverse: _hashContainerDefaultMaxLoadFactorInverse)
5074
+
5075
+ let (_, capacityChanged) = ensureUniqueNativeBuffer(minCapacity)
5076
+ if capacityChanged {
5077
+ i = asNative._find(key, startBucket: asNative._bucket(key)).pos
5078
+ }
5079
+
5080
+ asNative.initializeKey(key, value: [value], at: i.offset)
5081
+ asNative.count += 1
5082
+ }
5083
+ }
5084
+ }
5061
5085
%end
5062
5086
5063
5087
/// - parameter idealBucket: The ideal bucket for the element being deleted.
0 commit comments