diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Descriptions.swift b/Sources/BasicContainers/RigidArray/RigidArray+Descriptions.swift new file mode 100644 index 00000000..f237b812 --- /dev/null +++ b/Sources/BasicContainers/RigidArray/RigidArray+Descriptions.swift @@ -0,0 +1,30 @@ +//===----------------------------------------------------------------------===// +// +// 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) + +@available(SwiftStdlib 5.0, *) +extension RigidArray /*: CustomStringConvertible */ where Element: ~Copyable { + public var description: String { + /// FIXME: Print the item descriptions when available. + "<\(count) items>" + } +} + +@available(SwiftStdlib 5.0, *) +extension RigidArray /*: CustomDebugStringConvertible */ where Element: ~Copyable { + public var debugDescription: String { + /// FIXME: Print the item descriptions when available. + "<\(count) items>" + } +} + +#endif diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Equatable.swift b/Sources/BasicContainers/RigidArray/RigidArray+Equatable.swift new file mode 100644 index 00000000..53d836aa --- /dev/null +++ b/Sources/BasicContainers/RigidArray/RigidArray+Equatable.swift @@ -0,0 +1,38 @@ +//===----------------------------------------------------------------------===// +// +// 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) + +#if !COLLECTIONS_SINGLE_MODULE +import InternalCollectionsUtilities +#endif + +@available(SwiftStdlib 5.0, *) +extension RigidArray /*: Equatable */ where Element: Equatable /* & ~Copyable */ { + public func isTriviallyIdentical(to other: borrowing Self) -> Bool { + self._storage._isIdentical(to: other._storage) + && self._count == other._count + } +} + + +@available(SwiftStdlib 5.0, *) +extension RigidArray /*: Equatable */ where Element: Equatable /* & ~Copyable */ { + @inlinable + public static func ==( + left: borrowing Self, + right: borrowing Self + ) -> Bool { + left.span._elementsEqual(to: right.span) + } +} + +#endif diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Hashable.swift b/Sources/BasicContainers/RigidArray/RigidArray+Hashable.swift new file mode 100644 index 00000000..fa7d4b5f --- /dev/null +++ b/Sources/BasicContainers/RigidArray/RigidArray+Hashable.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.2) + +#if !COLLECTIONS_SINGLE_MODULE +import InternalCollectionsUtilities +#endif + +@available(SwiftStdlib 5.0, *) +extension RigidArray /*: Hashable */ where Element: Hashable /* & ~Copyable */ { + @inlinable + public func hash(into hasher: inout Hasher) { + hasher.combine(self.count) + self.span._hashContents(into: &hasher) + } +} + +#endif diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Descriptions.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Descriptions.swift new file mode 100644 index 00000000..bf815ce5 --- /dev/null +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Descriptions.swift @@ -0,0 +1,30 @@ +//===----------------------------------------------------------------------===// +// +// 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) + +@available(SwiftStdlib 5.0, *) +extension UniqueArray /*: CustomStringConvertible */ where Element: ~Copyable { + public var description: String { + /// FIXME: Print the item descriptions when available. + "<\(count) items>" + } +} + +@available(SwiftStdlib 5.0, *) +extension UniqueArray /*: CustomDebugStringConvertible */ where Element: ~Copyable { + public var debugDescription: String { + /// FIXME: Print the item descriptions when available. + "<\(count) items>" + } +} + +#endif diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Equatable.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Equatable.swift new file mode 100644 index 00000000..2c4753ae --- /dev/null +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Equatable.swift @@ -0,0 +1,29 @@ +//===----------------------------------------------------------------------===// +// +// 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) + +#if !COLLECTIONS_SINGLE_MODULE +import InternalCollectionsUtilities +#endif + +@available(SwiftStdlib 5.0, *) +extension UniqueArray /*: Equatable */ where Element: Equatable /* & ~Copyable */ { + @inlinable + public static func ==( + left: borrowing Self, + right: borrowing Self + ) -> Bool { + left.span._elementsEqual(to: right.span) + } +} + +#endif diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Hashable.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Hashable.swift new file mode 100644 index 00000000..ee93d30c --- /dev/null +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Hashable.swift @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// 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) + +@available(SwiftStdlib 5.0, *) +extension UniqueArray /*: Hashable */ where Element: Hashable /* & ~Copyable */ { + @inlinable + public func hash(into hasher: inout Hasher) { + hasher.combine(self.count) + let span = self.span + for i in 0 ..< count { + hasher.combine(span[unchecked: i]) + } + } +} + +#endif diff --git a/Sources/ContainersPreview/Protocols/BorrowingSequence+Utilities.swift b/Sources/ContainersPreview/Protocols/BorrowingSequence+Utilities.swift index 4032273b..1bd60bfd 100644 --- a/Sources/ContainersPreview/Protocols/BorrowingSequence+Utilities.swift +++ b/Sources/ContainersPreview/Protocols/BorrowingSequence+Utilities.swift @@ -36,7 +36,7 @@ extension BorrowingSequence where Self: ~Copyable & ~Escapable, Element: Copyabl } } -#if true +#if true // FIXME: rdar://150228920 @available(SwiftStdlib 5.0, *) extension BorrowingSequence where Self: ~Copyable & ~Escapable { @inlinable @@ -45,12 +45,10 @@ extension BorrowingSequence where Self: ~Copyable & ~Escapable { >( _ other: borrowing Other, ) -> Bool where Element: Equatable { - switch (self.estimatedCount, other.estimatedCount) { - case let (.exactly(a), .exactly(b)): - guard a == b else { return false } - default: - break + guard self.estimatedCount._mayBeEqual(to: other.estimatedCount) else { + return false } + var it1 = self.makeBorrowingIterator() var it2 = other.makeBorrowingIterator() while true { @@ -262,18 +260,6 @@ extension BorrowingSequence where Self: ~Copyable & ~Escapable { return result } } - -@available(SwiftStdlib 5.0, *) -extension BorrowingSequence where Self: ~Copyable & ~Escapable, Element: Equatable { - @inlinable - public func elementsEqual< - Other: BorrowingSequence & ~Copyable & ~Escapable - >( - _ other: borrowing Other, - ) -> Bool { - self.elementsEqual(other, by: ==) - } -} #endif #endif diff --git a/Sources/ContainersPreview/Protocols/BorrowingSequence.swift b/Sources/ContainersPreview/Protocols/BorrowingSequence.swift index b7581d25..16678a83 100644 --- a/Sources/ContainersPreview/Protocols/BorrowingSequence.swift +++ b/Sources/ContainersPreview/Protocols/BorrowingSequence.swift @@ -15,6 +15,20 @@ public enum EstimatedCount { case infinite case exactly(Int) case unknown + + @inlinable + package func _mayBeEqual(to other: EstimatedCount) -> Bool { + switch (self, other) { + case let (.exactly(a), .exactly(b)): + return a == b + case (.exactly(_), .infinite): + return false + case (.infinite, .exactly(_)): + return false + default: + return true + } + } } @available(SwiftStdlib 5.0, *) diff --git a/Sources/ContainersPreview/Protocols/ContainerAlgorithms.swift b/Sources/ContainersPreview/Protocols/ContainerAlgorithms.swift new file mode 100644 index 00000000..72fb0b41 --- /dev/null +++ b/Sources/ContainersPreview/Protocols/ContainerAlgorithms.swift @@ -0,0 +1,58 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2024 - 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_CONTAINERS_PREVIEW + +@available(SwiftStdlib 6.2, *) +extension Container where Self: ~Copyable & ~Escapable { + @_alwaysEmitIntoClient + internal func _spanwiseZip( + with other: borrowing Other, + by process: (Span, Span) throws(E) -> Bool + ) throws(E) -> (Index, Other.Index) { + var i = self.startIndex // End index of the current span in `self` + var j = other.startIndex // End index of the current span in `other` + var a: Span = .init() + var b: Span = .init() + var pi = i // Start index of the original span we're currently processing in `self` + var pj = j // Start index of the original span we're currently processing in `other` + var ca = 0 // Count of the original span we're currently processing in `self` + var cb = 0 // Count of the original span we're currently processing in `other` + loop: + while true { + if a.isEmpty { + pi = i + a = self.nextSpan(after: &i) + ca = a.count + } + if b.isEmpty { + pj = j + b = other.nextSpan(after: &j) + cb = b.count + } + if a.isEmpty || b.isEmpty { + return ( + (a.isEmpty ? i : self.index(pi, offsetBy: ca - a.count)), + (b.isEmpty ? j : other.index(pj, offsetBy: cb - b.count))) + } + + let c = Swift.min(a.count, b.count) + + guard try process(a._trim(first: c), b._trim(first: c)) else { + return ( + (a.isEmpty ? i : self.index(pi, offsetBy: c)), + (b.isEmpty ? j : other.index(pj, offsetBy: c))) + } + } + } +} + +#endif diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Descriptions.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Descriptions.swift new file mode 100644 index 00000000..5d7e53de --- /dev/null +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Descriptions.swift @@ -0,0 +1,30 @@ +//===----------------------------------------------------------------------===// +// +// 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) + +@available(SwiftStdlib 5.0, *) +extension RigidDeque /*: CustomStringConvertible */ where Element: ~Copyable { + public var description: String { + /// FIXME: Print the item descriptions when available. + "<\(count) items>" + } +} + +@available(SwiftStdlib 5.0, *) +extension RigidDeque /*: CustomDebugStringConvertible */ where Element: ~Copyable { + public var debugDescription: String { + /// FIXME: Print the item descriptions when available. + "<\(count) items>" + } +} + +#endif diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Equatable.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Equatable.swift new file mode 100644 index 00000000..747dcef1 --- /dev/null +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Equatable.swift @@ -0,0 +1,46 @@ +//===----------------------------------------------------------------------===// +// +// 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) + +#if !COLLECTIONS_SINGLE_MODULE +import InternalCollectionsUtilities +#endif + +@available(SwiftStdlib 5.0, *) +extension RigidDeque /*: Equatable */ where Element: Equatable /* & ~Copyable */ { + public func isTriviallyIdentical(to other: borrowing Self) -> Bool { + self._handle.isIdentical(to: other._handle) + } +} + +@available(SwiftStdlib 5.0, *) +extension RigidDeque /*: Equatable */ where Element: Equatable /* & ~Copyable */ { + @inlinable + public static func ==( + left: borrowing Self, + right: borrowing Self + ) -> Bool { + guard left.count == right.count else { return false } + guard !left.isTriviallyIdentical(to: right) else { return true } + +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW + return left.elementsEqual(right) +#else + for i in 0 ..< left.count { + guard left[i] == right[i] else { return false } + } + return true +#endif + } +} + +#endif diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Hashable.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Hashable.swift new file mode 100644 index 00000000..5600a2b7 --- /dev/null +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Hashable.swift @@ -0,0 +1,31 @@ +//===----------------------------------------------------------------------===// +// +// 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) + +#if !COLLECTIONS_SINGLE_MODULE +import InternalCollectionsUtilities +#endif + +@available(SwiftStdlib 5.0, *) +extension RigidDeque /*: Hashable */ where Element: Hashable /* & ~Copyable */ { + @inlinable + public func hash(into hasher: inout Hasher) { + hasher.combine(self.count) + let segments = self._handle.segments() + segments.first.span._hashContents(into: &hasher) + if let second = segments.second { + second.span._hashContents(into: &hasher) + } + } +} + +#endif diff --git a/Sources/DequeModule/UniqueDeque/UniqueDeque+Descriptions.swift b/Sources/DequeModule/UniqueDeque/UniqueDeque+Descriptions.swift new file mode 100644 index 00000000..5378d5ec --- /dev/null +++ b/Sources/DequeModule/UniqueDeque/UniqueDeque+Descriptions.swift @@ -0,0 +1,30 @@ +//===----------------------------------------------------------------------===// +// +// 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) + +@available(SwiftStdlib 5.0, *) +extension UniqueDeque /*: CustomStringConvertible */ where Element: ~Copyable { + public var description: String { + /// FIXME: Print the item descriptions when available. + "<\(count) items>" + } +} + +@available(SwiftStdlib 5.0, *) +extension UniqueDeque /*: CustomDebugStringConvertible */ where Element: ~Copyable { + public var debugDescription: String { + /// FIXME: Print the item descriptions when available. + "<\(count) items>" + } +} + +#endif diff --git a/Sources/DequeModule/UniqueDeque/UniqueDeque+Equatable.swift b/Sources/DequeModule/UniqueDeque/UniqueDeque+Equatable.swift new file mode 100644 index 00000000..7ee9fc88 --- /dev/null +++ b/Sources/DequeModule/UniqueDeque/UniqueDeque+Equatable.swift @@ -0,0 +1,29 @@ +//===----------------------------------------------------------------------===// +// +// 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) + +#if !COLLECTIONS_SINGLE_MODULE +import InternalCollectionsUtilities +#endif + +@available(SwiftStdlib 5.0, *) +extension UniqueDeque /*: Equatable */ where Element: Equatable /* & ~Copyable */ { + @inlinable + public static func ==( + left: borrowing Self, + right: borrowing Self + ) -> Bool { + left._storage == right._storage + } +} + +#endif diff --git a/Sources/DequeModule/UniqueDeque/UniqueDeque+Hashable.swift b/Sources/DequeModule/UniqueDeque/UniqueDeque+Hashable.swift new file mode 100644 index 00000000..25e0fccd --- /dev/null +++ b/Sources/DequeModule/UniqueDeque/UniqueDeque+Hashable.swift @@ -0,0 +1,22 @@ +//===----------------------------------------------------------------------===// +// +// 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) + +@available(SwiftStdlib 5.0, *) +extension UniqueDeque /*: Hashable */ where Element: Hashable /* & ~Copyable */ { + @inlinable + public func hash(into hasher: inout Hasher) { + self._storage.hash(into: &hasher) + } +} + +#endif diff --git a/Sources/DequeModule/_UnsafeDequeHandle.swift b/Sources/DequeModule/_UnsafeDequeHandle.swift index 4eb6bf20..f020dbaa 100644 --- a/Sources/DequeModule/_UnsafeDequeHandle.swift +++ b/Sources/DequeModule/_UnsafeDequeHandle.swift @@ -103,6 +103,16 @@ extension _UnsafeDequeHandle where Element: ~Copyable { } } +extension _UnsafeDequeHandle where Element: ~Copyable { + @_alwaysEmitIntoClient + @_transparent + internal func isIdentical(to other: borrowing Self) -> Bool { + self._buffer._isIdentical(to: other._buffer) + && self.count == other.count + && self.startSlot == other.startSlot + } +} + // MARK: Test initializer extension _UnsafeDequeHandle where Element: ~Copyable { diff --git a/Sources/InternalCollectionsUtilities/Span+Extras.swift b/Sources/InternalCollectionsUtilities/Span+Extras.swift index 0a94740c..a1e7473c 100644 --- a/Sources/InternalCollectionsUtilities/Span+Extras.swift +++ b/Sources/InternalCollectionsUtilities/Span+Extras.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -#if compiler(>=6.2) && COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW +#if compiler(>=6.2) @available(SwiftStdlib 5.0, *) extension Span where Element: ~Copyable { @@ -36,4 +36,38 @@ extension Span where Element: ~Copyable { } } +@available(SwiftStdlib 5.0, *) +extension Span where Element: Equatable /* & ~Copyable */ { + @_alwaysEmitIntoClient + package func _elementsEqual(to other: borrowing Self) -> Bool { + return self.withUnsafeBufferPointer { a in + other.withUnsafeBufferPointer { b in + guard a.count == b.count else { return false } + guard a.baseAddress != b.baseAddress else { return true } + var i = 0 + while i < self.count { + guard a[i] == b[i] else { return false } + i &+= 1 + } + return true + } + } + } +} + +@available(SwiftStdlib 5.0, *) +extension Span where Element: Hashable /* & ~Copyable */ { + @_alwaysEmitIntoClient + package func _hashContents(into hasher: inout Hasher) { + // Note: no discriminating combine call -- caller is expected to do that + // separately when needed. + var i = 0 + while i < self.count { + hasher.combine(self[unchecked: i]) + i &+= 1 + } + + } +} + #endif