Files
swift-collections/Sources/BitCollections/BitSet/BitSet+SetAlgebra intersection.swift
Karoy Lorentey 80878519f2 [doc] Fix most issues reported by docc
DocC keeps changing its link syntax. The latest iteration appears to have trouble dealing with `Self`-taking or `Self`-returning methods, so change our API to replace `Self` with a direct mention of the concrete type. ¯\\\_(ツ)\_/¯

That said, the new compiler also found a large number of mismatching parameter names, and that was really helpful.
2026-03-09 14:14:51 -07:00

94 lines
3.1 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 new bit set with the elements that are common to both this set
/// and the given set.
///
/// let a: BitSet = [1, 2, 3, 4]
/// let b: BitSet = [6, 4, 2, 0]
/// let c = a.intersection(b)
/// // c is now [2, 4]
///
/// - Parameter other: A bit set.
///
/// - Complexity: O(*max*), where *max* is the largest item in either set.
public func intersection(_ other: BitSet) -> BitSet {
self._read { first in
other._read { second in
Self(
_combining: (first, second),
includingTail: false,
using: { $0.intersection($1) })
}
}
}
/// Returns a new bit set with the elements that are common to both this set
/// and the given set.
///
/// let a: BitSet = [1, 2, 3, 4]
/// let b: BitSet.Counted = [6, 4, 2, 0]
/// let c = a.intersection(b)
/// // c is now [2, 4]
///
/// - Parameter other: A bit set.
///
/// - Complexity: O(*max*), where *max* is the largest item in either set.
public func intersection(_ other: BitSet.Counted) -> BitSet {
self.intersection(other._bits)
}
/// Returns a new bit set with the elements that are common to both this set
/// and the given range of integers.
///
/// let a: BitSet = [1, 2, 3, 4]
/// let c = a.intersection(-10 ..< 3)
/// // c is now [3, 4]
///
/// - Parameter other: A range of integers.
///
/// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`,
/// and *k* is the complexity of iterating over all elements in `other`.
public func intersection(_ other: Range<Int>) -> BitSet {
var result = self
result.formIntersection(other)
return result
}
/// Returns a new bit set with the elements that are common to both this set
/// and the given sequence.
///
/// let a: BitSet = [1, 2, 3, 4]
/// let b = [6, 4, 2, 0]
/// let c = a.intersection(b)
/// // c is now [2, 4]
///
/// - Parameter other: A sequence of integer values.
///
/// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`,
/// and *k* is the complexity of iterating over all elements in `other`.
@inlinable
public func intersection(_ other: __owned some Sequence<Int>) -> BitSet {
if let other = _specialize(other, for: Range<Int>.self) {
return intersection(other)
}
// Note: BitSet & BitSet.Counted are handled in the BitSet initializer below
return intersection(BitSet(_validMembersOf: other))
}
}