1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 - 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
//
//===----------------------------------------------------------------------===//
extension BitSet {
/// Returns a Boolean value that indicates whether the given element exists
/// in the set.
///
/// - Parameter element: 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 element: 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 ? newMember : nil
}
}
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
}
}
|