Files
swift-collections/Sources/SortedCollections/SortedSet/SortedSet.Index.swift
T
Karoy LorenteyandClaude Sonnet 4.6 64881e9cb4 [SortedCollections] SortedSet: Add firstIndex(after:) and lastIndex(before:)
The Range<Element> subscript covers half-open intervals, and with
partial-range support it would compose into ceiling/floor/lower (first
>= x, last <= x, last < x).  The strict upper bound "first element > x"
(Java's higher(_:)) can't be expressed as a range for arbitrary
Comparable types because Comparable has no successor operation, so
expose it — together with the symmetric strict lower bound — as
first-class operations.

Both are built on _BTree.startIndex(forKey:), which already returns the
first element >= the given key:
  - firstIndex(after: x) advances past that index if the element there
    equals x.
  - lastIndex(before: x) steps back one index, or returns nil if the
    lookup landed at startIndex.

Add test_firstIndexAfter and test_lastIndexBefore, covering queries
that hit an element, queries that fall between elements, and out-of-
range queries on both ends.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-01 17:59:18 -07:00

95 lines
2.8 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 COLLECTIONS_UNSTABLE_SORTED_COLLECTIONS
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
}
}
/// Returns the index of the smallest element that is strictly greater than
/// the given element, or `nil` if no such element exists.
///
/// The element itself does not need to be a member of the set.
///
/// - Parameter element: The element to look after.
/// - Complexity: O(`log n`)
@inlinable
public func firstIndex(after element: Element) -> Index? {
let i = self._root.startIndex(forKey: element)
if i == self._root.endIndex { return nil }
if self._root[i].key == element {
let next = self._root.index(after: i)
return next == self._root.endIndex ? nil : Index(next)
}
return Index(i)
}
/// Returns the index of the largest element that is strictly less than the
/// given element, or `nil` if no such element exists.
///
/// The element itself does not need to be a member of the set.
///
/// - Parameter element: The element to look before.
/// - Complexity: O(`log n`)
@inlinable
public func lastIndex(before element: Element) -> Index? {
let i = self._root.startIndex(forKey: element)
if i == self._root.startIndex { return nil }
return Index(self._root.index(before: i))
}
/// 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
}
}
}
extension SortedSet.Index: @unchecked Sendable
where Element: Sendable {}
// 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
}
}
#endif