Files
swift-collections/Sources/BitCollections/BitSet/BitSet+SetAlgebra basics.swift
T
Anton Karpov c2b2092f94 Name the documented arguments the way the declarations name them
Twenty-five doc comments in the shipping modules name an argument that the
declaration below them does not have, so DocC has nothing to bind the
description to and drops it. Nothing warns about this.

Most of them are renames that the comment did not follow, and the file itself
usually holds the proof: RigidDeque.prepend says - maxCount while its own
Complexity line already says newItemCount, and append(copying:) has three
overloads where the oldest still takes newElements and the two newer ones take
items with the older one's wording.

Three entries name an argument that no longer exists at all and are removed
rather than renamed.
2026-08-13 16:29:06 +03:00

138 lines
4.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_SINGLE_MODULE
import InternalCollectionsUtilities
#endif
extension BitSet {
/// Returns a Boolean value that indicates whether the given element exists
/// in the set.
///
/// - Parameter member: An element to look for in the set.
///
/// - Returns: `true` if `member` exists in the set; otherwise, `false`.
///
/// - Complexity: O(1)
@usableFromInline
internal func _contains(_ member: UInt) -> Bool {
_read { $0.contains(member) }
}
/// Returns a Boolean value that indicates whether the given element exists
/// in the set.
///
/// - Parameter member: An element to look for in the set.
///
/// - Returns: `true` if `member` exists in the set; otherwise, `false`.
///
/// - Complexity: O(1)
public func contains(_ member: Int) -> Bool {
guard let member = UInt(exactly: member) else { return false }
return _contains(member)
}
}
extension BitSet {
/// Insert the given element in the set if it is not already present.
///
/// If an element equal to `newMember` is already contained in the set, this
/// method has no effect.
///
/// If `newMember` was not already a member, it gets inserted into the set.
///
/// - Parameter newMember: An element to insert into the set.
///
/// - Returns: True if `newMember` was not contained in the
/// set, false otherwise.
///
/// - Complexity: O(1) if the set is a unique value (with no other copies),
/// and the inserted value is not greater than the largest value currently
/// contained in the set (named *max*). Otherwise the complexity is
/// O(max(`newMember`, *max*)).
@discardableResult
@usableFromInline
internal mutating func _insert(_ newMember: UInt) -> Bool {
_ensureCapacity(forValue: newMember)
return _update { $0.insert(newMember) }
}
/// Inserts the given element in the set if it is not already present.
///
/// If an element equal to `newMember` is already contained in the set, this
/// method has no effect.
///
/// If `newMember` was not already a member, it gets inserted into the set.
///
/// - Parameter newMember: An element to insert into the set.
///
/// - Returns: `(true, newMember)` if `newMember` was not contained in the
/// set. If `newMember` was already contained in the set, the method
/// returns `(false, newMember)`.
///
/// - Complexity: O(1) if the set is a unique value (with no other copies),
/// and the inserted value is not greater than the largest value currently
/// contained in the set (named *max*). Otherwise the complexity is
/// O(max(`newMember`, *max*)).
@discardableResult
public mutating func insert(
_ newMember: Int
) -> (inserted: Bool, memberAfterInsert: Int) {
guard let i = UInt(exactly: newMember) else {
preconditionFailure("Value out of range")
}
return (_insert(i), newMember)
}
/// Inserts the given element into the set unconditionally.
///
/// - Parameter newMember: An element to insert into the set.
///
/// - Returns: `newMember` if the set already contained it; otherwise, `nil`.
///
/// - Complexity: O(1) if the set is a unique value (with no live copies),
/// and the inserted value is not greater than the largest value currently
/// contained in the set (named *max*). Otherwise the complexity is
/// O(max(`newMember`, *max*)).
@discardableResult
public mutating func update(with newMember: Int) -> Int? {
insert(newMember).inserted ? nil : newMember
}
}
extension BitSet {
@discardableResult
@usableFromInline
internal mutating func _remove(_ member: UInt) -> Bool {
_updateThenShrink { handle, shrink in
shrink = handle.remove(member)
return shrink
}
}
/// Removes the given element from the set.
///
/// - Parameter member: The element of the set to remove.
///
/// - Returns: `member` if it was contained in the set; otherwise, `nil`.
///
/// - Complexity: O(`1`) if the set is a unique value (with no live copies),
/// and the removed value is less than the largest value currently in the
/// set (named *max*). Otherwise the complexity is at worst O(*max*).
@discardableResult
public mutating func remove(_ member: Int) -> Int? {
guard let m = UInt(exactly: member) else { return nil }
return _remove(m) ? member : nil
}
}