Files
swift-collections/Sources/BitCollections/Shared/UInt+Tricks.swift
T
Karoy Lorentey 35d5746117 Rename _CollectionsUtilities to InternalCollectionsUtilities
We are forced to do this due to an Xcode issue (#364) with underscored module names.

This module remains unstable, despite the removal of the underscore. Directly importing it from outside the package remains unsupported. This module and/or its entire contents may go away or change in incompatible ways in any future swift-collections release.
2024-05-29 14:00:13 -07:00

37 lines
1.2 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 - 2024 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
//
//===----------------------------------------------------------------------===//
#if !COLLECTIONS_SINGLE_MODULE
import InternalCollectionsUtilities
#endif
extension UInt {
/// Returns the position of the `n`th set bit in `self`.
///
/// - Parameter n: The to retrieve. This value is
/// decremented by the number of items found in this `self` towards the
/// value we're looking for. (If the function returns non-nil, then `n`
/// is set to `0` on return.)
/// - Returns: If this integer contains enough set bits to satisfy the
/// request, then this function returns the position of the bit found.
/// Otherwise it returns nil.
internal func _rank(ofBit n: inout UInt) -> UInt? {
let c = self.nonzeroBitCount
guard n < c else {
n &-= UInt(bitPattern: c)
return nil
}
let m = Int(bitPattern: n)
n = 0
return _bit(ranked: m)!
}
}