[Capsule] Use count as guidance for duplication checks

This commit is contained in:
Michael Steindorfer committed 2022-08-09 17:20:28 +02:00
1 parent 18209103d4
commit fbb72e0638
1 file changed
+5 -2
+5 -2
View File
@@ -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)
}