mirror of
https://github.com/apple/swift-collections
synced 2026-11-24 04:27:53 +00:00
Key Changes: - Add duplicate handling functionality to _BTree.Builder - Remove transient allocation in SortedDictionary.values _modify - Add missing SortedDictionary.Values.subscript _modify - Add SortedSet(sortedElements:) - Add optimized SetAlgebra implementations - Remove remaining high-level offset APIs - Various optimizations - Expand internal documentation Minor Changes: - Add ability to create dummy node - _BTree.Builder.append(key: Key) for sets. - Fix bug where findAnyIndex(forKey:) didn't calculate the offset. - Remove old, unused files - findAnyCursor -> takeCursor (to emphasize consuming nature) - Add takeCursor(at: Index) - Optimize init(grouping:by:) to use modifyValue to reduce CoW Optimizations: - Use dummy node for transient _modify state rather than allocating new buffer
54 lines
1.4 KiB
Swift
54 lines
1.4 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Collections open source project
|
|
//
|
|
// Copyright (c) 2021 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 SortedSet {
|
|
/// Returns the index for a given element, if it exists
|
|
/// - Complexity: O(`log n`)
|
|
@inlinable
|
|
public func index(of element: Element) -> Index? {
|
|
if let index = self._root.findAnyIndex(forKey: element) {
|
|
return Index(index)
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
/// The position of an element within a sorted set
|
|
public struct Index {
|
|
@usableFromInline
|
|
internal var _index: _Tree.Index
|
|
|
|
@inlinable
|
|
@inline(__always)
|
|
internal init(_ _index: _Tree.Index) {
|
|
self._index = _index
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: Equatable
|
|
extension SortedSet.Index: Equatable {
|
|
@inlinable
|
|
public static func ==(lhs: SortedSet.Index, rhs: SortedSet.Index) -> Bool {
|
|
lhs._index.ensureValid(with: rhs._index)
|
|
return lhs._index == rhs._index
|
|
}
|
|
}
|
|
|
|
// MARK: Comparable
|
|
extension SortedSet.Index: Comparable {
|
|
@inlinable
|
|
public static func <(lhs: SortedSet.Index, rhs: SortedSet.Index) -> Bool {
|
|
lhs._index.ensureValid(with: rhs._index)
|
|
return lhs._index < rhs._index
|
|
}
|
|
}
|