mirror of
https://github.com/apple/swift-collections
synced 2026-11-29 05:14:20 +00:00
[PersistentDictionary] Small cleanups
This commit is contained in:
1 parent
e19ba8e787
commit
c355812e53
6 files changed
+28
-21
No files matched your search
+1
-1
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
+5
@@ -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)
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
public struct PersistentDictionary<Key, Value> where Key: Hashable {
|
||||
public struct PersistentDictionary<Key: Hashable, Value> {
|
||||
@usableFromInline
|
||||
internal typealias _Node = PersistentCollections._Node<Key, Value>
|
||||
|
||||
@@ -30,19 +30,11 @@ public struct PersistentDictionary<Key, Value> 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
|
||||
|
||||
@@ -129,12 +129,7 @@ final class PersistentDictionarySmokeTests: CollectionTestCase {
|
||||
var map3: PersistentDictionary<Collider, String> = map2
|
||||
for index in 0..<upperBound {
|
||||
map3[Collider(index)] = nil
|
||||
|
||||
expectEqual(map3.count, inferSize(map3))
|
||||
if map3.count != inferSize(map3) {
|
||||
assertionFailure()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
expectEqual(map3.count, 0)
|
||||
|
||||
@@ -155,10 +155,10 @@ func expectEqualDictionaries<Key: Hashable, Value: Equatable>(
|
||||
var dict = dict
|
||||
var seen: Set<Key> = []
|
||||
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<Key: Hashable, Value: Equatable>(
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user