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