diff --git a/Sources/PersistentCollections/PersistentDictionary+Collection.swift b/Sources/PersistentCollections/PersistentDictionary+Collection.swift index fb6029d4..4a108f3b 100644 --- a/Sources/PersistentCollections/PersistentDictionary+Collection.swift +++ b/Sources/PersistentCollections/PersistentDictionary+Collection.swift @@ -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) - } -} diff --git a/Sources/PersistentCollections/PersistentDictionary._Node.swift b/Sources/PersistentCollections/PersistentDictionary._Node.swift index fc967cd0..3282caf5 100644 --- a/Sources/PersistentCollections/PersistentDictionary._Node.swift +++ b/Sources/PersistentCollections/PersistentDictionary._Node.swift @@ -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 { diff --git a/Sources/PersistentCollections/_DictionaryNodeProtocol.swift b/Sources/PersistentCollections/_DictionaryNodeProtocol.swift index 8e4ad1ed..f40f5750 100644 --- a/Sources/PersistentCollections/_DictionaryNodeProtocol.swift +++ b/Sources/PersistentCollections/_DictionaryNodeProtocol.swift @@ -20,7 +20,7 @@ internal protocol _DictionaryNodeProtocol: _NodeProtocol { func index( _ key: Key, _ hash: Int, _ shift: Int, _ skippedBefore: Int - ) -> PersistentDictionaryIndex? + ) -> PersistentDictionary.Index? func updateOrUpdating( _ isStorageKnownUniquelyReferenced: Bool, diff --git a/Tests/PersistentCollectionsTests/PersistentCollections Smoke Tests.swift b/Tests/PersistentCollectionsTests/PersistentCollections Smoke Tests.swift index ed092521..286ea598 100644 --- a/Tests/PersistentCollectionsTests/PersistentCollections Smoke Tests.swift +++ b/Tests/PersistentCollectionsTests/PersistentCollections Smoke Tests.swift @@ -578,15 +578,16 @@ final class CapsuleSmokeTests: CollectionTestCase { CollidableInt(32769): "b" ] + typealias Index = PersistentDictionary.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) diff --git a/Tests/PersistentCollectionsTests/PersistentCollections Tests.swift b/Tests/PersistentCollectionsTests/PersistentCollections Tests.swift index 7ab0aee1..626fa5df 100644 --- a/Tests/PersistentCollectionsTests/PersistentCollections Tests.swift +++ b/Tests/PersistentCollectionsTests/PersistentCollections Tests.swift @@ -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