Files
swift-collections/Sources/HashTreeCollections/TreeSet/TreeSet+Sequence.swift
T
Karoy Lorentey 424c8ece98 HashTreeCollections: Qualify internal type names with “Hash”
This makes them unique throughout the package, allowing this code to be built as a single, huge module.
2023-03-21 16:37:22 -07:00

56 lines
1.6 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 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
//
//===----------------------------------------------------------------------===//
extension TreeSet: Sequence {
/// An iterator over the members of a `TreeSet`.
@frozen
public struct Iterator: IteratorProtocol {
@usableFromInline
internal typealias _UnsafeHandle = _HashNode.UnsafeHandle
@usableFromInline
internal var _it: _HashTreeIterator
@inlinable
internal init(_root: _RawHashNode) {
_it = _HashTreeIterator(root: _root)
}
/// Advances to the next element and returns it, or `nil` if no next element
/// exists.
///
/// Once `nil` has been returned, all subsequent calls return `nil`.
///
/// - Complexity: O(1)
@inlinable
public mutating func next() -> Element? {
guard let (node, slot) = _it.next() else { return nil }
return _UnsafeHandle.read(node) { $0[item: slot].key }
}
}
/// Returns an iterator over the members of the set.
@inlinable
public func makeIterator() -> Iterator {
Iterator(_root: _root.raw)
}
@inlinable
public func _customContainsEquatableElement(_ element: Element) -> Bool? {
_root.containsKey(.top, element, _Hash(element))
}
}
#if swift(>=5.5)
extension TreeSet.Iterator: @unchecked Sendable
where Element: Sendable {}
#endif