Files
swift-collections/Sources/HashTreeCollections/TreeDictionary/TreeDictionary+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

76 lines
2.3 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2019 - 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 TreeDictionary: Sequence {
/// The element type of a dictionary: a tuple containing an individual
/// key-value pair.
public typealias Element = (key: Key, value: Value)
/// The type that allows iteration over a persistent dictionary's elements.
@frozen
public struct Iterator {
// Fixed-stack iterator for traversing a hash tree.
// The iterator performs a pre-order traversal, with items at a node visited
// before any items within children.
@usableFromInline
internal typealias _UnsafeHandle = _HashNode.UnsafeHandle
@usableFromInline
internal var _it: _HashTreeIterator
@inlinable
internal init(_root: _RawHashNode) {
self._it = _HashTreeIterator(root: _root)
}
}
/// A value less than or equal to the number of elements in the sequence,
/// calculated nondestructively.
///
/// - Complexity: O(1)
@inlinable
public var underestimatedCount: Int {
_root.count
}
/// Returns an iterator over the elements of this collection.
///
/// - Complexity: O(1)
@inlinable
public __consuming func makeIterator() -> Iterator {
return Iterator(_root: _root.raw)
}
}
#if swift(>=5.5)
extension TreeDictionary.Iterator: @unchecked Sendable
where Key: Sendable, Value: Sendable {}
#endif
extension TreeDictionary.Iterator: IteratorProtocol {
/// The element type of a dictionary: a tuple containing an individual
/// key-value pair.
public typealias Element = (key: Key, value: Value)
/// 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] }
}
}