mirror of
https://github.com/apple/swift-collections
synced 2026-11-25 04:37:48 +00:00
[PersistentDictionary] PersistentDictionaryIndex → PersistentDictionary.Index
This commit is contained in:
1 parent
6f93a628e8
commit
750559ea9e
5 files changed
+35
-40
No files matched your search
@@ -10,43 +10,38 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension PersistentDictionary: Collection {
|
||||
public typealias Index = PersistentDictionaryIndex
|
||||
|
||||
public struct Index: Comparable {
|
||||
internal let _value: Int
|
||||
|
||||
internal init(_value: Int) {
|
||||
self._value = _value
|
||||
}
|
||||
|
||||
public static func < (lhs: Self, rhs: Self) -> Bool {
|
||||
lhs._value < rhs._value
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// Manipulating Indices
|
||||
///
|
||||
|
||||
public var startIndex: Self.Index { PersistentDictionaryIndex(value: 0) }
|
||||
public var startIndex: Index { Index(_value: 0) }
|
||||
|
||||
public var endIndex: Self.Index { PersistentDictionaryIndex(value: count) }
|
||||
public var endIndex: Index { Index(_value: count) }
|
||||
|
||||
public func index(after i: Self.Index) -> Self.Index {
|
||||
return i + 1
|
||||
public func index(after i: Index) -> Index {
|
||||
Index(_value: i._value + 1)
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns the index for the given key.
|
||||
///
|
||||
public func index(forKey key: Key) -> Self.Index? {
|
||||
return rootNode.index(key, _computeHash(key), 0, 0)
|
||||
public func index(forKey key: Key) -> Index? {
|
||||
rootNode.index(key, _computeHash(key), 0, 0)
|
||||
}
|
||||
|
||||
///
|
||||
/// Accesses the key-value pair at the specified position.
|
||||
///
|
||||
public subscript(position: Self.Index) -> Self.Element {
|
||||
return rootNode.get(position: position, 0, position.value)
|
||||
public subscript(position: Index) -> Element {
|
||||
rootNode.get(position: position, 0, position._value)
|
||||
}
|
||||
}
|
||||
|
||||
public struct PersistentDictionaryIndex: Comparable {
|
||||
let value: Int
|
||||
|
||||
public static func < (lhs: Self, rhs: Self) -> Bool {
|
||||
return lhs.value < rhs.value
|
||||
}
|
||||
|
||||
public static func +(lhs: Self, rhs: Int) -> Self {
|
||||
return PersistentDictionaryIndex(value: lhs.value + rhs)
|
||||
}
|
||||
}
|
||||
@@ -45,6 +45,7 @@ extension _NodeHeader {
|
||||
|
||||
extension PersistentDictionary {
|
||||
internal final class _Node {
|
||||
typealias Index = PersistentDictionary.Index
|
||||
typealias Capacity = _NodeHeader.Capacity
|
||||
|
||||
typealias DataBufferElement = ReturnPayload
|
||||
@@ -399,7 +400,7 @@ extension PersistentDictionary._Node: _DictionaryNodeProtocol {
|
||||
_ keyHash: Int,
|
||||
_ shift: Int,
|
||||
_ skippedBefore: Int
|
||||
) -> PersistentDictionaryIndex? {
|
||||
) -> Index? {
|
||||
guard collisionFree else {
|
||||
let content: [ReturnPayload] = Array(self)
|
||||
let hash = _computeHash(content.first!.key)
|
||||
@@ -407,7 +408,7 @@ extension PersistentDictionary._Node: _DictionaryNodeProtocol {
|
||||
assert(keyHash == hash)
|
||||
return content
|
||||
.firstIndex(where: { _key, _ in _key == key })
|
||||
.map { PersistentDictionaryIndex(value: $0) }
|
||||
.map { Index(_value: $0) }
|
||||
}
|
||||
|
||||
let mask = _maskFrom(keyHash, shift)
|
||||
@@ -420,7 +421,7 @@ extension PersistentDictionary._Node: _DictionaryNodeProtocol {
|
||||
let payload = self.getPayload(index)
|
||||
guard key == payload.key else { return nil }
|
||||
|
||||
return PersistentDictionaryIndex(value: skippedBefore + skipped)
|
||||
return Index(_value: skippedBefore + skipped)
|
||||
}
|
||||
|
||||
guard (trieMap & bitpos) == 0 else {
|
||||
@@ -689,11 +690,7 @@ extension PersistentDictionary._Node: _DictionaryNodeProtocol {
|
||||
}
|
||||
|
||||
extension PersistentDictionary._Node {
|
||||
func get(
|
||||
position: PersistentDictionaryIndex,
|
||||
_ shift: Int,
|
||||
_ stillToSkip: Int
|
||||
) -> ReturnPayload {
|
||||
func get(position: Index, _ shift: Int, _ stillToSkip: Int) -> ReturnPayload {
|
||||
var cumulativeCounts = self._counts
|
||||
|
||||
for i in 1 ..< cumulativeCounts.count {
|
||||
|
||||
@@ -20,7 +20,7 @@ internal protocol _DictionaryNodeProtocol: _NodeProtocol {
|
||||
|
||||
func index(
|
||||
_ key: Key, _ hash: Int, _ shift: Int, _ skippedBefore: Int
|
||||
) -> PersistentDictionaryIndex?
|
||||
) -> PersistentDictionary<Key, Value>.Index?
|
||||
|
||||
func updateOrUpdating(
|
||||
_ isStorageKnownUniquelyReferenced: Bool,
|
||||
|
||||
@@ -578,15 +578,16 @@ final class CapsuleSmokeTests: CollectionTestCase {
|
||||
CollidableInt(32769): "b"
|
||||
]
|
||||
|
||||
typealias Index = PersistentDictionary<CollidableInt, String>.Index
|
||||
expectEqual(
|
||||
map.index(forKey: CollidableInt(11, 1)),
|
||||
PersistentDictionaryIndex(value: 0))
|
||||
Index(_value: 0))
|
||||
expectEqual(
|
||||
map.index(forKey: CollidableInt(12, 1)),
|
||||
PersistentDictionaryIndex(value: 1))
|
||||
Index(_value: 1))
|
||||
expectEqual(
|
||||
map.index(forKey: CollidableInt(32769)),
|
||||
PersistentDictionaryIndex(value: 2))
|
||||
Index(_value: 2))
|
||||
expectNil(map.index(forKey: CollidableInt(13, 1)))
|
||||
}
|
||||
|
||||
@@ -607,7 +608,7 @@ final class CapsuleSmokeTests: CollectionTestCase {
|
||||
|
||||
expectEqual(expectedValue, actualValue)
|
||||
|
||||
expectedPositions.remove(position.value)
|
||||
expectedPositions.remove(position._value)
|
||||
}
|
||||
|
||||
expectTrue(expectedPositions.isEmpty)
|
||||
|
||||
@@ -188,8 +188,8 @@ class PersistentDictionaryTests: CollectionTestCase {
|
||||
withEvery("offset", in: 0 ..< count) { offset in
|
||||
expectEqual(
|
||||
// NOTE: uses the actual order `d.keys`
|
||||
d.index(forKey: d.keys[offset]),
|
||||
PersistentDictionaryIndex(value: offset))
|
||||
d.index(forKey: d.keys[offset])?._value,
|
||||
offset)
|
||||
}
|
||||
expectNil(d.index(forKey: tracker.instance(for: -1)))
|
||||
expectNil(d.index(forKey: tracker.instance(for: count)))
|
||||
@@ -197,6 +197,7 @@ class PersistentDictionaryTests: CollectionTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
#if false
|
||||
// TODO: determine how to best calculate the expected order of the hash-trie
|
||||
// for testing purposes, without relying on the actual implementation
|
||||
func test_subscript_offset() {
|
||||
@@ -213,6 +214,7 @@ class PersistentDictionaryTests: CollectionTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
func test_subscript_getter() {
|
||||
withEvery("count", in: 0 ..< 30) { count in
|
||||
|
||||
Reference in new issue
Block a user