diff --git a/Sources/PersistentCollections/Node/_AncestorOffsets.swift b/Sources/PersistentCollections/Node/_AncestorSlots.swift similarity index 98% rename from Sources/PersistentCollections/Node/_AncestorOffsets.swift rename to Sources/PersistentCollections/Node/_AncestorSlots.swift index f686ec9a..47f14ec1 100644 --- a/Sources/PersistentCollections/Node/_AncestorOffsets.swift +++ b/Sources/PersistentCollections/Node/_AncestorSlots.swift @@ -52,7 +52,7 @@ extension _AncestorSlots { return _Slot((path &>> level.shift) & _Bucket.bitMask) } set { - assert(newValue._value < UInt.bitWidth) + assert(newValue._value < _Bitmap.capacity) assert(self[level] == .zero) path |= (UInt(truncatingIfNeeded: newValue._value) &<< level.shift) } diff --git a/Sources/PersistentCollections/Node/_Node.swift b/Sources/PersistentCollections/Node/_Node.swift index f225bf84..de2159ca 100644 --- a/Sources/PersistentCollections/Node/_Node.swift +++ b/Sources/PersistentCollections/Node/_Node.swift @@ -119,3 +119,15 @@ extension _Node { read { $0.isAtrophiedNode } } } + +extension _Node { + @inlinable + internal var initialVersionNumber: UInt { + // Ideally we would simply just generate a true random number, but the + // memory address of the root node is a reasonable substitute. + // Alternatively, we could use a per-thread counter along with a thread + // id, or some sort of global banks of atomic integer counters. + let address = Unmanaged.passUnretained(raw.storage).toOpaque() + return UInt(bitPattern: address) + } +} diff --git a/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Debugging.swift b/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Debugging.swift index d6d0ef13..30fc20a5 100644 --- a/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Debugging.swift +++ b/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Debugging.swift @@ -16,6 +16,11 @@ extension PersistentDictionary { _isCollectionsInternalCheckingEnabled } + @inlinable + public func _invariantCheck() { + _root._fullInvariantCheck(.top, _Hash(_value: 0)) + } + public func _dump(iterationOrder: Bool = false) { _root.dump(iterationOrder: iterationOrder) } diff --git a/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary.swift b/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary.swift index 73736364..33f598d6 100644 --- a/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary.swift +++ b/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -public struct PersistentDictionary where Key: Hashable { +public struct PersistentDictionary { @usableFromInline internal typealias _Node = PersistentCollections._Node @@ -30,19 +30,11 @@ public struct PersistentDictionary where Key: Hashable { @inlinable internal init(_new: _Node) { - // Ideally we would simply just generate a true random number, but the - // memory address of the root node is a reasonable substitute. - let address = Unmanaged.passUnretained(_new.raw.storage).toOpaque() - self.init(_root: _new, version: UInt(bitPattern: address)) + self.init(_root: _new, version: _new.initialVersionNumber) } } extension PersistentDictionary { - @inlinable - public func _invariantCheck() { - _root._fullInvariantCheck(.top, _Hash(_value: 0)) - } - /// Accesses the value associated with the given key for reading and writing. /// /// This *key-based* subscript returns the value for the given key if the key diff --git a/Tests/PersistentCollectionsTests/PersistentCollections Smoke Tests.swift b/Tests/PersistentCollectionsTests/PersistentCollections Smoke Tests.swift index c7dc26ea..8aff7b30 100644 --- a/Tests/PersistentCollectionsTests/PersistentCollections Smoke Tests.swift +++ b/Tests/PersistentCollectionsTests/PersistentCollections Smoke Tests.swift @@ -129,12 +129,7 @@ final class PersistentDictionarySmokeTests: CollectionTestCase { var map3: PersistentDictionary = map2 for index in 0..( var dict = dict var seen: Set = [] var mismatches: [(key: Key, map: Value?, dict: Value?)] = [] - + var dupes: [(key: Key, map: Value)] = [] for (key, value) in map { if !seen.insert(key).inserted { - mismatches.append((key, value, nil)) + dupes.append((key, value)) } else { let expected = dict.removeValue(forKey: key) if value != expected { @@ -169,12 +169,15 @@ func expectEqualDictionaries( for (key, value) in dict { mismatches.append((key, nil, value)) } - if !mismatches.isEmpty { - let msg = mismatches.lazy.map { k, m, d in + if !mismatches.isEmpty || !dupes.isEmpty { + let msg1 = mismatches.lazy.map { k, m, d in "\n \(k): \(m == nil ? "nil" : "\(m!)") vs \(d == nil ? "nil" : "\(d!)")" }.joined(separator: "") + let msg2 = dupes.lazy.map { k, v in + "\n \(k): \(v) (duped)" + }.joined(separator: "") _expectFailure( - "\n\(mismatches.count) mismatches (actual vs expected):\(msg)", + "\n\(mismatches.count) mismatches (actual vs expected):\(msg1)\(msg2)", message, trapping: trapping, file: file, line: line) } }