diff --git a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Keys.swift b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Keys.swift new file mode 100644 index 00000000..437d8c48 --- /dev/null +++ b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Keys.swift @@ -0,0 +1,37 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2026 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +#if compiler(>=6.2) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS + +@available(SwiftStdlib 5.0, *) +extension RigidDictionary where Key: ~Copyable, Value: ~Copyable { + +#if false // This requires the BorrowAndMutateAccessors feature that isn't available yet. +// @inlinable +// public var keys: RigidSet { +// borrow { +// _keys +// } +// } +#endif + + /// This is a stand-in for an eventual computed property with a `borrow` + /// accessor. It is destined for deprecation once that feature becomes + /// available. + @_alwaysEmitIntoClient + public func withKeys( + _ body: (borrowing RigidSet) throws(E) -> R + ) throws(E) -> R { + try body(_keys) + } +} + +#endif diff --git a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Keys.swift b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Keys.swift new file mode 100644 index 00000000..802ea36f --- /dev/null +++ b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Keys.swift @@ -0,0 +1,37 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2026 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +#if compiler(>=6.2) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS + +@available(SwiftStdlib 5.0, *) +extension UniqueDictionary where Key: ~Copyable, Value: ~Copyable { + +#if false // This requires the BorrowAndMutateAccessors feature that isn't available yet. +// @inlinable +// public var keys: RigidSet { +// borrow { +// _storage.keys +// } +// } +#endif + + /// This is a stand-in for an eventual computed property with a `borrow` + /// accessor. It is destined for deprecation once that feature becomes + /// available. + @_alwaysEmitIntoClient + public func withKeys( + _ body: (borrowing RigidSet) throws(E) -> R + ) throws(E) -> R { + try _storage.withKeys(body) + } +} + +#endif diff --git a/Tests/BasicContainersTests/RigidDictionaryTests.swift b/Tests/BasicContainersTests/RigidDictionaryTests.swift index 6d21c96a..ff733773 100644 --- a/Tests/BasicContainersTests/RigidDictionaryTests.swift +++ b/Tests/BasicContainersTests/RigidDictionaryTests.swift @@ -239,6 +239,44 @@ class RigidDictionaryTests: CollectionTestCase { } } #endif + + func test_withKeys() { + typealias Key = LifetimeTracked + typealias Value = LifetimeTracked + withEvery("capacity", in: [0, 1, 2, 10, 100, 1000]) { capacity in + withLifetimeTracking { tracker in + var d = RigidDictionary(capacity: capacity) + withEvery("i", in: 0 ..< capacity) { i in + let key = tracker.instance(for: i) + let value = tracker.instance(for: "\(i)") + d.insertValue(value, forKey: key) + + d.withKeys { keys in + expectEqual(keys.count, i + 1) + expectEqual(keys.capacity, capacity) +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW + var it = keys.makeBorrowingIterator() + var actual: Set = [] + while true { + let next = it.nextSpan() + guard !next.isEmpty else { break } + for i in next.indices { + expectTrue( + actual.insert(next[i].payload).inserted, + "Duplicate value \(next[i].payload)") + } + } + expectEqualElements(actual.sorted(), 0 ... i) +#else + for j in 0 ... i { + expectTrue(keys.contains(tracker.instance(for: j))) + } +#endif + } + } + } + } + } } #endif diff --git a/Tests/BasicContainersTests/UniqueDictionaryTests.swift b/Tests/BasicContainersTests/UniqueDictionaryTests.swift new file mode 100644 index 00000000..b53cd47d --- /dev/null +++ b/Tests/BasicContainersTests/UniqueDictionaryTests.swift @@ -0,0 +1,59 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2026 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +import XCTest +#if COLLECTIONS_SINGLE_MODULE +import Collections +#else +import _CollectionsTestSupport +import BasicContainers +#endif + + +class UniqueDictionaryTests: CollectionTestCase { + func test_withKeys() { + typealias Key = LifetimeTracked + typealias Value = LifetimeTracked + withEvery("capacity", in: [0, 1, 2, 10, 100, 1000]) { capacity in + withLifetimeTracking { tracker in + var d = RigidDictionary(capacity: capacity) + withEvery("i", in: 0 ..< capacity) { i in + let key = tracker.instance(for: i) + let value = tracker.instance(for: "\(i)") + d.insertValue(value, forKey: key) + + d.withKeys { keys in + expectEqual(keys.count, i + 1) + expectEqual(keys.capacity, capacity) +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW + var it = keys.makeBorrowingIterator() + var actual: Set = [] + while true { + let next = it.nextSpan() + guard !next.isEmpty else { break } + for i in next.indices { + expectTrue( + actual.insert(next[i].payload).inserted, + "Duplicate value \(next[i].payload)") + } + } + expectEqualElements(actual.sorted(), 0 ... i) +#else + for j in 0 ... i { + expectTrue(keys.contains(tracker.instance(for: j))) + } +#endif + } + } + } + } + } +} diff --git a/Tests/BasicContainersTests/UniqueSetTests.swift b/Tests/BasicContainersTests/UniqueSetTests.swift index d3692908..3d0e937a 100644 --- a/Tests/BasicContainersTests/UniqueSetTests.swift +++ b/Tests/BasicContainersTests/UniqueSetTests.swift @@ -116,7 +116,7 @@ class UniqueSetTests: CollectionTestCase { func test_insert_growth() { // These are the storage capacities we expect to see for a UniqueSet // up to size 1000. - var expectedCapacities = [ + let expectedCapacities = [ 0, 1, 2, 4, 7, 14, 28, 56, 112, 224, 448, 896, 1792 ] var j = 0