diff --git a/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Collection.swift b/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Collection.swift index 0f71e296..a8968aab 100644 --- a/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Collection.swift +++ b/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Collection.swift @@ -77,7 +77,7 @@ extension PersistentDictionary.Index: CustomDebugStringConvertible { } } -extension PersistentDictionary: BidirectionalCollection { +extension PersistentDictionary: Collection { @inlinable public var isEmpty: Bool { _root.count == 0 @@ -130,14 +130,6 @@ extension PersistentDictionary: BidirectionalCollection { } } - @inlinable - public func formIndex(before i: inout Index) { - precondition(_isValid(i), "Invalid index") - guard i._path.findPredecessorItem(under: _root.raw) else { - preconditionFailure("The start index has no predecessor") - } - } - @inlinable @inline(__always) public func index(after i: Index) -> Index { var i = i @@ -145,13 +137,6 @@ extension PersistentDictionary: BidirectionalCollection { return i } - @inlinable @inline(__always) - public func index(before i: Index) -> Index { - var i = i - formIndex(before: &i) - return i - } - @inlinable public func distance(from start: Index, to end: Index) -> Int { precondition(_isValid(start) && _isValid(end), "Invalid index") @@ -182,3 +167,37 @@ extension PersistentDictionary: BidirectionalCollection { return nil } } + +#if false +// Note: Let's not do this. `BidirectionalCollection` would imply that +// the ordering of elements would be meaningful, which isn't true for +// `PersistentDictionary`. +extension PersistentDictionary: BidirectionalCollection { + /// Replaces the given index with its predecessor. + /// + /// - Parameter i: A valid index of the collection. + /// `i` must be greater than `startIndex`. + /// + /// - Complexity: O(1) + @inlinable + public func formIndex(before i: inout Index) { + precondition(_isValid(i), "Invalid index") + guard i._path.findPredecessorItem(under: _root.raw) else { + preconditionFailure("The start index has no predecessor") + } + } + + /// Returns the position immediately before the given index. + /// + /// - Parameter i: A valid index of the collection. + /// `i` must be greater than `startIndex`. + /// + /// - Complexity: O(1) + @inlinable @inline(__always) + public func index(before i: Index) -> Index { + var i = i + formIndex(before: &i) + return i + } +} +#endif diff --git a/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Keys.swift b/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Keys.swift index abc5da0f..f973ed5c 100644 --- a/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Keys.swift +++ b/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Keys.swift @@ -102,7 +102,7 @@ extension PersistentDictionary.Keys.Iterator: Sendable where Key: Sendable, Value: Sendable {} #endif -extension PersistentDictionary.Keys: BidirectionalCollection { +extension PersistentDictionary.Keys: Collection { public typealias Index = PersistentDictionary.Index @inlinable @@ -127,21 +127,11 @@ extension PersistentDictionary.Keys: BidirectionalCollection { _base.formIndex(after: &i) } - @inlinable - public func formIndex(before i: inout Index) { - _base.formIndex(before: &i) - } - @inlinable public func index(after i: Index) -> Index { _base.index(after: i) } - @inlinable - public func index(before i: Index) -> Index { - _base.index(before: i) - } - @inlinable public func index(_ i: Index, offsetBy distance: Int) -> Index { _base.index(i, offsetBy: distance) @@ -159,3 +149,17 @@ extension PersistentDictionary.Keys: BidirectionalCollection { _base.distance(from: start, to: end) } } + +#if false +extension PersistentDictionary.Keys: BidirectionalCollection { + @inlinable + public func formIndex(before i: inout Index) { + _base.formIndex(before: &i) + } + + @inlinable + public func index(before i: Index) -> Index { + _base.index(before: i) + } +} +#endif diff --git a/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Values.swift b/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Values.swift index 454e8544..cf8b48a2 100644 --- a/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Values.swift +++ b/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Values.swift @@ -106,7 +106,7 @@ where Key: Sendable, Value: Sendable {} // Note: This cannot be a MutableCollection because its subscript setter // needs to invalidate indices. -extension PersistentDictionary.Values: BidirectionalCollection { +extension PersistentDictionary.Values: Collection { public typealias Index = PersistentDictionary.Index @inlinable @@ -153,21 +153,11 @@ extension PersistentDictionary.Values: BidirectionalCollection { _base.formIndex(after: &i) } - @inlinable - public func formIndex(before i: inout Index) { - _base.formIndex(before: &i) - } - @inlinable public func index(after i: Index) -> Index { _base.index(after: i) } - @inlinable - public func index(before i: Index) -> Index { - _base.index(before: i) - } - @inlinable public func index(_ i: Index, offsetBy distance: Int) -> Index { _base.index(i, offsetBy: distance) @@ -185,3 +175,17 @@ extension PersistentDictionary.Values: BidirectionalCollection { _base.distance(from: start, to: end) } } + +#if false +extension PersistentDictionary.Values: BidirectionalCollection { + @inlinable + public func formIndex(before i: inout Index) { + _base.formIndex(before: &i) + } + + @inlinable + public func index(before i: Index) -> Index { + _base.index(before: i) + } +} +#endif diff --git a/Sources/PersistentCollections/PersistentSet/PersistentSet+Collection.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet+Collection.swift index 0d832613..0a9eff11 100644 --- a/Sources/PersistentCollections/PersistentSet/PersistentSet+Collection.swift +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet+Collection.swift @@ -78,7 +78,7 @@ extension PersistentSet.Index: CustomDebugStringConvertible { } } -extension PersistentSet: BidirectionalCollection { +extension PersistentSet: Collection { @inlinable public var isEmpty: Bool { _root.count == 0 @@ -130,14 +130,6 @@ extension PersistentSet: BidirectionalCollection { } } - @inlinable - public func formIndex(before i: inout Index) { - precondition(_isValid(i), "Invalid index") - guard i._path.findPredecessorItem(under: _root.raw) else { - preconditionFailure("The start index has no predecessor") - } - } - @inlinable @inline(__always) public func index(after i: Index) -> Index { var i = i @@ -145,13 +137,6 @@ extension PersistentSet: BidirectionalCollection { return i } - @inlinable @inline(__always) - public func index(before i: Index) -> Index { - var i = i - formIndex(before: &i) - return i - } - @inlinable public func distance(from start: Index, to end: Index) -> Int { precondition(_isValid(start) && _isValid(end), "Invalid index") @@ -221,3 +206,37 @@ extension PersistentSet: BidirectionalCollection { precondition(_isValid(range.lowerBound) && _isValid(range.upperBound)) } } + +#if false +// Note: Let's not do this. `BidirectionalCollection` would imply that +// the ordering of elements would be meaningful, which isn't true for +// `PersistentSet`. +extension PersistentSet: BidirectionalCollection { + /// Replaces the given index with its predecessor. + /// + /// - Parameter i: A valid index of the collection. + /// `i` must be greater than `startIndex`. + /// + /// - Complexity: O(1) + @inlinable + public func formIndex(before i: inout Index) { + precondition(_isValid(i), "Invalid index") + guard i._path.findPredecessorItem(under: _root.raw) else { + preconditionFailure("The start index has no predecessor") + } + } + + /// Returns the position immediately before the given index. + /// + /// - Parameter i: A valid index of the collection. + /// `i` must be greater than `startIndex`. + /// + /// - Complexity: O(1) + @inlinable @inline(__always) + public func index(before i: Index) -> Index { + var i = i + formIndex(before: &i) + return i + } +} +#endif diff --git a/Sources/_CollectionsTestSupport/ConformanceCheckers/CheckBidirectionalCollection.swift b/Sources/_CollectionsTestSupport/ConformanceCheckers/CheckBidirectionalCollection.swift index e0fe1596..34e11bc7 100644 --- a/Sources/_CollectionsTestSupport/ConformanceCheckers/CheckBidirectionalCollection.swift +++ b/Sources/_CollectionsTestSupport/ConformanceCheckers/CheckBidirectionalCollection.swift @@ -80,6 +80,61 @@ public func checkBidirectionalCollection( + _ collection: C, + expectedContents: [C.Element], + by areEquivalent: (C.Element, C.Element) -> Bool, + maxSamples: Int? = nil, + file: StaticString = #file, + line: UInt = #line +) { + var allIndices = collection._indicesByIndexAfter() + allIndices.append(collection.endIndex) + + withSomeRanges( + "range", + in: 0 ..< allIndices.count - (1 as Int), + maxSamples: maxSamples + ) { range in + let i = range.lowerBound + let j = range.upperBound + + // Check `index(_,offsetBy:)` with negative offsets + let a = collection.index(allIndices[j], offsetBy: i - j) + expectEqual(a, allIndices[i]) + if i < expectedContents.count { + expectEquivalent( + collection[a], expectedContents[i], + by: areEquivalent) + } + + // Check `distance(from:to:)` with decreasing indices + let d = collection.distance(from: allIndices[j], to: allIndices[i]) + expectEqual(d, i - j) + } + + // Check `index(_,offsetBy:limitedBy:)` + let limits = + Set([0, allIndices.count - 1, allIndices.count / 2]) + .sorted() + withEvery("limit", in: limits) { limit in + withEvery("i", in: 0 ..< allIndices.count) { i in + let min = -i - (limit <= i ? 2 : 0) + withEvery("delta", in: stride(from: 0, through: min, by: -1)) { delta in + let actual = collection.index( + allIndices[i], + offsetBy: delta, + limitedBy: allIndices[limit]) + let j = i + delta + let expected = i < limit || j >= limit ? allIndices[j] : nil + expectEqual(actual, expected) + } + } + } +} + public func _checkBidirectionalCollection( _ collection: C, expectedContents: S, @@ -112,6 +167,11 @@ public func _checkBidirectionalCollection= limit ? allIndices[j] : nil - expectEqual(actual, expected) - } - } - } - // Check the Indices associated type if C.self != C.Indices.self && C.self != DefaultIndices.self { TestContext.current.withTrace("Indices") { diff --git a/Sources/_CollectionsTestSupport/Utilities/DictionaryAPIChecker.swift b/Sources/_CollectionsTestSupport/Utilities/DictionaryAPIChecker.swift index 936e219e..dc16bfc6 100644 --- a/Sources/_CollectionsTestSupport/Utilities/DictionaryAPIChecker.swift +++ b/Sources/_CollectionsTestSupport/Utilities/DictionaryAPIChecker.swift @@ -107,10 +107,10 @@ public protocol DictionaryAPIExtras: DictionaryAPIChecker { uniqueKeysWithValues keysAndValues: S ) where S.Element == Element - init( - uniqueKeys keys: Keys, - values: Values - ) where Keys.Element == Key, Values.Element == Value +// init( +// uniqueKeys keys: Keys, +// values: Values +// ) where Keys.Element == Key, Values.Element == Value init( _ keysAndValues: S, diff --git a/Tests/PersistentCollectionsTests/PersistentCollections Fixtures.swift b/Tests/PersistentCollectionsTests/PersistentCollections Fixtures.swift index db219e48..8920eef7 100644 --- a/Tests/PersistentCollectionsTests/PersistentCollections Fixtures.swift +++ b/Tests/PersistentCollectionsTests/PersistentCollections Fixtures.swift @@ -38,9 +38,9 @@ extension LifetimeTracker { keyTransform: (C.Element) -> Key, valueTransform: (C.Element) -> Value ) -> PersistentDictionary, LifetimeTracked> { - PersistentDictionary( - uniqueKeys: instances(for: items, by: keyTransform), - values: instances(for: items, by: valueTransform)) + let keys = instances(for: items, by: keyTransform) + let values = instances(for: items, by: valueTransform) + return PersistentDictionary(uniqueKeysWithValues: zip(keys, values)) } func persistentDictionary( @@ -48,9 +48,9 @@ extension LifetimeTracker { by valueTransform: (C.Element) -> Value ) -> PersistentDictionary, LifetimeTracked> where C.Element: Hashable { - PersistentDictionary( - uniqueKeys: instances(for: keys), - values: instances(for: keys, by: valueTransform)) + let k = instances(for: keys) + let v = instances(for: keys, by: valueTransform) + return PersistentDictionary(uniqueKeysWithValues: zip(k, v)) } func dictionary( diff --git a/Tests/PersistentCollectionsTests/PersistentDictionary Tests.swift b/Tests/PersistentCollectionsTests/PersistentDictionary Tests.swift index 738734f5..baa2a30d 100644 --- a/Tests/PersistentCollectionsTests/PersistentDictionary Tests.swift +++ b/Tests/PersistentCollectionsTests/PersistentDictionary Tests.swift @@ -254,14 +254,20 @@ class PersistentDictionaryTests: CollectionTestCase { withEachFixture { fixture in withLifetimeTracking { tracker in let (d, ref) = tracker.persistentDictionary(for: fixture) - checkBidirectionalCollection(d, expectedContents: ref, by: ==) + checkCollection(d, expectedContents: ref, by: ==) + _checkBidirectionalCollection_indexOffsetBy( + d, expectedContents: ref, by: ==) } } } func test_BidirectionalCollection_random100() { - let d = PersistentDictionary(uniqueKeys: 0 ..< 100, values: 0 ..< 100) - checkBidirectionalCollection(d, expectedContents: Array(d), by: ==) + let d = PersistentDictionary( + uniqueKeysWithValues: (0 ..< 100).map { ($0, $0) }) + let ref = Array(d) + checkCollection(d, expectedContents: ref, by: ==) + _checkBidirectionalCollection_indexOffsetBy( + d, expectedContents: ref, by: ==) } #if swift(>=5.6) diff --git a/Tests/PersistentCollectionsTests/PersistentDictionary.Keys Tests.swift b/Tests/PersistentCollectionsTests/PersistentDictionary.Keys Tests.swift index 2a49a413..6fc4784e 100644 --- a/Tests/PersistentCollectionsTests/PersistentDictionary.Keys Tests.swift +++ b/Tests/PersistentCollectionsTests/PersistentDictionary.Keys Tests.swift @@ -18,10 +18,10 @@ class PersistentDictionaryKeysTests: CollectionTestCase { withEachFixture { fixture in withLifetimeTracking { tracker in let (d, ref) = tracker.persistentDictionary(for: fixture) - checkBidirectionalCollection( - d.keys, - expectedContents: ref.map { $0.key }, - by: ==) + let k = ref.map { $0.key } + checkCollection(d.keys, expectedContents: k, by: ==) + _checkBidirectionalCollection_indexOffsetBy( + d.keys, expectedContents: k, by: ==) } } } diff --git a/Tests/PersistentCollectionsTests/PersistentDictionary.Values Tests.swift b/Tests/PersistentCollectionsTests/PersistentDictionary.Values Tests.swift index 76265e4d..e3d93368 100644 --- a/Tests/PersistentCollectionsTests/PersistentDictionary.Values Tests.swift +++ b/Tests/PersistentCollectionsTests/PersistentDictionary.Values Tests.swift @@ -18,10 +18,10 @@ class PersistentDictionaryValuesTests: CollectionTestCase { withEachFixture { fixture in withLifetimeTracking { tracker in let (d, ref) = tracker.persistentDictionary(for: fixture) - checkBidirectionalCollection( - d.values, - expectedContents: ref.map { $0.value }, - by: ==) + let v = ref.map { $0.value } + checkCollection(d.values, expectedContents: v, by: ==) + _checkBidirectionalCollection_indexOffsetBy( + d.values, expectedContents: v, by: ==) } } } diff --git a/Tests/PersistentCollectionsTests/PersistentSet Tests.swift b/Tests/PersistentCollectionsTests/PersistentSet Tests.swift index adce7d93..29785f2d 100644 --- a/Tests/PersistentCollectionsTests/PersistentSet Tests.swift +++ b/Tests/PersistentCollectionsTests/PersistentSet Tests.swift @@ -26,14 +26,19 @@ class PersistentSetTests: CollectionTestCase { withEachFixture { fixture in withLifetimeTracking { tracker in let (set, ref) = tracker.persistentSet(for: fixture) - checkBidirectionalCollection(set, expectedContents: ref, by: ==) + checkCollection(set, expectedContents: ref, by: ==) + _checkBidirectionalCollection_indexOffsetBy( + set, expectedContents: ref, by: ==) } } } func test_BidirectionalCollection_random100() { let s = PersistentSet(0 ..< 100) - checkBidirectionalCollection(s, expectedContents: Array(s)) + let ref = Array(s) + checkCollection(s, expectedContents: ref) + _checkBidirectionalCollection_indexOffsetBy( + s, expectedContents: ref, by: ==) } func test_basics() { diff --git a/Tests/PersistentCollectionsTests/Utilities.swift b/Tests/PersistentCollectionsTests/Utilities.swift index 428ee13a..e5354529 100644 --- a/Tests/PersistentCollectionsTests/Utilities.swift +++ b/Tests/PersistentCollectionsTests/Utilities.swift @@ -25,7 +25,8 @@ extension LifetimeTracker { let k = Array(keys) let keys = self.instances(for: k) let values = self.instances(for: k.map { $0 + 100 }) - let dictionary = PersistentDictionary(uniqueKeys: keys, values: values) + let dictionary = PersistentDictionary( + uniqueKeysWithValues: zip(keys, values)) return (dictionary, keys, values) } }