mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
45 lines
1.4 KiB
Swift
45 lines
1.4 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// Initializes a `Dictionary` from unique members.
|
|
///
|
|
/// Using a builder can be faster than inserting members into an empty
|
|
/// `Dictionary`.
|
|
@_fixed_layout
|
|
public // SPI(Foundation)
|
|
struct _DictionaryBuilder<Key: Hashable, Value> {
|
|
@usableFromInline
|
|
internal var _target: _NativeDictionary<Key, Value>
|
|
@usableFromInline
|
|
internal let _requestedCount: Int
|
|
|
|
@inlinable
|
|
public init(count: Int) {
|
|
_target = _NativeDictionary(capacity: count)
|
|
_requestedCount = count
|
|
}
|
|
|
|
@inlinable
|
|
public mutating func add(key newKey: Key, value: Value) {
|
|
_precondition(_target.count < _requestedCount,
|
|
"Can't add more members than promised")
|
|
_target._unsafeInsertNew(key: newKey, value: value)
|
|
}
|
|
|
|
@inlinable
|
|
public __consuming func take() -> Dictionary<Key, Value> {
|
|
_precondition(_target.count == _requestedCount,
|
|
"The number of members added does not match the promised count")
|
|
return Dictionary(_native: _target)
|
|
}
|
|
}
|