diff --git a/Sources/BasicContainers/HashTable/_HTable+Iteration.swift b/Sources/BasicContainers/HashTable/_HTable+Iteration.swift index 35474d57..6431d65b 100644 --- a/Sources/BasicContainers/HashTable/_HTable+Iteration.swift +++ b/Sources/BasicContainers/HashTable/_HTable+Iteration.swift @@ -76,10 +76,10 @@ extension _HTable { self._words = .init(bitmap) self._endBucket = table.endBucket self._nextBits = bitmap[_bucket.word].shiftedDown(by: _bucket.bit) - if table.endBucket._offset < _Word.capacity { + if table.endBucket._offset < Word.capacity { self._nextBitCount = UInt8(table.endBucket.bit &- _bucket.bit) } else { - self._nextBitCount = UInt8(_Word._capacity &- _bucket.bit) + self._nextBitCount = UInt8(Word._capacity &- _bucket.bit) } } else { self._words = nil @@ -114,17 +114,19 @@ extension _HTable.BucketIterator { @_alwaysEmitIntoClient @_transparent + @_lifetime(self: copy self) package mutating func restart() { _wrapped = true _bucket._offset = 0 if let words = _words { _nextBits = words[_bucket.word] - _nextBitCount = UInt8(Swift.min(_Word._capacity, _endBucket._offset)) + _nextBitCount = UInt8(Swift.min(Word._capacity, _endBucket._offset)) } } @_alwaysEmitIntoClient @_transparent + @_lifetime(self: copy self) package mutating func _wrap() { precondition(!_wrapped, "Corrupt hash table") restart() @@ -133,6 +135,7 @@ extension _HTable.BucketIterator { @_alwaysEmitIntoClient @discardableResult + @_lifetime(self: copy self) package mutating func _advanceToNextWord() -> Bool { assert(!isAtEnd) _bucket.advanceToNextWord() @@ -144,24 +147,26 @@ extension _HTable.BucketIterator { } if let words = _words{ _nextBits = words[_bucket.word] - _nextBitCount = UInt8(_Word._capacity) + _nextBitCount = UInt8(Word._capacity) } return true } @_alwaysEmitIntoClient + @_lifetime(self: copy self) package mutating func _wrapToNextWord() { _bucket.advanceToNextWord() if isAtEnd { _wrap() } else if let words = _words { _nextBits = words[_bucket.word] - _nextBitCount = UInt8(_Word._capacity) + _nextBitCount = UInt8(Word._capacity) } } @_alwaysEmitIntoClient @discardableResult + @_lifetime(self: copy self) package mutating func advanceToNextBit() -> Bool { assert(!isAtEnd) _bucket._offset &+= 1 @@ -178,12 +183,13 @@ extension _HTable.BucketIterator { } else { assert(_bucket.bit == 0) _nextBits = words[_bucket.word] - _nextBitCount = UInt8(_Word._capacity) + _nextBitCount = UInt8(Word._capacity) } return true } @_alwaysEmitIntoClient + @_lifetime(self: copy self) package mutating func wrapToNextBit() { _bucket._offset &+= 1 if isAtEnd { @@ -197,13 +203,14 @@ extension _HTable.BucketIterator { _nextBits = _nextBits.shiftedDown(by: 1) } else { _nextBits = words[_bucket.word] - _nextBitCount = UInt8(_Word._capacity) + _nextBitCount = UInt8(Word._capacity) } } /// If the current bucket is already occupied, this does nothing. @_alwaysEmitIntoClient @discardableResult + @_lifetime(self: copy self) package mutating func advanceToOccupied() -> Bool { if isAtEnd { return false } guard _words != nil else { @@ -222,6 +229,7 @@ extension _HTable.BucketIterator { } @_alwaysEmitIntoClient + @_lifetime(self: copy self) package mutating func wrapToOccupied() { if _words == nil { wrapToNextBit() @@ -241,6 +249,7 @@ extension _HTable.BucketIterator { /// If the current bucket is already unoccupied, this does nothing. @_alwaysEmitIntoClient @discardableResult + @_lifetime(self: copy self) package mutating func advanceToUnoccupied() -> Bool { assert(!isAtEnd) if _words == nil { @@ -263,6 +272,7 @@ extension _HTable.BucketIterator { /// If the current bucket is already unoccupied, this does nothing. @_alwaysEmitIntoClient @discardableResult + @_lifetime(self: copy self) package mutating func advanceToUnoccupied( maximumCount: Int = .max ) -> Bool { @@ -299,6 +309,7 @@ extension _HTable.BucketIterator { } @_alwaysEmitIntoClient + @_lifetime(self: copy self) package mutating func wrapToUnoccupied() { if _words == nil { _bucket = _endBucket @@ -316,6 +327,7 @@ extension _HTable.BucketIterator { } @_alwaysEmitIntoClient + @_lifetime(self: copy self) package mutating func nextOccupiedRegion( maximumCount: Int = .max ) -> Range? { diff --git a/Sources/BasicContainers/HashTable/_Htable+Debugging.swift b/Sources/BasicContainers/HashTable/_Htable+Debugging.swift index 6cb0ba76..4a4cb846 100644 --- a/Sources/BasicContainers/HashTable/_Htable+Debugging.swift +++ b/Sources/BasicContainers/HashTable/_Htable+Debugging.swift @@ -9,10 +9,14 @@ // //===----------------------------------------------------------------------===// +#if !COLLECTIONS_SINGLE_MODULE +import InternalCollectionsUtilities +#endif + #if compiler(>=6.2) extension _HTable { package var description: String { - let multiline = self.endBucket.offset > _Word.capacity + let multiline = self.endBucket.offset > Word.capacity var buckets = "" if !self.isSmall { diff --git a/Sources/BasicContainers/RigidDictionary/RigidDictionary.swift b/Sources/BasicContainers/RigidDictionary/RigidDictionary.swift index c50f93e3..34a235c0 100644 --- a/Sources/BasicContainers/RigidDictionary/RigidDictionary.swift +++ b/Sources/BasicContainers/RigidDictionary/RigidDictionary.swift @@ -126,6 +126,7 @@ extension RigidDictionary where Key: ~Copyable, Value: ~Copyable { } extension RigidDictionary where Key: ~Copyable, Value: ~Copyable { +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW @inlinable @_lifetime(borrow self) public func value( @@ -134,7 +135,21 @@ extension RigidDictionary where Key: ~Copyable, Value: ~Copyable { guard let bucket = _keys._find(key) else { return nil } return Borrow(unsafeAddress: _valuePtr(at: bucket), borrowing: self) } - +#endif + + /// A stand-in for a `struct Borrow`-returning lookup operation. + /// This is quite clumsy to use, but this is the best we can do without a way + /// to express optional borrows. + @_alwaysEmitIntoClient + @_transparent + public func withValue( + forKey key: borrowing Key, + _ body: (borrowing Value) throws(E) -> R? + ) throws(E) -> R? { + guard let bucket = _keys._find(key) else { return nil } + return try body(_valueBuf[bucket]) + } + @inlinable public mutating func insertValue( _ value: consuming Value, @@ -167,6 +182,7 @@ extension RigidDictionary where Key: ~Copyable, Value: ~Copyable { return nil } +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW @inlinable @_lifetime(&self) public mutating func memoizedValue( @@ -186,6 +202,7 @@ extension RigidDictionary where Key: ~Copyable, Value: ~Copyable { } return Borrow(unsafeAddress: p, borrowing: self) } +#endif } diff --git a/Sources/BasicContainers/RigidSet/RigidSet.swift b/Sources/BasicContainers/RigidSet/RigidSet.swift index 32e6a74e..a85f68b9 100644 --- a/Sources/BasicContainers/RigidSet/RigidSet.swift +++ b/Sources/BasicContainers/RigidSet/RigidSet.swift @@ -11,6 +11,7 @@ #if !COLLECTIONS_SINGLE_MODULE import InternalCollectionsUtilities +import ContainersPreview #endif #if compiler(>=6.3) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS @@ -109,7 +110,7 @@ extension RigidSet where Element: ~Copyable { #if COLLECTIONS_DETERMINISTIC_HASHING Int(_table.scale) #else - Int(bitPattern: _baseAddress) + Int(bitPattern: _members) #endif } @@ -252,7 +253,8 @@ extension RigidSet where Element: ~Copyable { } } -extension RigidSet where Element: ~Copyable { +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW +extension RigidSet: BorrowingSequence where Element: ~Copyable { @inlinable public var estimatedCount: EstimatedCount { .exactly(count) @@ -315,3 +317,4 @@ extension RigidSet where Element: ~Copyable { } } #endif +#endif diff --git a/Tests/BasicContainersTests/RigidDictionaryTests.swift b/Tests/BasicContainersTests/RigidDictionaryTests.swift index ffc0c447..6d21c96a 100644 --- a/Tests/BasicContainersTests/RigidDictionaryTests.swift +++ b/Tests/BasicContainersTests/RigidDictionaryTests.swift @@ -55,6 +55,7 @@ class RigidDictionaryTests: CollectionTestCase { d.insertValue(secondValue, forKey: secondKey), secondValue) +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW let thirdKey = tracker.instance(for: 42) expectNotNil(d.value(forKey: thirdKey)) { valueRef in expectIdentical(valueRef.value, firstValue) @@ -63,6 +64,7 @@ class RigidDictionaryTests: CollectionTestCase { expectNotNil(d.value(forKey: thirdKey)) { valueRef in expectEqual(valueRef.value.payload, "forty-two") } +#endif expectEqual(d.count, 1) expectFalse(d.isEmpty) @@ -86,6 +88,7 @@ class RigidDictionaryTests: CollectionTestCase { let res = d.updateValue(secondValue, forKey: secondKey) expectIdentical(res, firstValue) +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW let thirdKey = tracker.instance(for: 42) expectNotNil(d.value(forKey: thirdKey)) { valueRef in expectIdentical(valueRef.value, secondValue) @@ -94,7 +97,8 @@ class RigidDictionaryTests: CollectionTestCase { expectNotNil(d.value(forKey: thirdKey)) { valueRef in expectEqual(valueRef.value.payload, "forty-two") } - +#endif + expectEqual(d.count, 1) expectFalse(d.isEmpty) expectFalse(d.isFull) @@ -133,6 +137,7 @@ class RigidDictionaryTests: CollectionTestCase { expectEqual(d.capacity, capacity) expectEqual(d.freeCapacity, 0) +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW for i in 0 ..< capacity { let dupe = tracker.instance(for: i) expectNotNil( @@ -142,6 +147,7 @@ class RigidDictionaryTests: CollectionTestCase { expectEqual(valueRef.value.payload, "\(i)") } } +#endif } } } @@ -179,6 +185,7 @@ class RigidDictionaryTests: CollectionTestCase { expectEqual(d.capacity, capacity) expectEqual(d.freeCapacity, 0) +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW for i in 0 ..< capacity { let dupe = tracker.instance(for: i) expectNotNil( @@ -188,11 +195,13 @@ class RigidDictionaryTests: CollectionTestCase { expectEqual(valueRef.value.payload, "\(i)") } } +#endif } } } - func test_memoizedValue_ond() { +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW + func test_memoizedValue_one() { typealias Key = LifetimeTracked typealias Value = LifetimeTracked withLifetimeTracking { tracker in @@ -229,6 +238,7 @@ class RigidDictionaryTests: CollectionTestCase { expectEqual(d.freeCapacity, 19) } } +#endif } #endif diff --git a/Tests/BasicContainersTests/RigidSetTests.swift b/Tests/BasicContainersTests/RigidSetTests.swift index 2f91a3ff..9073da04 100644 --- a/Tests/BasicContainersTests/RigidSetTests.swift +++ b/Tests/BasicContainersTests/RigidSetTests.swift @@ -185,6 +185,7 @@ class RigidSetTests: CollectionTestCase { } } +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW func test_iterate() { withEvery("capacity", in: [0, 1, 2, 3, 4, 10, 100, 1000]) { capacity in withEvery("maximumCount", in: [1, 2, 3, Int.max]) { maximumCount in @@ -210,5 +211,6 @@ class RigidSetTests: CollectionTestCase { } } } +#endif } #endif diff --git a/Tests/_CollectionsTestSupport/AssertionContexts/Assertions.swift b/Tests/_CollectionsTestSupport/AssertionContexts/Assertions.swift index 5496c075..a149244e 100644 --- a/Tests/_CollectionsTestSupport/AssertionContexts/Assertions.swift +++ b/Tests/_CollectionsTestSupport/AssertionContexts/Assertions.swift @@ -74,6 +74,20 @@ public func expectFalse( message, trapping: trapping, file: file, line: line) } +#if compiler(>=6.2) +public func expectNil( + _ value: borrowing Optional, + _ message: @autoclosure () -> String = "", + trapping: Bool = false, + file: StaticString = #filePath, + line: UInt = #line +) { + if value == nil { return } + _expectFailure( + "value is not nil", + message, trapping: trapping, file: file, line: line) +} +#else public func expectNil( _ value: Optional, _ message: @autoclosure () -> String = "", @@ -86,7 +100,9 @@ public func expectNil( "'\(value!)' is not nil", message, trapping: trapping, file: file, line: line) } +#endif +#if compiler(>=6.2) public func expectNotNil( _ value: borrowing Optional, _ message: @autoclosure () -> String = "", @@ -99,7 +115,22 @@ public func expectNotNil( "value is nil", message, trapping: trapping, file: file, line: line) } +#else +public func expectNotNil( + _ value: Optional, + _ message: @autoclosure () -> String = "", + trapping: Bool = false, + file: StaticString = #filePath, + line: UInt = #line +) { + if value != nil { return } + _expectFailure( + "value is nil", + message, trapping: trapping, file: file, line: line) +} +#endif +#if compiler(>=6.2) public func expectNotNil( _ value: borrowing Optional, _ message: @autoclosure () -> String = "", @@ -117,6 +148,25 @@ public func expectNotNil( message, trapping: trapping, file: file, line: line) } } +#else +public func expectNotNil( + _ value: Optional, + _ message: @autoclosure () -> String = "", + trapping: Bool = false, + file: StaticString = #filePath, + line: UInt = #line, + _ handler: (borrowing T) throws -> Void = { _ in } +) rethrows { + switch value { + case let value?: + try handler(value) + case nil: + _expectFailure( + "value is nil", + message, trapping: trapping, file: file, line: line) + } +} +#endif public func expectIdentical( _ left: T?, _ right: T?,