mirror of
https://github.com/apple/swift-collections
synced 2026-11-22 04:20:40 +00:00
The examples for `intersection(_ other: Range<Int>)` and `formIntersection(_ other: Range<Int>)` (on both `BitSet` and `BitSet.Counted`) showed the result of the corresponding `subtract` example instead: intersecting `[1, 2, 3, 4]` with `-10 ..< 3` yields `[1, 2]`, not `[3, 4]`.
94 lines
3.1 KiB
Swift
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 {
|
|
unsafe self._read { first in
|
|
unsafe other._read { second in
|
|
unsafe 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 [1, 2]
|
|
///
|
|
/// - 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))
|
|
}
|
|
}
|