mirror of
https://github.com/apple/swift-collections
synced 2026-11-24 04:27:53 +00:00
Package traits already pass defines with their trait name when building. Instead of adding our own conditional defines based on the traits we can use the trait provided defines directly. I observed that various tooling behaves better with the trait provided defines.
63 lines
1.7 KiB
Swift
63 lines
1.7 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Collections open source project
|
|
//
|
|
// Copyright (c) 2021 - 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
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#if UnstableSortedCollections
|
|
|
|
extension SortedDictionary {
|
|
/// Returns the index for a given key, if it exists
|
|
/// - Complexity: O(`log n`)
|
|
@inlinable
|
|
public func index(forKey key: Key) -> Index? {
|
|
if let index = self._root.findAnyIndex(forKey: key) {
|
|
return Index(index)
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
/// The position of an element within a sorted dictionary
|
|
public struct Index {
|
|
@usableFromInline
|
|
internal var _index: _Tree.Index
|
|
|
|
@inlinable
|
|
@inline(__always)
|
|
internal init(_ _index: _Tree.Index) {
|
|
self._index = _index
|
|
}
|
|
}
|
|
}
|
|
|
|
extension SortedDictionary.Index: @unchecked Sendable
|
|
where Key: Sendable, Value: Sendable {}
|
|
|
|
// MARK: Equatable
|
|
extension SortedDictionary.Index: Equatable {
|
|
@inlinable
|
|
public static func ==(lhs: SortedDictionary.Index, rhs: SortedDictionary.Index) -> Bool {
|
|
lhs._index.ensureValid(with: rhs._index)
|
|
return lhs._index == rhs._index
|
|
}
|
|
}
|
|
|
|
// MARK: Comparable
|
|
extension SortedDictionary.Index: Comparable {
|
|
@inlinable
|
|
public static func <(lhs: SortedDictionary.Index, rhs: SortedDictionary.Index) -> Bool {
|
|
lhs._index.ensureValid(with: rhs._index)
|
|
return lhs._index < rhs._index
|
|
}
|
|
}
|
|
|
|
#endif
|