diff --git a/Sources/BasicContainers/CMakeLists.txt b/Sources/BasicContainers/CMakeLists.txt index d3c709c0..e8a7cdb6 100644 --- a/Sources/BasicContainers/CMakeLists.txt +++ b/Sources/BasicContainers/CMakeLists.txt @@ -68,6 +68,7 @@ target_sources(${module_name} PRIVATE "RigidSet/RigidSet+Debugging.swift" "RigidSet/RigidSet+Equatable.swift" "RigidSet/RigidSet+Hashable.swift" + "RigidSet/RigidSet+Index.swift" "RigidSet/RigidSet+Initializers.swift" "RigidSet/RigidSet+Insertions.swift" "RigidSet/RigidSet+Lookup.swift" @@ -79,6 +80,7 @@ target_sources(${module_name} PRIVATE "UniqueSet/UniqueSet+Debugging.swift" "UniqueSet/UniqueSet+Equatable.swift" "UniqueSet/UniqueSet+Hashable.swift" + "UniqueSet/UniqueSet+Index.swift" "UniqueSet/UniqueSet+Initializers.swift" "UniqueSet/UniqueSet+Insertions.swift" "UniqueSet/UniqueSet+Lookup.swift" @@ -88,6 +90,8 @@ target_sources(${module_name} PRIVATE "RigidDictionary/RigidDictionary+Consumption.swift" "RigidDictionary/RigidDictionary+Equatable.swift" "RigidDictionary/RigidDictionary+Hashable.swift" + "RigidDictionary/RigidDictionary+Index.swift" + "RigidDictionary/RigidDictionary+Indices.swift" "RigidDictionary/RigidDictionary+Initializers.swift" "RigidDictionary/RigidDictionary+Insertions.swift" "RigidDictionary/RigidDictionary+Keys.swift" @@ -98,6 +102,8 @@ target_sources(${module_name} PRIVATE "UniqueDictionary/UniqueDictionary+Consumption.swift" "UniqueDictionary/UniqueDictionary+Equatable.swift" "UniqueDictionary/UniqueDictionary+Hashable.swift" + "UniqueDictionary/UniqueDictionary+Index.swift" + "UniqueDictionary/UniqueDictionary+Indices.swift" "UniqueDictionary/UniqueDictionary+Initializers.swift" "UniqueDictionary/UniqueDictionary+Insertions.swift" "UniqueDictionary/UniqueDictionary+Keys.swift" diff --git a/Sources/BasicContainers/HashTable/_HTable+Bitmap.swift b/Sources/BasicContainers/HashTable/_HTable+Bitmap.swift index dcdb6dba..bb935422 100644 --- a/Sources/BasicContainers/HashTable/_HTable+Bitmap.swift +++ b/Sources/BasicContainers/HashTable/_HTable+Bitmap.swift @@ -67,7 +67,27 @@ extension _HTable.Bitmap { } @inlinable - package func firstUnoccupiedBucket(from start: Bucket) -> Bucket { + package func firstOccupiedBucket(from start: Bucket) -> Bucket? { + assert(isValid(start)) + + var word = start.word + var bits = _words[word] + bits.removeAll(upTo: start.bit) + + while true { + if let bit = bits.firstMember { + return Bucket(word: word, bit: bit) + } + word &+= 1 + if word >= _words.count { + return nil + } + bits = _words[word] + } + } + + @inlinable + package func firstUnoccupiedBucket(wrappingFrom start: Bucket) -> Bucket { assert(isValid(start)) var word = start.word var bits = _words[word] diff --git a/Sources/BasicContainers/HashTable/_HTable+Insert.swift b/Sources/BasicContainers/HashTable/_HTable+Insert.swift index 380c3d33..a378e89e 100644 --- a/Sources/BasicContainers/HashTable/_HTable+Insert.swift +++ b/Sources/BasicContainers/HashTable/_HTable+Insert.swift @@ -36,7 +36,7 @@ extension _HTable { #if COLLECTIONS_NO_ROBIN_HOOD_HASHING let ideal = idealBucket(forHashValue: hashValue) let bitmap = self.bitmap - let actual = bitmap.firstUnoccupiedBucket(from: ideal) + let actual = bitmap.nextUnoccupiedBucket(wrappingFrom: ideal) bitmap.setOccupied(actual) let probeLength = self.probeLength(from: ideal, to: actual) _maxProbeLength = Swift.max(_maxProbeLength, probeLength) diff --git a/Sources/BasicContainers/HashTable/_HTable+Iteration.swift b/Sources/BasicContainers/HashTable/_HTable+Iteration.swift index 7d4b9e0e..04417a8a 100644 --- a/Sources/BasicContainers/HashTable/_HTable+Iteration.swift +++ b/Sources/BasicContainers/HashTable/_HTable+Iteration.swift @@ -36,7 +36,7 @@ extension _HTable { /// iterator values outside the closure call that produced the original /// hash table. @usableFromInline - package struct BucketIterator: ~Copyable, ~Escapable { + package struct BucketIterator: ~Escapable { @usableFromInline package typealias Bucket = _HTable.Bucket @usableFromInline @@ -210,14 +210,14 @@ extension _HTable.BucketIterator { @_lifetime(self: copy self) package mutating func advanceToOccupied() -> Bool { if isAtEnd { return false } - guard _words != nil else { + if _words == nil { return true } while true { if let first = _nextBits.firstMember { _nextBits = _nextBits.shiftedDown(by: first) - _bucket._offset &+= first _nextBitCount &-= UInt8(first) + _bucket._offset &+= first break } guard _advanceToNextWord() else { return false } @@ -225,6 +225,42 @@ extension _HTable.BucketIterator { return true } + /// If the current bucket is already occupied, this does nothing. + @usableFromInline + @discardableResult + @_lifetime(self: copy self) + package mutating func advanceToOccupied( + maximumCount: Int + ) -> Bool { + var remainder = UInt(bitPattern: maximumCount) + if isAtEnd { return false } + if _words == nil { + let delta = Swift.min(_endBucket._offset &- _bucket._offset, remainder) + _bucket._offset &+= delta + return true + } + while true { + if let first = _nextBits.firstMember { + let c = Swift.min(remainder, first) + _nextBits = _nextBits.shiftedDown(by: c) + _nextBitCount &-= UInt8(c) + _bucket._offset &+= c + remainder &-= c + break + } + if remainder < _nextBitCount { + _nextBits = _nextBits.shiftedDown(by: remainder) + _nextBitCount &-= UInt8(remainder) + _bucket._offset &+= remainder + remainder = 0 + break + } + remainder &-= UInt(_nextBitCount) + guard _advanceToNextWord() else { return false } + } + return true + } + @usableFromInline @_lifetime(self: copy self) package mutating func wrapToOccupied() { @@ -271,14 +307,14 @@ extension _HTable.BucketIterator { @discardableResult @_lifetime(self: copy self) package mutating func advanceToUnoccupied( - maximumCount: Int = .max + maximumCount: Int ) -> Bool { assert(maximumCount > 0) assert(!isAtEnd) var remainder = UInt(bitPattern: maximumCount) if _words == nil { let delta = Swift.min(_endBucket._offset &- _bucket._offset, remainder) - _bucket._offset = _bucket._offset + delta + _bucket._offset &+= delta return false } while remainder > 0 { @@ -291,16 +327,15 @@ extension _HTable.BucketIterator { remainder &-= c break } - if remainder >= _nextBitCount { - remainder &-= UInt(_nextBitCount) - guard _advanceToNextWord() else { return false } - } else { + if remainder < _nextBitCount { _nextBits = _nextBits.shiftedDown(by: remainder) _nextBitCount &-= UInt8(remainder) _bucket._offset &+= remainder remainder = 0 break } + remainder &-= UInt(_nextBitCount) + guard _advanceToNextWord() else { return false } } return true } @@ -336,5 +371,19 @@ extension _HTable.BucketIterator { let end = self.currentBucket return Range(uncheckedBounds: (start, end)) } + + @usableFromInline + @_lifetime(self: copy self) + package mutating func nextUnoccupiedRegion( + maximumCount: Int = .max + ) -> Range? { + assert(maximumCount > 0) + guard self.advanceToUnoccupied() else { return nil } + assert(!self.isOccupied) + let start = self.currentBucket + self.advanceToOccupied(maximumCount: maximumCount) + let end = self.currentBucket + return Range(uncheckedBounds: (start, end)) + } } #endif diff --git a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Index.swift b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Index.swift new file mode 100644 index 00000000..2b322592 --- /dev/null +++ b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Index.swift @@ -0,0 +1,101 @@ +//===----------------------------------------------------------------------===// +// +// 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 !COLLECTIONS_SINGLE_MODULE +import ContainersPreview +#endif + +#if compiler(>=6.3) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS + +@available(SwiftStdlib 5.0, *) +extension RigidDictionary where Key: ~Copyable, Value: ~Copyable { + public typealias Index = RigidSet.Index + + @inlinable + @inline(__always) + public var startIndex: Index { + _keys.startIndex + } + + @inlinable + @inline(__always) + public var endIndex: Index { + _keys.endIndex + } + + @inlinable + @inline(__always) + public func index(forKey key: borrowing Key) -> Index? { + _keys.index(of: key) + } + + @inlinable + @inline(__always) + public func index(after index: Index) -> Index { + _keys.index(after: index) + } + + @_alwaysEmitIntoClient + @_transparent + package func _isOccupied(_ bucket: _Bucket) -> Bool { + _keys._isOccupied(bucket) + } + + @_alwaysEmitIntoClient + @_transparent + package func _checkItemIndex(_ index: Index) -> Void { + _keys._checkItemIndex(index) + } +} + +@available(SwiftStdlib 5.0, *) +extension RigidDictionary where Key: ~Copyable, Value: ~Copyable { + @frozen + public struct Element: ~Copyable, ~Escapable { + // FIXME: See if using (the real) `struct Borrow` would make sense here. + @_alwaysEmitIntoClient + package var _key: UnsafePointer + + @_alwaysEmitIntoClient + package var _value: UnsafePointer + + @_alwaysEmitIntoClient + @_lifetime(borrow _base) + package init( + _base: borrowing RigidDictionary, + bucket: _Bucket + ) { + assert(_base._isOccupied(bucket)) + self._key = .init(_base._keyPtr(at: bucket)) + self._value = .init(_base._valuePtr(at: bucket)) + } + + @_alwaysEmitIntoClient + public var key: Key { + // FIXME: This should be a borrow accessor + unsafeAddress { _key } + } + + @_alwaysEmitIntoClient + public var value: Value { + // FIXME: This should be a borrow accessor + unsafeAddress { _value } + } + } + + @_alwaysEmitIntoClient + public subscript(index: Index) -> Element { + _checkItemIndex(index) + return Element(_base: self, bucket: index._bucket) + } +} + +#endif diff --git a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Indices.swift b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Indices.swift new file mode 100644 index 00000000..b5c1df07 --- /dev/null +++ b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Indices.swift @@ -0,0 +1,88 @@ +//===----------------------------------------------------------------------===// +// +// 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.3) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS + +@available(SwiftStdlib 5.0, *) +extension RigidDictionary where Key: ~Copyable, Value: ~Copyable { + @frozen + public struct Indices: ~Escapable { + @_alwaysEmitIntoClient + package let _base: Borrow<_HTable> + + @_alwaysEmitIntoClient + @_lifetime(borrow _base) + package init(_base: borrowing @_addressable _HTable) { + self._base = Borrow(_base) + } + } + + @inlinable + public var indices: Indices { + @_lifetime(borrow self) + get { + Indices(_base: self._keys._table) + } + } +} + +@available(SwiftStdlib 6.2, *) +extension RigidDictionary.Indices: BorrowingSequence +where Key: ~Copyable, Value: ~Copyable +{ + public typealias Element = RigidDictionary.Index + + @frozen + public struct BorrowingIterator: ~Escapable { + public typealias Element = RigidDictionary.Index + + @usableFromInline + package var _it: _HTable.BucketIterator + + @usableFromInline + package var _current: InlineArray<1, Element> + + @inlinable + @_lifetime(copy _it) + package init(_it: consuming _HTable.BucketIterator) { + self._it = _it + self._current = InlineArray { target in + target.append(Element(_bucket: _HTable.Bucket(offset: 0))) + } + } + } + + @inlinable + public var estimatedCount: EstimatedCount { + .exactly(self._base.value.count) + } + + @_lifetime(copy self) + public func makeBorrowingIterator() -> BorrowingIterator { + let bit = self._base.value.makeBucketIterator() + // FIXME: This override really should not be necessary. Check if the real `struct Borrow` fixes it. + let override = _overrideLifetime(bit, copying: self) + return BorrowingIterator(_it: override) + } +} + +@available(SwiftStdlib 6.2, *) +extension RigidDictionary.Indices.BorrowingIterator: BorrowingIteratorProtocol +where Key: ~Copyable, Value: ~Copyable { + @_lifetime(&self) + public mutating func nextSpan(maximumCount: Int) -> Span { + guard _it.advanceToOccupied() else { return .init() } + _current[0] = Element(_bucket: _it.currentBucket) + _it.advanceToNextBit() + return _current.span + } +} +#endif diff --git a/Sources/BasicContainers/RigidDictionary/RigidDictionary.swift b/Sources/BasicContainers/RigidDictionary/RigidDictionary.swift index 1d772cd0..a06ffff2 100644 --- a/Sources/BasicContainers/RigidDictionary/RigidDictionary.swift +++ b/Sources/BasicContainers/RigidDictionary/RigidDictionary.swift @@ -17,6 +17,7 @@ import ContainersPreview #if compiler(>=6.2) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS @available(SwiftStdlib 5.0, *) @frozen +@_addressableForDependencies public struct RigidDictionary< Key: GeneralizedHashable & ~Copyable, Value: ~Copyable diff --git a/Sources/BasicContainers/RigidSet/RigidSet+Index.swift b/Sources/BasicContainers/RigidSet/RigidSet+Index.swift new file mode 100644 index 00000000..f7a41949 --- /dev/null +++ b/Sources/BasicContainers/RigidSet/RigidSet+Index.swift @@ -0,0 +1,109 @@ +//===----------------------------------------------------------------------===// +// +// 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 !COLLECTIONS_SINGLE_MODULE +import ContainersPreview +#endif + +#if compiler(>=6.3) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS + +@available(SwiftStdlib 5.0, *) +extension RigidSet where Element: ~Copyable { + @frozen + public struct Index: Equatable { + @_alwaysEmitIntoClient + package var _bucket: _HTable.Bucket + + @_alwaysEmitIntoClient + @_transparent + package init(_bucket: _HTable.Bucket) { + self._bucket = _bucket + } + + @_alwaysEmitIntoClient + @_transparent + package init(_offset: UInt) { + self._bucket = _HTable.Bucket(offset: _offset) + } + + @_alwaysEmitIntoClient + @_transparent + package init(_offset: Int) { + self._bucket = _HTable.Bucket(offset: _offset) + } + + @_alwaysEmitIntoClient + @_transparent + package var _offset: UInt { _bucket._offset } + + @_alwaysEmitIntoClient + public static func ==(left: Self, right: Self) -> Bool { + left._bucket == right._bucket + } + } + + @inlinable + public var startIndex: Index { + if _table.isSmall { return Index(_bucket: _Bucket(offset: 0)) } + guard let b = _table.bitmap.firstOccupiedBucket(from: _Bucket(offset: 0)) + else { return endIndex } + return Index(_bucket: b) + } + + @inlinable + public var endIndex: Index { + if _table.isSmall { return Index(_offset: count) } + return Index(_bucket: _table.endBucket) + } + + @_alwaysEmitIntoClient + @_transparent + package func _isOccupied(_ bucket: _Bucket) -> Bool { + _table.isValid(bucket) && _table.isOccupied(bucket) + } + + @_alwaysEmitIntoClient + @_transparent + package func _checkItemIndex(_ index: Index) -> Void { + precondition(_isOccupied(index._bucket), "Index out of bounds") + } + + @inlinable + public func index(of member: borrowing Element) -> Index? { + guard let bucket = self._find(member).bucket else { return nil } + return Index(_bucket: bucket) + } + + @inlinable + public func index(after index: Index) -> Index { + _checkItemIndex(index) + if _table.isSmall { + return Index(_bucket: _Bucket(offset: index._bucket._offset &+ 1)) + } + var start = index._bucket + start._offset &+= 1 + guard start < _table.endBucket else { return Index(_bucket: start) } + guard let b = _table.bitmap.firstOccupiedBucket(from: start) + else { return endIndex } + return Index(_bucket: b) + } + + @inlinable + public subscript(index: Index) -> Element { + // FIXME: Use borrow accessor here + unsafeAddress { + _checkItemIndex(index) + return .init(_memberPtr(at: index._bucket)) + } + } +} + +#endif diff --git a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Index.swift b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Index.swift new file mode 100644 index 00000000..3c2550ce --- /dev/null +++ b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Index.swift @@ -0,0 +1,69 @@ +//===----------------------------------------------------------------------===// +// +// 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 !COLLECTIONS_SINGLE_MODULE +import ContainersPreview +#endif + +#if compiler(>=6.3) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS + +@available(SwiftStdlib 5.0, *) +extension UniqueDictionary where Key: ~Copyable, Value: ~Copyable { + public typealias Index = RigidSet.Index + + @inlinable + @inline(__always) + public var startIndex: Index { + _storage._keys.startIndex + } + + @inlinable + @inline(__always) + public var endIndex: Index { + _storage._keys.endIndex + } + + @inlinable + @inline(__always) + public func index(forKey key: borrowing Key) -> Index? { + _storage._keys.index(of: key) + } + + @inlinable + @inline(__always) + public func index(after index: Index) -> Index { + _storage._keys.index(after: index) + } + + @_alwaysEmitIntoClient + @_transparent + package func _isOccupied(_ bucket: _Bucket) -> Bool { + _storage._keys._isOccupied(bucket) + } + + @_alwaysEmitIntoClient + @_transparent + package func _checkItemIndex(_ index: Index) -> Void { + _storage._keys._checkItemIndex(index) + } +} + +@available(SwiftStdlib 5.0, *) +extension UniqueDictionary where Key: ~Copyable, Value: ~Copyable { + public typealias Element = RigidDictionary.Element + + @_alwaysEmitIntoClient + public subscript(index: Index) -> Element { + _storage[index] + } +} + +#endif diff --git a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Indices.swift b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Indices.swift new file mode 100644 index 00000000..e0522775 --- /dev/null +++ b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Indices.swift @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// 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.3) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS + +@available(SwiftStdlib 5.0, *) +extension UniqueDictionary where Key: ~Copyable, Value: ~Copyable { + public typealias Indices = RigidDictionary.Indices + + @inlinable + public var indices: Indices { + @_lifetime(borrow self) + get { + _storage.indices + } + } +} + +#endif diff --git a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Lookup.swift b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Lookup.swift index 0ad74d57..8e8e3aaa 100644 --- a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Lookup.swift +++ b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Lookup.swift @@ -28,7 +28,8 @@ extension UniqueDictionary where Key: ~Copyable, Value: ~Copyable { public func value( forKey key: borrowing Key ) -> Borrow? { - _storage.value(forKey: key) + // FIXME: Why is this override necessary? Is it sound? It was triggered by RigidDictionary becoming `@_addressableForDependencies`. + _overrideLifetime(_storage.value(forKey: key), borrowing: self) } #endif diff --git a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary.swift b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary.swift index 2d098a2a..45d51114 100644 --- a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary.swift +++ b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary.swift @@ -13,6 +13,7 @@ @available(SwiftStdlib 5.0, *) @frozen +@_addressableForDependencies public struct UniqueDictionary< Key: GeneralizedHashable & ~Copyable, Value: ~Copyable diff --git a/Sources/BasicContainers/UniqueSet/UniqueSet+Index.swift b/Sources/BasicContainers/UniqueSet/UniqueSet+Index.swift new file mode 100644 index 00000000..e31b8acc --- /dev/null +++ b/Sources/BasicContainers/UniqueSet/UniqueSet+Index.swift @@ -0,0 +1,68 @@ +//===----------------------------------------------------------------------===// +// +// 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 !COLLECTIONS_SINGLE_MODULE +import ContainersPreview +#endif + +#if compiler(>=6.3) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS + +@available(SwiftStdlib 5.0, *) +extension UniqueSet where Element: ~Copyable { + public typealias Index = RigidSet.Index + + @inlinable + @inline(__always) + public var startIndex: Index { + _storage.startIndex + } + + @inlinable + @inline(__always) + public var endIndex: Index { + _storage.endIndex + } + + @_alwaysEmitIntoClient + @_transparent + package func _isOccupied(_ bucket: _Bucket) -> Bool { + _storage._isOccupied(bucket) + } + + @_alwaysEmitIntoClient + @_transparent + package func _checkItemIndex(_ index: Index) -> Void { + _storage._checkItemIndex(index) + } + + @inlinable + @inline(__always) + public func index(of member: borrowing Element) -> Index? { + _storage.index(of: member) + } + + @inlinable + @inline(__always) + public func index(after index: Index) -> Index { + _storage.index(after: index) + } + + @inlinable + public subscript(index: Index) -> Element { + // FIXME: Use borrow accessor here + unsafeAddress { + _checkItemIndex(index) + return .init(_storage._memberPtr(at: index._bucket)) + } + } +} + +#endif diff --git a/Tests/BasicContainersTests/RigidDictionaryTests.swift b/Tests/BasicContainersTests/RigidDictionaryTests.swift index 69cbcd5e..6de7905f 100644 --- a/Tests/BasicContainersTests/RigidDictionaryTests.swift +++ b/Tests/BasicContainersTests/RigidDictionaryTests.swift @@ -279,6 +279,69 @@ class RigidDictionaryTests: CollectionTestCase { } } + func test_iteration_indexAfter() { + typealias Key = LifetimeTracked + typealias Value = LifetimeTracked + withEvery("capacity", in: [1, 2, 10, 100, 200]) { capacity in + withLifetimeTracking { tracker in + var d = RigidDictionary(capacity: capacity) + withEvery("payload", in: 0 ..< capacity) { payload in + let key = tracker.instance(for: payload) + let value = tracker.instance(for: "\(payload)") + d.insertValue(value, forKey: key) + + var seen: Set = [] + + var i = d.startIndex + while i != d.endIndex { + let item = d[i] + let payload = item.key.payload + expectEqual(item.value.payload, "\(payload)") + expectTrue(seen.insert(payload).inserted, "Duplicate item \(payload)") + i = d.index(after: i) + } + expectEqual(seen.count, payload + 1) + } + } + } + } + + @available(SwiftStdlib 6.2, *) + func test_iteration_indices() { + typealias Key = LifetimeTracked + typealias Value = LifetimeTracked + withEvery("capacity", in: [1, 2, 10, 100, 200]) { capacity in + withLifetimeTracking { tracker in + var d = RigidDictionary(capacity: capacity) + withEvery("payload", in: 0 ..< capacity) { payload in + let key = tracker.instance(for: payload) + let value = tracker.instance(for: "\(payload)") + d.insertValue(value, forKey: key) + + var seen: Set = [] + let indices = d.indices + var it = indices.makeBorrowingIterator() + while true { + let next = it.nextSpan() + if next.isEmpty { break } + expectEqual(next.count, 1) + var i = 0 + while i < next.count { + let index = next[i] + let item = d[index] + expectTrue( + seen.insert(item.key.payload).inserted, + "Duplicate item \(item.key.payload)") + expectEqual(item.value.payload, "\(item.key.payload)") + i += 1 + } + } + expectEqual(seen.count, payload + 1) + } + } + } + } + func test_removeValueForKey_one() { typealias Key = LifetimeTracked typealias Value = LifetimeTracked diff --git a/Tests/BasicContainersTests/RigidSetTests.swift b/Tests/BasicContainersTests/RigidSetTests.swift index cc8f9922..b944764a 100644 --- a/Tests/BasicContainersTests/RigidSetTests.swift +++ b/Tests/BasicContainersTests/RigidSetTests.swift @@ -198,7 +198,7 @@ class RigidSetTests: CollectionTestCase { } #if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW - func test_iterate() { + func test_borrowing_iterator() { withEvery("capacity", in: [0, 1, 2, 3, 4, 10, 100, 1000]) { capacity in withEvery("maximumCount", in: [1, 2, 3, Int.max]) { maximumCount in withLifetimeTracking { tracker in @@ -224,7 +224,28 @@ class RigidSetTests: CollectionTestCase { } } #endif - + + func test_iteration_indexAfter() { + withEvery("capacity", in: [0, 1, 2, 10, 100, 200]) { capacity in + withLifetimeTracking { tracker in + var s = RigidSet>(capacity: capacity) + withEvery("payload", in: 0 ..< capacity) { payload in + let item = tracker.instance(for: payload) + s.insert(item) + + var seen: Set = [] + var i = s.startIndex + while i != s.endIndex { + let payload = s[i].payload + expectTrue(seen.insert(payload).inserted, "Duplicate item \(payload)") + i = s.index(after: i) + } + expectEqual(seen.count, payload + 1) + } + } + } + } + func test_remove_one() { withEvery("count", in: [0, 1, 2, 4, 10, 100, 500]) { count in withEvery("item", in: 0 ..< count) { item in diff --git a/Tests/BasicContainersTests/UniqueDictionaryTests.swift b/Tests/BasicContainersTests/UniqueDictionaryTests.swift index 851ad1cf..cb2e9652 100644 --- a/Tests/BasicContainersTests/UniqueDictionaryTests.swift +++ b/Tests/BasicContainersTests/UniqueDictionaryTests.swift @@ -73,7 +73,7 @@ class UniqueDictionaryTests: CollectionTestCase { expectGreaterThanOrEqual(s.capacity, 1) } } - + func test_update_one() { typealias Key = LifetimeTracked typealias Value = LifetimeTracked @@ -106,7 +106,7 @@ class UniqueDictionaryTests: CollectionTestCase { expectGreaterThanOrEqual(s.capacity, 1) } } - + func test_init_minimumCapacity_growth() { // These are the storage capacities we expect to see for a UniqueSet // up to size 1000. @@ -127,8 +127,8 @@ class UniqueDictionaryTests: CollectionTestCase { } expectEqual(actual.sorted(), expected) } - - + + func test_withKeys() { typealias Key = LifetimeTracked typealias Value = LifetimeTracked @@ -167,6 +167,67 @@ class UniqueDictionaryTests: CollectionTestCase { } } + func test_iteration_indexAfter() { + let c = 1000 + typealias Key = LifetimeTracked + typealias Value = LifetimeTracked + withLifetimeTracking { tracker in + var d = UniqueDictionary() + withEvery("payload", in: 0 ..< c) { payload in + let key = tracker.instance(for: payload) + let value = tracker.instance(for: "\(payload)") + d.insertValue(value, forKey: key) + + var seen: Set = [] + + var i = d.startIndex + while i != d.endIndex { + let item = d[i] + let payload = item.key.payload + expectEqual(item.value.payload, "\(payload)") + expectTrue(seen.insert(payload).inserted, "Duplicate item \(payload)") + i = d.index(after: i) + } + expectEqual(seen.count, payload + 1) + } + } + } + + @available(SwiftStdlib 6.2, *) + func test_iteration_indices() { + typealias Key = LifetimeTracked + typealias Value = LifetimeTracked + let c = 200 + withLifetimeTracking { tracker in + var d = UniqueDictionary() + withEvery("payload", in: 0 ..< c) { payload in + let key = tracker.instance(for: payload) + let value = tracker.instance(for: "\(payload)") + d.insertValue(value, forKey: key) + + var seen: Set = [] + let indices = d.indices + var it = indices.makeBorrowingIterator() + while true { + let next = it.nextSpan() + if next.isEmpty { break } + expectEqual(next.count, 1) + var i = 0 + while i < next.count { + let index = next[i] + let item = d[index] + expectTrue( + seen.insert(item.key.payload).inserted, + "Duplicate item \(item.key.payload)") + expectEqual(item.value.payload, "\(item.key.payload)") + i += 1 + } + } + expectEqual(seen.count, payload + 1) + } + } + } + func test_removeValueForKey_one() { typealias Key = LifetimeTracked typealias Value = LifetimeTracked diff --git a/Tests/BasicContainersTests/UniqueSetTests.swift b/Tests/BasicContainersTests/UniqueSetTests.swift index f747dc5f..ee6fa397 100644 --- a/Tests/BasicContainersTests/UniqueSetTests.swift +++ b/Tests/BasicContainersTests/UniqueSetTests.swift @@ -133,6 +133,26 @@ class UniqueSetTests: CollectionTestCase { } } + func test_iteration_indexAfter() { + let c = 1000 + withLifetimeTracking { tracker in + var s = UniqueSet>() + withEvery("payload", in: 0 ..< c) { payload in + let item = tracker.instance(for: payload) + s.insert(item) + + var seen: Set = [] + var i = s.startIndex + while i != s.endIndex { + let payload = s[i].payload + expectTrue(seen.insert(payload).inserted, "Duplicate item \(payload)") + i = s.index(after: i) + } + expectEqual(seen.count, payload + 1) + } + } + } + func test_remove_one() { withEvery("cap", in: [0, 1, 2, 4, 10, 100, 500]) { cap in let scale = _HTable.minimumScale(forCapacity: cap) diff --git a/Xcode/Collections.xcodeproj/project.pbxproj b/Xcode/Collections.xcodeproj/project.pbxproj index 662a39dd..ac38dc23 100644 --- a/Xcode/Collections.xcodeproj/project.pbxproj +++ b/Xcode/Collections.xcodeproj/project.pbxproj @@ -511,6 +511,12 @@ 7DEB315C2F4E7A2500F9AF28 /* UniqueDictionary+Equatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEB315B2F4E7A2000F9AF28 /* UniqueDictionary+Equatable.swift */; }; 7DEB315E2F4E7B8700F9AF28 /* UniqueDictionary+Hashable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEB315D2F4E7B8200F9AF28 /* UniqueDictionary+Hashable.swift */; }; 7DEB31602F4E7CEF00F9AF28 /* UniqueDictionaryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEB315F2F4E7CEF00F9AF28 /* UniqueDictionaryTests.swift */; }; + 7DEB31622F4F98D700F9AF28 /* RigidDictionary+Indices.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEB31612F4F98CD00F9AF28 /* RigidDictionary+Indices.swift */; }; + 7DEB31642F5218AA00F9AF28 /* RigidSet+Index.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEB31632F5218A600F9AF28 /* RigidSet+Index.swift */; }; + 7DEB31662F52268400F9AF28 /* UniqueSet+Index.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEB31652F52268100F9AF28 /* UniqueSet+Index.swift */; }; + 7DEB31682F52271900F9AF28 /* RigidDictionary+Index.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEB31672F52271300F9AF28 /* RigidDictionary+Index.swift */; }; + 7DEB316A2F52284200F9AF28 /* UniqueDictionary+Index.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEB31692F52283C00F9AF28 /* UniqueDictionary+Index.swift */; }; + 7DEB316C2F522EAB00F9AF28 /* UniqueDictionary+Indices.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEB316B2F522EA300F9AF28 /* UniqueDictionary+Indices.swift */; }; 7DEBDB6E29CCE44A00ADC226 /* SortedCollectionAPIChecker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEBDB3029CCE43600ADC226 /* SortedCollectionAPIChecker.swift */; }; 7DEBDB6F29CCE44A00ADC226 /* MinimalMutableRangeReplaceableRandomAccessCollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEBDB3F29CCE43600ADC226 /* MinimalMutableRangeReplaceableRandomAccessCollection.swift */; }; 7DEBDB7029CCE44A00ADC226 /* ResettableValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEBDB4229CCE43600ADC226 /* ResettableValue.swift */; }; @@ -1108,6 +1114,12 @@ 7DEB315B2F4E7A2000F9AF28 /* UniqueDictionary+Equatable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueDictionary+Equatable.swift"; sourceTree = ""; }; 7DEB315D2F4E7B8200F9AF28 /* UniqueDictionary+Hashable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueDictionary+Hashable.swift"; sourceTree = ""; }; 7DEB315F2F4E7CEF00F9AF28 /* UniqueDictionaryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UniqueDictionaryTests.swift; sourceTree = ""; }; + 7DEB31612F4F98CD00F9AF28 /* RigidDictionary+Indices.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RigidDictionary+Indices.swift"; sourceTree = ""; }; + 7DEB31632F5218A600F9AF28 /* RigidSet+Index.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RigidSet+Index.swift"; sourceTree = ""; }; + 7DEB31652F52268100F9AF28 /* UniqueSet+Index.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueSet+Index.swift"; sourceTree = ""; }; + 7DEB31672F52271300F9AF28 /* RigidDictionary+Index.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RigidDictionary+Index.swift"; sourceTree = ""; }; + 7DEB31692F52283C00F9AF28 /* UniqueDictionary+Index.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueDictionary+Index.swift"; sourceTree = ""; }; + 7DEB316B2F522EA300F9AF28 /* UniqueDictionary+Indices.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueDictionary+Indices.swift"; sourceTree = ""; }; 7DEBDB2129CCE43600ADC226 /* Combinatorics.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Combinatorics.swift; sourceTree = ""; }; 7DEBDB2229CCE43600ADC226 /* TestContext.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestContext.swift; sourceTree = ""; }; 7DEBDB2329CCE43600ADC226 /* Assertions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Assertions.swift; sourceTree = ""; }; @@ -1284,6 +1296,7 @@ 7D479CBA2F46A71500A8CB59 /* UniqueSet+Debugging.swift */, 7DEB31532F4E75AF00F9AF28 /* UniqueSet+Equatable.swift */, 7DEB31552F4E75B800F9AF28 /* UniqueSet+Hashable.swift */, + 7DEB31652F52268100F9AF28 /* UniqueSet+Index.swift */, 7D479CD32F46DD9600A8CB59 /* UniqueSet+Initializers.swift */, 7D479CC82F46D21800A8CB59 /* UniqueSet+Insertions.swift */, 7D479CCA2F46D22B00A8CB59 /* UniqueSet+Lookup.swift */, @@ -1300,6 +1313,8 @@ 7D479CEE2F47F4E300A8CB59 /* UniqueDictionary+Consumption.swift */, 7DEB315B2F4E7A2000F9AF28 /* UniqueDictionary+Equatable.swift */, 7DEB315D2F4E7B8200F9AF28 /* UniqueDictionary+Hashable.swift */, + 7DEB31692F52283C00F9AF28 /* UniqueDictionary+Index.swift */, + 7DEB316B2F522EA300F9AF28 /* UniqueDictionary+Indices.swift */, 7D479CDA2F47CD8900A8CB59 /* UniqueDictionary+Initializers.swift */, 7D479CE22F47D63300A8CB59 /* UniqueDictionary+Insertions.swift */, 7D479CFA2F4D080500A8CB59 /* UniqueDictionary+Keys.swift */, @@ -1358,6 +1373,8 @@ 7D479CEC2F47F40700A8CB59 /* RigidDictionary+Consumption.swift */, 7DEB31572F4E761D00F9AF28 /* RigidDictionary+Equatable.swift */, 7DEB31592F4E78F200F9AF28 /* RigidDictionary+Hashable.swift */, + 7DEB31672F52271300F9AF28 /* RigidDictionary+Index.swift */, + 7DEB31612F4F98CD00F9AF28 /* RigidDictionary+Indices.swift */, 7D479CD82F47CBB400A8CB59 /* RigidDictionary+Initializers.swift */, 7D479CDE2F47D5C300A8CB59 /* RigidDictionary+Insertions.swift */, 7D479CF62F4D07F200A8CB59 /* RigidDictionary+Keys.swift */, @@ -1377,6 +1394,7 @@ 7D479CB82F46A6D700A8CB59 /* RigidSet+Debugging.swift */, 7D479CFE2F4E487700A8CB59 /* RigidSet+Equatable.swift */, 7DEB31512F4E720500F9AF28 /* RigidSet+Hashable.swift */, + 7DEB31632F5218A600F9AF28 /* RigidSet+Index.swift */, 7D479CD12F46DD8600A8CB59 /* RigidSet+Initializers.swift */, 7D479CBE2F46D09E00A8CB59 /* RigidSet+Insertions.swift */, 7D479CBC2F46D05000A8CB59 /* RigidSet+Lookup.swift */, @@ -2517,6 +2535,7 @@ 7DE920C429CA70F4004483EB /* BitSet+SetAlgebra isStrictSuperset.swift in Sources */, 7DE9216229CA70F4004483EB /* _AncestorHashSlots.swift in Sources */, 7DE9216B29CA70F4004483EB /* TreeDictionary+Codable.swift in Sources */, + 7DEB31682F52271900F9AF28 /* RigidDictionary+Index.swift in Sources */, 7DE9201A29CA70F3004483EB /* OrderedDictionary+Sendable.swift in Sources */, 7DE9203B29CA70F3004483EB /* OrderedSet+Partial SetAlgebra subtracting.swift in Sources */, 7DE9213A29CA70F4004483EB /* TreeSet.swift in Sources */, @@ -2633,6 +2652,7 @@ 7DE9217229CA70F4004483EB /* TreeDictionary+Sendable.swift in Sources */, 7DE9207729CA70F4004483EB /* Range+BigString.swift in Sources */, 7DE920DF29CA70F4004483EB /* Slice+Utilities.swift in Sources */, + 7DEB316A2F52284200F9AF28 /* UniqueDictionary+Index.swift in Sources */, 7DE9215729CA70F4004483EB /* _RawHashNode.swift in Sources */, 7DE9209529CA70F4004483EB /* RopeSummary.swift in Sources */, 7DE920C229CA70F4004483EB /* BitSet+Sorted Collection APIs.swift in Sources */, @@ -2687,6 +2707,7 @@ 7DE920CF29CA70F4004483EB /* BitSet+Initializers.swift in Sources */, 7DE9201D29CA70F3004483EB /* OrderedDictionary.swift in Sources */, 7DE920A529CA70F4004483EB /* Rope+Collection.swift in Sources */, + 7DEB31662F52268400F9AF28 /* UniqueSet+Index.swift in Sources */, 7D479CCD2F46D23900A8CB59 /* UniqueSet+BorrowingSequence.swift in Sources */, 7DE9203629CA70F3004483EB /* OrderedSet+ReserveCapacity.swift in Sources */, 7DE9208D29CA70F4004483EB /* Rope+Debugging.swift in Sources */, @@ -2783,6 +2804,7 @@ 7DE9201B29CA70F3004483EB /* OrderedDictionary+Descriptions.swift in Sources */, 7DE9213C29CA70F4004483EB /* TreeSet+Codable.swift in Sources */, 7DE9207129CA70F4004483EB /* BigString+Split.swift in Sources */, + 7DEB31642F5218AA00F9AF28 /* RigidSet+Index.swift in Sources */, 7DE9214F29CA70F4004483EB /* _Bucket.swift in Sources */, 7DE9208029CA70F4004483EB /* BigString+Hashing.swift in Sources */, 7D479CF52F49070400A8CB59 /* UniqueSet+Removals.swift in Sources */, @@ -2806,6 +2828,8 @@ 7DE9208729CA70F4004483EB /* BigString+Sequence.swift in Sources */, 7DE9202229CA70F3004483EB /* OrderedSet+Diffing.swift in Sources */, 7DE920CA29CA70F4004483EB /* BitSet+CustomStringConvertible.swift in Sources */, + 7DEB31622F4F98D700F9AF28 /* RigidDictionary+Indices.swift in Sources */, + 7DEB316C2F522EAB00F9AF28 /* UniqueDictionary+Indices.swift in Sources */, 7DE9212E29CA70F4004483EB /* TreeSet+SetAlgebra isStrictSuperset.swift in Sources */, 7DE920A829CA70F4004483EB /* String.Index+ABI.swift in Sources */, 7DE9214129CA70F4004483EB /* _HashNode+Initializers.swift in Sources */,