mirror of
https://github.com/apple/swift-collections
synced 2026-11-23 04:24:23 +00:00
59 lines
1.5 KiB
Swift
59 lines
1.5 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
|
|
}
|
|
}
|
|
}
|
|
|
|
#if swift(>=5.5)
|
|
extension SortedSet.Index: @unchecked Sendable
|
|
where Element: Sendable {}
|
|
#endif
|
|
|
|
// 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
|
|
}
|
|
}
|