From d8d9c5ea2530192936da95156967dbc28c54b7c5 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Tue, 16 Aug 2022 19:01:11 -0700 Subject: [PATCH] [BitSet] Add documentation --- .../BitCollections/BitArray/BitArray.swift | 9 +++ .../BitSet+BidirectionalCollection.swift | 59 ++++++++++++++++++- .../BitSet/BitSet+Equatable.swift | 4 ++ .../BitSet+ExpressibleByArrayLiteral.swift | 10 ++++ .../BitSet/BitSet+Hashable.swift | 4 ++ .../BitSet/BitSet+Initializers.swift | 55 ++++++++++++++++- .../BitSet/BitSet+SetAlgebra basics.swift | 44 ++++++++++++++ .../BitSet+SetAlgebra formIntersection.swift | 30 ++++++++++ ...t+SetAlgebra formSymmetricDifference.swift | 34 +++++++++++ .../BitSet/BitSet+SetAlgebra formUnion.swift | 37 ++++++++++-- .../BitSet+SetAlgebra intersection.swift | 34 +++++++++++ .../BitSet/BitSet+SetAlgebra isDisjoint.swift | 41 ++++++++++++- .../BitSet+SetAlgebra isStrictSubset.swift | 58 ++++++++++++++++++ .../BitSet+SetAlgebra isStrictSuperset.swift | 54 ++++++++++++++++- .../BitSet/BitSet+SetAlgebra isSubset.swift | 52 ++++++++++++++++ .../BitSet/BitSet+SetAlgebra isSuperset.swift | 51 ++++++++++++++++ .../BitSet/BitSet+SetAlgebra subtract.swift | 32 ++++++++++ .../BitSet+SetAlgebra subtracting.swift | 36 +++++++++++ ...itSet+SetAlgebra symmetricDifference.swift | 35 +++++++++++ .../BitSet/BitSet+SetAlgebra union.swift | 30 ++++++++++ .../BitCollections/BitSet/BitSet.Index.swift | 17 ++++++ Sources/BitCollections/BitSet/BitSet.swift | 4 ++ 22 files changed, 717 insertions(+), 13 deletions(-) diff --git a/Sources/BitCollections/BitArray/BitArray.swift b/Sources/BitCollections/BitArray/BitArray.swift index 1f991ee5..47e24a26 100644 --- a/Sources/BitCollections/BitArray/BitArray.swift +++ b/Sources/BitCollections/BitArray/BitArray.swift @@ -9,6 +9,15 @@ // //===----------------------------------------------------------------------===// +/// An ordered, random-access collection of `Bool` values, implemented as an +/// uncompressed bitmap of as many bits as the count of the array. +/// +/// Bit arrays implement `RangeReplaceableCollection` and `MutableCollection` +/// and provide limited support for bitwise operations on same-sized arrays. +/// +/// See `BitSet` for an alternative form of the same underlying data +/// structure, treating it as a set of nonnegative integers corresponding to +/// `true` bits. public struct BitArray { @usableFromInline internal var _storage: [_Word] diff --git a/Sources/BitCollections/BitSet/BitSet+BidirectionalCollection.swift b/Sources/BitCollections/BitSet/BitSet+BidirectionalCollection.swift index 1d06dc73..0bea1677 100644 --- a/Sources/BitCollections/BitSet/BitSet+BidirectionalCollection.swift +++ b/Sources/BitCollections/BitSet/BitSet+BidirectionalCollection.swift @@ -10,14 +10,22 @@ //===----------------------------------------------------------------------===// extension BitSet: Sequence { + /// The type representing the bit set's elements. + /// Bit sets are collections of nonnegative integers. public typealias Element = Int + /// Returns the exact count of the bit set. + /// + /// - Complexity: O(1) @inlinable @inline(__always) public var underestimatedCount: Int { return count } + /// Returns an iterator over the elements of the bit set. + /// + /// - Complexity: O(1) @inlinable public func makeIterator() -> Iterator { return Iterator(self) @@ -30,6 +38,7 @@ extension BitSet: Sequence { return _contains(element) } + /// An iterator over the members of a bit set. public struct Iterator: IteratorProtocol { internal typealias _UnsafeHandle = BitSet._UnsafeHandle @@ -47,6 +56,17 @@ extension BitSet: Sequence { } } + /// Advances to the next element and returns it, or `nil` if no next element + /// exists. + /// + /// Once `nil` has been returned, all subsequent calls return `nil`. + /// + /// - Complexity: + /// Each individual call has a worst case time complexity of O(*n*), + /// where *n* is largest element in the set, as each call needs to + /// search for the next `true` bit in the underlying storage. + /// However, each storage bit is only visited once, so iterating over the + /// entire set has the same O(*n*) complexity. @_effects(releasenone) public mutating func next() -> Int? { if let bit = word.next() { @@ -69,14 +89,28 @@ extension BitSet: Sequence { } extension BitSet: Collection, BidirectionalCollection { + /// A Boolean value indicating whether the collection is empty. + /// + /// - Complexity: O(1) public var isEmpty: Bool { _count == 0 } + + /// The number of elements in the bit set. + /// + /// - Complexity: O(1) public var count: Int { _count } + /// The position of the first element in a nonempty set, or `endIndex` + /// if the collection is empty. + /// + /// - Complexity: O(*min*) where *min* is the value of the first element. public var startIndex: Index { - // Note: This is O(n) Index(_position: _read { $0.startIndex }) } + /// The collection’s “past the end” position--that is, the position one step + /// after the last valid subscript argument. + /// + /// - Complexity: O(1) public var endIndex: Index { Index(_position: .init(word: _storage.count, bit: 0)) } @@ -85,10 +119,33 @@ extension BitSet: Collection, BidirectionalCollection { Int(bitPattern: position._position.value) } + /// Returns the position immediately after the given index. + /// + /// - Parameter `index`: A valid index of the bit set. `index` must be less than `endIndex`. + /// + /// - Returns: The valid index immediately after `index`. + /// + /// - Complexity: + /// O(*d*), where *d* is difference between the value of the member + /// addressed by `index` and the member following it in the set. + /// (Each call needs to search for the next `true` bit in the underlying + /// storage.) public func index(after index: Index) -> Index { Index(_position: _read { $0.index(after: index._position) }) } + /// Returns the position immediately before the given index. + /// + /// - Parameter `index`: A valid index of the bit set. + /// `index` must be greater than `startIndex`. + /// + /// - Returns: The preceding valid index immediately before `index`. + /// + /// - Complexity: + /// O(*d*), where *d* is difference between the value of the member + /// addressed by `index` and the member preceding it in the set. + /// (Each call needs to search for the next `true` bit in the underlying + /// storage.) public func index(before index: Index) -> Index { Index(_position: _read { $0.index(before: index._position) }) } diff --git a/Sources/BitCollections/BitSet/BitSet+Equatable.swift b/Sources/BitCollections/BitSet/BitSet+Equatable.swift index 7f1d95cc..d4323b24 100644 --- a/Sources/BitCollections/BitSet/BitSet+Equatable.swift +++ b/Sources/BitCollections/BitSet/BitSet+Equatable.swift @@ -10,6 +10,10 @@ //===----------------------------------------------------------------------===// extension BitSet: Equatable { + /// Returns a Boolean value indicating whether two values are equal. Two + /// bit sets are considered equal if they contain the same elements. + /// + /// - Complexity: O(*max*), where *max* is value of the largest member of either set. public static func ==(left: Self, right: Self) -> Bool { left._storage == right._storage } diff --git a/Sources/BitCollections/BitSet/BitSet+ExpressibleByArrayLiteral.swift b/Sources/BitCollections/BitSet/BitSet+ExpressibleByArrayLiteral.swift index 91d6e694..c04f4e13 100644 --- a/Sources/BitCollections/BitSet/BitSet+ExpressibleByArrayLiteral.swift +++ b/Sources/BitCollections/BitSet/BitSet+ExpressibleByArrayLiteral.swift @@ -10,6 +10,16 @@ //===----------------------------------------------------------------------===// extension BitSet: ExpressibleByArrayLiteral { + /// Creates a new bit set from the contents of an array literal. + /// + /// Do not call this initializer directly. It is used by the compiler when + /// you use an array literal. Instead, create a new bit set using an array + /// literal as its value by enclosing a comma-separated list of values in + /// square brackets. You can use an array literal anywhere a bit set is + /// expected by the type context. + /// + /// - Parameter elements: A variadic list of elements of the new set. + /// - Complexity: O(`elements.count`) @inlinable public init(arrayLiteral elements: Int...) { self.init(elements) diff --git a/Sources/BitCollections/BitSet/BitSet+Hashable.swift b/Sources/BitCollections/BitSet/BitSet+Hashable.swift index 16d7bee3..f21566a7 100644 --- a/Sources/BitCollections/BitSet/BitSet+Hashable.swift +++ b/Sources/BitCollections/BitSet/BitSet+Hashable.swift @@ -10,6 +10,10 @@ //===----------------------------------------------------------------------===// extension BitSet: Hashable { + /// Hashes the essential components of this value by feeding them into the + /// given hasher. + /// + /// Complexity: O(*max*) where *max* is the largest value stored in this set. public func hash(into hasher: inout Hasher) { hasher.combine(_storage) } diff --git a/Sources/BitCollections/BitSet/BitSet+Initializers.swift b/Sources/BitCollections/BitSet/BitSet+Initializers.swift index 65c4e4a0..715e960e 100644 --- a/Sources/BitCollections/BitSet/BitSet+Initializers.swift +++ b/Sources/BitCollections/BitSet/BitSet+Initializers.swift @@ -10,6 +10,18 @@ //===----------------------------------------------------------------------===// extension BitSet { + /// Initializes a new, empty bit set. + /// + /// This is equivalent to initializing with an empty array literal. + /// For example: + /// + /// let set1 = BitSet() + /// print(set1.isEmpty) // true + /// + /// let set2: BitSet = [] + /// print(set2.isEmpty) // true + /// + /// - Complexity: O(1) public init() { self.init(_storage: [], count: 0) } @@ -22,26 +34,56 @@ extension BitSet { _checkInvariants() } + /// Initialize a new bit set from the raw bits of the supplied sequence of + /// words. (The term "words" is used here to mean a sequence of `UInt` + /// values, as in the `words` property of `BinaryInteger`.) + /// + /// The resulting bit set will contain precisely those integers that + /// correspond to `true` bits within `words`. Bits are counted from least + /// to most significant within each word. + /// + /// let bits = BitSet(words: [5, 2]) + /// // bits is [0, 2, UInt.bitWidth + 1] + /// + /// - Complexity: O(`words.count`) @inlinable public init(words: S) where S.Element == UInt { self.init(_words: words.map { _Word($0) }) } + /// Initialize a new bit set from the raw bits of the supplied integer value. + /// + /// The resulting bit set will contain precisely those integers that + /// correspond to `true` bits within `x`. Bits are counted from least + /// to most significant. + /// + /// let bits = BitSet(bitPattern: 42) + /// // bits is [1, 3, 5] + /// + /// - Complexity: O(`x.bitWidth`) @inlinable - public init(bitPattern: I) { - self.init(words: bitPattern.words) + public init(bitPattern x: I) { + self.init(words: x.words) } - /// Initialize a new bit set from the bit pattern of the given bit array. + /// Initialize a new bit set from the storage bits of the given bit array. /// The resulting bit set will contain exactly those integers that address /// `true` elements in the array. /// /// Note that this conversion is lossy -- it discards the precise length of /// the input array. + /// + /// - Complexity: O(`array.count`) public init(_ array: BitArray) { self.init(_words: array._storage) } + /// Create a new bit set containing the elements of a sequence. + /// + /// - Parameters: + /// - elements: The sequence of elements to turn into a bit set. + /// + /// - Complexity: O(*n*), where *n* is the number of elements in the sequence. @inlinable public init( _ elements: __owned S @@ -78,6 +120,13 @@ extension BitSet { } extension BitSet { + /// Create a new bit set containing the elements of a range of integers. + /// + /// - Parameters: + /// - range: The range to turn into a bit set. The range must not contain + /// negative values. + /// + /// - Complexity: O(`range.upperBound`) public init(_ range: Range) { guard let range = range._toUInt() else { preconditionFailure("BitSet can only hold nonnegative integers") diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra basics.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra basics.swift index 2435f544..c420f244 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra basics.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra basics.swift @@ -15,6 +15,14 @@ extension BitSet { _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) @@ -29,6 +37,23 @@ extension BitSet { return inserted } + /// 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 @@ -39,6 +64,16 @@ extension BitSet { 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 @@ -55,6 +90,15 @@ extension BitSet { } } + /// 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 O(*max*). @discardableResult public mutating func remove(_ member: Int) -> Int? { guard let m = UInt(exactly: member) else { return nil } diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra formIntersection.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra formIntersection.swift index 5aa5f3d4..97c32313 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra formIntersection.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra formIntersection.swift @@ -10,6 +10,16 @@ //===----------------------------------------------------------------------===// extension BitSet { + /// Removes the elements of this set that aren't also in the given one. + /// + /// var set: BitSet = [1, 2, 3, 4] + /// let other: BitSet = [0, 2, 4, 6] + /// set.formIntersection(other) + /// // set is now [2, 4] + /// + /// - Parameter other: A bit set. + /// + /// - Complexity: O(*max*), where *max* is the largest item in either set. public mutating func formIntersection(_ other: Self) { other._read { source in if source.wordCount < _storage.count { @@ -22,6 +32,17 @@ extension BitSet { } } + /// Removes the elements of this set that aren't also in the given sequence. + /// + /// var set: BitSet = [1, 2, 3, 4] + /// let other: Set = [6, 4, 2, 0] + /// set.formIntersection(other) + /// // set is now [2, 4] + /// + /// - Parameter other: A sequence 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`. @inlinable public mutating func formIntersection( _ other: __owned S @@ -33,6 +54,15 @@ extension BitSet { formIntersection(BitSet(_validMembersOf: other)) } + /// Removes the elements of this set that aren't also in the given range. + /// + /// var set: BitSet = [1, 2, 3, 4] + /// set.formIntersection(-10 ..< 3) + /// // set is now [3, 4] + /// + /// - Parameter other: A range of integers. + /// + /// - Complexity: O(*max*), where *max* is the largest item in `self`. public mutating func formIntersection(_ other: Range) { let other = other._clampedToUInt() guard let last = other.last else { diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra formSymmetricDifference.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra formSymmetricDifference.swift index cec48a92..743c79aa 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra formSymmetricDifference.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra formSymmetricDifference.swift @@ -10,6 +10,17 @@ //===----------------------------------------------------------------------===// extension BitSet { + /// Replace this set with the elements contained in this set or the given + /// set, but not both. + /// + /// var set: BitSet = [1, 2, 3, 4] + /// let other: BitSet = [0, 2, 4, 6] + /// set.formSymmetricDifference(other) + /// // set is now [0, 1, 3, 6] + /// + /// - Parameter other: Another set. + /// + /// - Complexity: O(*max*), where *max* is the largest item in either set. public mutating func formSymmetricDifference(_ other: __owned Self) { _ensureCapacity(limit: other._capacity) _updateThenShrink { target, shrink in @@ -20,6 +31,19 @@ extension BitSet { } } + /// Replace this set with the elements contained in this set or the given + /// sequence, but not both. + /// + /// var set: BitSet = [1, 2, 3, 4] + /// let other = [6, 4, 2, 0, 2, 0] + /// set.formSymmetricDifference(other) + /// // set is now [0, 1, 3, 6] + /// + /// - Parameter other: A sequence of nonnegative integers. + /// + /// - Complexity: O(*max*) + *k*, where *max* is the largest item in either + /// input, and *k* is the complexity of iterating over all elements in + /// `other`. @inlinable public mutating func formSymmetricDifference( _ other: __owned S @@ -33,6 +57,16 @@ extension BitSet { } extension BitSet { + /// Replace this set with the elements contained in this set or the given + /// range of integers, but not both. + /// + /// var set: BitSet = [1, 2, 3, 4] + /// set.formSymmetricDifference(3 ..< 7) + /// // set is now [1, 2, 5, 6] + /// + /// - Parameter other: A range of nonnegative integers. + /// + /// - Complexity: O(*max*), where *max* is the largest item in either input. public mutating func formSymmetricDifference(_ other: Range) { guard let other = other._toUInt() else { preconditionFailure("Invalid range") diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra formUnion.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra formUnion.swift index 4b407229..0ef99a80 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra formUnion.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra formUnion.swift @@ -10,12 +10,16 @@ //===----------------------------------------------------------------------===// extension BitSet { - @usableFromInline - internal mutating func formUnion(_ other: Range) { - } -} - -extension BitSet { + /// Adds the elements of the given set to this set. + /// + /// var set: BitSet = [1, 2, 3, 4] + /// let other: BitSet = [0, 2, 4, 6] + /// set.formUnion(other) + /// // `set` is now `[0, 1, 2, 3, 4, 6]` + /// + /// - Parameter other: The set of elements to insert. + /// + /// - Complexity: O(*max*), where *max* is the largest item in either input. public mutating func formUnion(_ other: __owned Self) { _ensureCapacity(limit: other._capacity) _update { target in @@ -25,6 +29,18 @@ extension BitSet { } } + /// Adds the elements of the given sequence to this set. + /// + /// var set: BitSet = [1, 2, 3, 4] + /// let other = [6, 4, 2, 0, 2, 0] + /// set.formUnion(other) + /// // `set` is now `[0, 1, 2, 3, 4, 6]` + /// + /// - Parameter other: A sequence of nonnegative integers. + /// + /// - Complexity: O(*max*) + *k*, where *max* is the largest item in either + /// input, and *k* is the complexity of iterating over all elements in + /// `other`. @inlinable public mutating func formUnion( _ other: __owned S @@ -42,6 +58,15 @@ extension BitSet { } } + /// Adds the elements of the given range of integers to this set. + /// + /// var set: BitSet = [1, 2, 3, 4] + /// set.formUnion(3 ..< 7) + /// // `set` is now `[1, 2, 3, 4, 5, 6]` + /// + /// - Parameter other: A range of nonnegative integers. + /// + /// - Complexity: O(*max*), where *max* is the largest item in either input. public mutating func formUnion(_ other: Range) { guard let other = other._toUInt() else { preconditionFailure("Invalid range") diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra intersection.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra intersection.swift index d1ce2833..ce5b6edb 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra intersection.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra intersection.swift @@ -10,6 +10,17 @@ //===----------------------------------------------------------------------===// 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 __consuming func intersection(_ other: Self) -> Self { self._read { first in other._read { second in @@ -21,6 +32,18 @@ extension BitSet { } } + /// 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 __consuming func intersection( _ other: __owned S @@ -36,6 +59,17 @@ extension BitSet { return intersection(BitSet(_validMembersOf: other)) } + /// 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 __consuming func intersection(_ other: Range) -> Self { var result = self result.formIntersection(other) diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isDisjoint.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isDisjoint.swift index da4a02b2..d95b1553 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isDisjoint.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isDisjoint.swift @@ -10,6 +10,19 @@ //===----------------------------------------------------------------------===// extension BitSet { + /// Returns a Boolean value that indicates whether the set has no members in + /// common with the given set. + /// + /// let a: BitSet = [1, 2, 3, 4] + /// let b: BitSet = [5, 6] + /// a.isDisjoint(with: b) // true + /// + /// - Parameter other: Another bit set. + /// + /// - Returns: `true` if `self` has no elements in common with `other`; + /// otherwise, `false`. + /// + /// - Complexity: O(*max*), where *max* is the largest item in either input. public func isDisjoint(with other: Self) -> Bool { self._read { first in other._read { second in @@ -23,6 +36,20 @@ extension BitSet { } } + /// Returns a Boolean value that indicates whether the set has no members in + /// common with the given sequence of integers. + /// + /// let a: BitSet = [1, 2, 3, 4] + /// let b: BitSet = [5, 6, -10, 42] + /// a.isDisjoint(with: b) // true + /// + /// - Parameter other: A sequence of arbitrary integers. + /// + /// - Returns: `true` if `self` has no elements in common with `other`; + /// otherwise, `false`. + /// + /// - 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 isDisjoint(with other: S) -> Bool where S.Element == Int @@ -38,9 +65,19 @@ extension BitSet { } return true } -} -extension BitSet { + /// Returns a Boolean value that indicates whether the set has no members in + /// common with the given range of integers. + /// + /// let a: BitSet = [1, 2, 3, 4] + /// a.isDisjoint(with: -10 ..< 0) // true + /// + /// - Parameter other: A range of arbitrary integers. + /// + /// - Returns: `true` if `self` has no elements in common with `other`; + /// otherwise, `false`. + /// + /// - Complexity: O(*max*), where *max* is the largest item in `self`. public func isDisjoint(with other: Range) -> Bool { _read { $0.isDisjoint(with: other._clampedToUInt()) } } diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSubset.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSubset.swift index c066ba97..2033b475 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSubset.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSubset.swift @@ -10,6 +10,26 @@ //===----------------------------------------------------------------------===// extension BitSet { + /// Returns a Boolean value that indicates whether this bit set is a strict + /// subset of the given set. + /// + /// Set *A* is a strict subset of another set *B* if every member of *A* is + /// also a member of *B* and *B* contains at least one element that is not a + /// member of *A*. + /// + /// let a: BitSet = [1, 2, 3, 4] + /// let b: BitSet = [1, 2, 4] + /// let c: BitSet = [0, 1] + /// a.isStrictSubset(of: a) // false + /// b.isStrictSubset(of: a) // true + /// c.isStrictSubset(of: a) // false + /// + /// - Parameter other: Another bit set. + /// + /// - Returns: `true` if the set is a strict subset of `other`; + /// otherwise, `false`. + /// + /// - Complexity: O(*max*), where *max* is the largest item in `self`. public func isStrictSubset(of other: Self) -> Bool { self._read { first in other._read { second in @@ -30,6 +50,26 @@ extension BitSet { } } + /// Returns a Boolean value that indicates whether this bit set is a strict + /// subset of the values in a given sequence of integers. + /// + /// Set *A* is a strict subset of another set *B* if every member of *A* is + /// also a member of *B* and *B* contains at least one element that is not a + /// member of *A*. + /// + /// let a = [1, 2, 3, 4, -10] + /// let b: BitSet = [1, 2, 4] + /// let c: BitSet = [0, 1] + /// b.isStrictSubset(of: a) // true + /// c.isStrictSubset(of: a) // false + /// + /// - Parameter other: A sequence of arbitrary integers. + /// + /// - Returns: `true` if the set is a strict subset of `other`; + /// otherwise, `false`. + /// + /// - 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 isStrictSubset(of other: S) -> Bool where S.Element == Int @@ -68,6 +108,24 @@ extension BitSet { } } + /// Returns a Boolean value that indicates whether this set is a strict + /// subset of the given set. + /// + /// Set *A* is a strict subset of another set *B* if every member of *A* is + /// also a member of *B* and *B* contains at least one element that is not a + /// member of *A*. + /// + /// let b: BitSet = [0, 1, 2] + /// let c: BitSet = [2, 3, 4] + /// b.isStrictSubset(of: -10 ..< 4) // true + /// c.isStrictSubset(of: -10 ..< 4) // false + /// + /// - Parameter other: An arbitrary range of integers. + /// + /// - Returns: `true` if the set is a strict subset of `other`; + /// otherwise, `false`. + /// + /// - Complexity: O(*max*), where *max* is the largest item in `self`. public func isStrictSubset(of other: Range) -> Bool { isSubset(of: other) && !isSuperset(of: other) } diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSuperset.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSuperset.swift index c90cd7b2..76021242 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSuperset.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSuperset.swift @@ -10,10 +10,47 @@ //===----------------------------------------------------------------------===// extension BitSet { + /// Returns a Boolean value that indicates whether this set is a strict + /// superset of another set. + /// + /// Set *A* is a strict superset of another set *B* if every member of *B* is + /// also a member of *A* and *A* contains at least one element that is *not* + /// a member of *B*. + /// + /// let a: BitSet = [1, 2, 3, 4] + /// let b: BitSet = [1, 2, 4] + /// let c: BitSet = [0, 1] + /// a.isStrictSuperset(of: a) // false + /// a.isStrictSuperset(of: b) // true + /// a.isStrictSuperset(of: c) // false + /// + /// - Parameter other: Another bit set. + /// + /// - Returns: `true` if the set is a superset of `other`; otherwise, `false`. + /// + /// - Complexity: O(*max*), where *max* is the largest item in `other`. public func isStrictSuperset(of other: Self) -> Bool { other.isStrictSubset(of: self) } + /// Returns a Boolean value that indicates whether this set is a superset of + /// the values in a given sequence of integers. + /// + /// Set *A* is a superset of another set *B* if every member of *B* is also a + /// member of *A*. + /// + /// let a = [1, 2, 3] + /// let b: BitSet = [0, 1, 2, 3, 4] + /// let c: BitSet = [0, 1, 2] + /// b.isSuperset(of: a) // true + /// c.isSuperset(of: a) // false + /// + /// - Parameter other: A sequence of arbitrary integers. + /// + /// - Returns: `true` if the set is a subset of `other`; otherwise, `false`. + /// + /// - Complexity: O(*max*) + *k*, where *max* is the largest item in `other`, + /// and *k* is the complexity of iterating over all elements in `other`. @inlinable public func isStrictSuperset(of other: S) -> Bool where S.Element == Int @@ -36,10 +73,25 @@ extension BitSet { } } + /// Returns a Boolean value that indicates whether this set is a superset of + /// a given range of integers. + /// + /// Set *A* is a superset of another set *B* if every member of *B* is also a + /// member of *A*. + /// + /// let a: BitSet = [0, 1, 2, 3, 4, 10] + /// a.isSuperset(of: 0 ..< 4) // true + /// a.isSuperset(of: -10 ..< 4) // false + /// + /// - Parameter other: An arbitrary range of integers. + /// + /// - Returns: `true` if the set is a subset of `other`; otherwise, `false`. + /// + /// - Complexity: O(`range.count`) public func isStrictSuperset(of other: Range) -> Bool { if other.isEmpty { return !isEmpty } if isEmpty { return false } guard let r = other._toUInt() else { return false } - return _read { $0.isSuperset(of: r) && !$0.isSubset(of: r) } + return _read { r.count < $0.count && $0.isSuperset(of: r) } } } diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSubset.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSubset.swift index 9046b179..2fa82b1d 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSubset.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSubset.swift @@ -10,6 +10,24 @@ //===----------------------------------------------------------------------===// extension BitSet { + /// Returns a Boolean value that indicates whether this set is a subset of + /// the given set. + /// + /// Set *A* is a subset of another set *B* if every member of *A* is also a + /// member of *B*. + /// + /// let a: BitSet = [1, 2, 3, 4] + /// let b: BitSet = [1, 2, 4] + /// let c: BitSet = [0, 1] + /// a.isSubset(of: a) // true + /// b.isSubset(of: a) // true + /// c.isSubset(of: a) // false + /// + /// - Parameter other: Another bit set. + /// + /// - Returns: `true` if the set is a subset of `other`; otherwise, `false`. + /// + /// - Complexity: O(*max*), where *max* is the largest item in `self`. public func isSubset(of other: Self) -> Bool { self._read { first in other._read { second in @@ -28,6 +46,24 @@ extension BitSet { } } + /// Returns a Boolean value that indicates whether this set is a subset of + /// the values in a given sequence of integers. + /// + /// Set *A* is a subset of another set *B* if every member of *A* is also a + /// member of *B*. + /// + /// let a = [1, 2, 3, 4, -10] + /// let b: BitSet = [1, 2, 4] + /// let c: BitSet = [0, 1] + /// b.isSubset(of: a) // true + /// c.isSubset(of: a) // false + /// + /// - Parameter other: A sequence of arbitrary integers. + /// + /// - Returns: `true` if the set is a subset of `other`; otherwise, `false`. + /// + /// - 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 isSubset(of other: S) -> Bool where S.Element == Int @@ -48,6 +84,22 @@ extension BitSet { return false } + /// Returns a Boolean value that indicates whether this set is a subset of + /// the given range of integers. + /// + /// Set *A* is a subset of another set *B* if every member of *A* is also a + /// member of *B*. + /// + /// let b: BitSet = [0, 1, 2] + /// let c: BitSet = [2, 3, 4] + /// b.isSubset(of: -10 ..< 4) // true + /// c.isSubset(of: -10 ..< 4) // false + /// + /// - Parameter other: An arbitrary range of integers. + /// + /// - Returns: `true` if the set is a subset of `other`; otherwise, `false`. + /// + /// - Complexity: O(*max*), where *max* is the largest item in `self`. public func isSubset(of other: Range) -> Bool { guard !isEmpty else { return true } return _read { $0.isSubset(of: other._clampedToUInt()) } diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSuperset.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSuperset.swift index 41863326..45d1f06c 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSuperset.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSuperset.swift @@ -10,10 +10,46 @@ //===----------------------------------------------------------------------===// extension BitSet { + /// Returns a Boolean value that indicates whether this set is a superset of + /// the given set. + /// + /// Set *A* is a superset of another set *B* if every member of *B* is also a + /// member of *A*. + /// + /// let a: BitSet = [1, 2, 3, 4] + /// let b: BitSet = [1, 2, 4] + /// let c: BitSet = [0, 1] + /// a.isSuperset(of: a) // true + /// a.isSuperset(of: b) // true + /// a.isSuperset(of: c) // false + /// + /// - Parameter other: Another bit set. + /// + /// - Returns: `true` if the set is a superset of `other`; otherwise, `false`. + /// + /// - Complexity: O(*max*), where *max* is the largest item in `other`. public func isSuperset(of other: Self) -> Bool { other.isSubset(of: self) } + /// Returns a Boolean value that indicates whether this set is a superset of + /// the values in a given sequence of integers. + /// + /// Set *A* is a superset of another set *B* if every member of *B* is also a + /// member of *A*. + /// + /// let a = [1, 2, 3] + /// let b: BitSet = [0, 1, 2, 3, 4] + /// let c: BitSet = [0, 1, 2] + /// b.isSuperset(of: a) // true + /// c.isSuperset(of: a) // false + /// + /// - Parameter other: A sequence of arbitrary integers. + /// + /// - Returns: `true` if the set is a subset of `other`; otherwise, `false`. + /// + /// - Complexity: The same as the complexity of iterating over all elements + /// in `other`. @inlinable public func isSuperset(of other: S) -> Bool where S.Element == Int @@ -31,6 +67,21 @@ extension BitSet { return true } + /// Returns a Boolean value that indicates whether this set is a superset of + /// a given range of integers. + /// + /// Set *A* is a superset of another set *B* if every member of *B* is also a + /// member of *A*. + /// + /// let a: BitSet = [0, 1, 2, 3, 4, 10] + /// a.isSuperset(of: 0 ..< 4) // true + /// a.isSuperset(of: -10 ..< 4) // false + /// + /// - Parameter other: An arbitrary range of integers. + /// + /// - Returns: `true` if the set is a subset of `other`; otherwise, `false`. + /// + /// - Complexity: O(`range.count`) public func isSuperset(of other: Range) -> Bool { if other.isEmpty { return true } if isEmpty { return false } diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtract.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtract.swift index f93e62f4..a3be359e 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtract.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtract.swift @@ -10,6 +10,16 @@ //===----------------------------------------------------------------------===// extension BitSet { + /// Removes the elements of the given bit set from this set. + /// + /// var set: BitSet = [1, 2, 3, 4] + /// let other: BitSet = [0, 2, 4, 6] + /// set.subtract(other) + /// // set is now [1, 3] + /// + /// - Parameter other: Another bit set. + /// + /// - Complexity: O(*max*), where *max* is the largest item in either input. public mutating func subtract(_ other: Self) { _updateThenShrink { target, shrink in other._read { source in @@ -30,6 +40,17 @@ extension BitSet { } } + /// Removes the elements of the given sequence of integers from this set. + /// + /// var set: BitSet = [1, 2, 3, 4] + /// let other = [6, 4, 2, 0, -2, -4] + /// set.subtract(other) + /// // set is now [1, 3] + /// + /// - Parameter other: A sequence of arbitrary 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`. @inlinable public mutating func subtract( _ other: __owned S @@ -61,6 +82,17 @@ extension BitSet { } } + /// Removes the elements of the given range of integers from this set. + /// + /// var set: BitSet = [1, 2, 3, 4] + /// set.subtract(-10 ..< 3) + /// // set is now [3, 4] + /// + /// - Parameter other: A range of arbitrary integers. + /// + /// - Returns: A new set. + /// + /// - Complexity: O(*max*), where *max* is the largest item in self. public mutating func subtract(_ other: Range) { _subtract(other._clampedToUInt()) } diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtracting.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtracting.swift index 80530f82..df3d41a5 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtracting.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtracting.swift @@ -10,6 +10,18 @@ //===----------------------------------------------------------------------===// extension BitSet { + /// Returns a new set containing the elements of this set that do not occur + /// in the given other set. + /// + /// let set: BitSet = [1, 2, 3, 4] + /// let other: BitSet = [0, 2, 4, 6] + /// set.subtracting(other) // [1, 3] + /// + /// - Parameter other: Another bit set. + /// + /// - Returns: A new set. + /// + /// - Complexity: O(*max*), where *max* is the largest item in either input. public __consuming func subtracting(_ other: Self) -> Self { self._read { first in other._read { second in @@ -21,6 +33,19 @@ extension BitSet { } } + /// Returns a new set containing the elements of this set that do not occur + /// in the given sequence of integers. + /// + /// let set: BitSet = [1, 2, 3, 4] + /// let other = [6, 4, 2, 0, -2, -4] + /// set.subtracting(other) // [1, 3] + /// + /// - Parameter other: A sequence of arbitrary integers. + /// + /// - Returns: A new set. + /// + /// - 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 __consuming func subtracting( _ other: __owned S @@ -32,6 +57,17 @@ extension BitSet { return result } + /// Returns a new set containing the elements of this set that do not occur + /// in the given range of integers. + /// + /// let set: BitSet = [1, 2, 3, 4] + /// set.subtracting(-10 ..< 3) // [3, 4] + /// + /// - Parameter other: A range of arbitrary integers. + /// + /// - Returns: A new set. + /// + /// - Complexity: O(*max*), where *max* is the largest item in self. public __consuming func subtracting(_ other: Range) -> Self { var result = self result.subtract(other) diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra symmetricDifference.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra symmetricDifference.swift index 8d524c6e..ad3fcf68 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra symmetricDifference.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra symmetricDifference.swift @@ -10,6 +10,18 @@ //===----------------------------------------------------------------------===// extension BitSet { + /// Returns a new bit set with the elements that are either in this set or in + /// `other`, but not in both. + /// + /// let set: BitSet = [1, 2, 3, 4] + /// let other: BitSet = [6, 4, 2, 0] + /// set.symmetricDifference(other) // [0, 1, 3, 6] + /// + /// - Parameter other: Another set. + /// + /// - Returns: A new set. + /// + /// - Complexity: O(*max*), where *max* is the largest item in either set. public __consuming func symmetricDifference(_ other: __owned Self) -> Self { self._read { first in other._read { second in @@ -21,6 +33,18 @@ extension BitSet { } } + /// Returns a new bit set with the elements that are either in this set or in + /// `other`, but not in both. + /// + /// let set: BitSet = [1, 2, 3, 4] + /// let other = [6, 4, 2, 0, 2, 0] + /// set.formSymmetricDifference(other) // [0, 1, 3, 6] + /// + /// - Parameter other: A sequence of nonnegative integers. + /// + /// - Complexity: O(*max*) + *k*, where *max* is the largest item in either + /// input, and *k* is the complexity of iterating over all elements in + /// `other`. @inlinable public __consuming func symmetricDifference( _ other: __owned S @@ -33,6 +57,17 @@ extension BitSet { return symmetricDifference(BitSet(other)) } + /// Returns a new bit set with the elements that are either in this set or in + /// `other`, but not in both. + /// + /// let set: BitSet = [1, 2, 3, 4] + /// set.formSymmetricDifference(3 ..< 7) // [1, 2, 5, 6] + /// + /// - Parameter other: A range of nonnegative integers. + /// + /// - Returns: A new set. + /// + /// - Complexity: O(*max*), where *max* is the largest item in either input. public __consuming func symmetricDifference(_ other: Range) -> Self { var result = self result.formSymmetricDifference(other) diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra union.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra union.swift index 993d5a8e..f40716e9 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra union.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra union.swift @@ -10,6 +10,15 @@ //===----------------------------------------------------------------------===// extension BitSet { + /// Returns a new set with the elements of both this and the given set. + /// + /// let set: BitSet = [1, 2, 3, 4] + /// let other: BitSet = [0, 2, 4, 6] + /// set.union(other) // [0, 1, 2, 3, 4, 6] + /// + /// - Parameter other: The set of elements to insert. + /// + /// - Complexity: O(*max*), where *max* is the largest item in either input. public __consuming func union(_ other: __owned Self) -> Self { self._read { first in other._read { second in @@ -21,6 +30,18 @@ extension BitSet { } } + /// Returns a new set with the elements of both this set and the given + /// sequence of integers. + /// + /// let set: BitSet = [1, 2, 3, 4] + /// let other = [6, 4, 2, 0, 2, 0] + /// set.union(other) // [0, 1, 2, 3, 4, 6] + /// + /// - Parameter other: A sequence of nonnegative integers. + /// + /// - Complexity: O(*max*) + *k*, where *max* is the largest item in either + /// input, and *k* is the complexity of iterating over all elements in + /// `other`. @inlinable public __consuming func union( _ other: __owned S @@ -35,6 +56,15 @@ extension BitSet { return result } + /// Returns a new set with the elements of both this set and the given + /// range of integers. + /// + /// let set: BitSet = [1, 2, 3, 4] + /// set.union(3 ..< 7) // [1, 2, 3, 4, 5, 6] + /// + /// - Parameter other: A range of nonnegative integers. + /// + /// - Complexity: O(*max*), where *max* is the largest item in either input. public __consuming func union(_ other: Range) -> Self { var result = self result.formUnion(other) diff --git a/Sources/BitCollections/BitSet/BitSet.Index.swift b/Sources/BitCollections/BitSet/BitSet.Index.swift index 76c3701c..df1fc9ce 100644 --- a/Sources/BitCollections/BitSet/BitSet.Index.swift +++ b/Sources/BitCollections/BitSet/BitSet.Index.swift @@ -10,6 +10,13 @@ //===----------------------------------------------------------------------===// extension BitSet { + /// An opaque type that represents a position in a bit set. + /// + /// The elements of a bit set can be addressed simply by their value, + /// so the `Index` type could be defined to be `Int`, the same as `Element`. + /// However, `BitSet` uses an opaque wrapper instead, to prevent confusion: + /// it would otherwise be all too easy to accidentally use `i + 1` instead of + /// calling `index(after: i)`, and ending up with an invalid index. @frozen public struct Index { @usableFromInline @@ -33,16 +40,26 @@ extension BitSet { } extension BitSet.Index: Comparable, Hashable { + /// Returns a Boolean value indicating whether two values are equal. + /// + /// - Complexity: O(1) @inlinable public static func ==(left: Self, right: Self) -> Bool { left._value == right._value } + /// Returns a Boolean value indicating whether the first value is ordered + /// before the second. + /// + /// - Complexity: O(1) @inlinable public static func < (left: Self, right: Self) -> Bool { left._value < right._value } + /// Hashes the essential components of this value by feeding them to the given hasher. + /// + /// - Complexity: O(1) @inlinable public func hash(into hasher: inout Hasher) { hasher.combine(_value) diff --git a/Sources/BitCollections/BitSet/BitSet.swift b/Sources/BitCollections/BitSet/BitSet.swift index b628d58b..cd1c5503 100644 --- a/Sources/BitCollections/BitSet/BitSet.swift +++ b/Sources/BitCollections/BitSet/BitSet.swift @@ -14,6 +14,10 @@ /// /// Bit sets implement `SetAlgebra` and provide efficient implementations /// for set operations based on standard binary logic operations. +/// +/// See `BitArray` for an alternative form of the same underlying data +/// structure, treating it as a mutable random-access collection of `Bool` +/// values. public struct BitSet { @usableFromInline internal var _storage: [_Word]