mirror of
https://github.com/apple/swift-collections
synced 2026-11-24 04:27:53 +00:00
[Capsule] Use count as guidance for duplication checks
This commit is contained in:
1 parent
18209103d4
commit
fbb72e0638
1 file changed
+5
-2
@@ -43,11 +43,14 @@ public struct HashMap<Key, Value> where Key: Hashable {
|
||||
@inline(__always)
|
||||
public init<S>(uniqueKeysWithValues keysAndValues: S) where S : Sequence, S.Element == (Key, Value) {
|
||||
var builder = Self()
|
||||
var expectedCount = 0
|
||||
keysAndValues.forEach { key, value in
|
||||
guard !builder.contains(key) else {
|
||||
builder.insert(key: key, value: value)
|
||||
expectedCount += 1
|
||||
|
||||
guard expectedCount == builder.count else {
|
||||
preconditionFailure("Duplicate key: '\(key)'")
|
||||
}
|
||||
builder.insert(key: key, value: value)
|
||||
}
|
||||
self.init(builder)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user