NumberSet
public struct NumberSet<T> : SetAlgebra where T : FixedWidthInteger, T : UnsignedInteger
extension NumberSet: Sequence
extension NumberSet: CustomStringConvertible
NumberSet uses a specified unsigned FixedWidthInteger as the storage for a Set containing the numbers
0…bitWidth. eg a UInt8 can contain the numbers 0...7, a UInt64 can store 0...63.
The storage uses a bit per number where bit[x] is used to store x. This gives an ordering
of the set from lowest to highest.
The rawValue property represents the underlying storage and can be easily interpreted.
rawValue = 0b1001_0001 [0, 4, 7]
-
The elements stored in the
NumberSetare of typeInt.Declaration
Swift
public typealias Element = Int -
The underlying storage. Read only.
Declaration
Swift
private(set) public var rawValue: T { get } -
A Boolean value that indicates whether the set is empty.
Declaration
Swift
public var isEmpty: Bool { get } -
The number of elements in the set.
Declaration
Swift
public var count: Int { get } -
The total number of elements that the set can contain without allocating new storage.
Declaration
Swift
public var capacity: Int { get } -
The first element of the collection.
If the collection is empty, the value of this property is
nil.Declaration
Swift
public var first: Int? { get } -
Creates an empty set.
Declaration
Swift
@inline(__always) public init() -
Create a new set from a literal value.
Use this initialiser to create a new set. Member
xcorresponds to bit x. To create a set containing the numbers 1 and 5:let numberSet = Set<UInt8>(rawValue: 0b0010_0010) print(numberSet) // prints [1, 5]Declaration
Swift
@inline(__always) public init(rawValue: T)Parameters
rawValueA literal with the members of the new set represented by the appropaite bit being set.
-
Inserts the given element in the set if it is not already present.
Declaration
Swift
@discardableResult public mutating func insert(_ newMember: Int) -> (inserted: Bool, memberAfterInsert: Int)Parameters
newMemberAn element to insert into the set.
Return Value
(true, newMember) if newMember was not contained in the set. If an element equal to newMember was already contained in the set, the method returns (false, oldMember), where oldMember is the element that was equal to newMember. In some cases, oldMember may be distinguishable from newMember by identity comparison or some other means.
-
Inserts the given element into the set unconditionally.
Declaration
Swift
public mutating func update(with newMember: Int) -> Int?Parameters
newMemberAn element to insert into the set.
Return Value
An element equal to newMember if the set already contained such a member; otherwise,
nil. In some cases, the returned element may be distinguishable from newMember by identity comparison or some other means. -
Removes the specified element from the set.
Declaration
Swift
public mutating func remove(_ member: Int) -> Int?Parameters
memberThe element to remove from the set.
Return Value
The value of the member parameter if it was a member of the set; otherwise,
nil. -
Removes the first element of the set.
A
NumberSetis not an ordered collection so the “first” element may is always the element with the lowest numeric value. The set must not be empty. Complexity: O(1).Declaration
Swift
@discardableResult public mutating func removeFirst() -> IntReturn Value
A member of the set. This memeber is the element with the lowest numeric value.
-
Removes the element at the given index of the set.
Declaration
Swift
@discardableResult public mutating func remove(at position: Int) -> IntParameters
positionThe index of the member to remove. position must be a valid index of the set, and must not be equal to the set’s end index.
Return Value
The element that was removed from the set.
-
Removes all members from the set.
Declaration
Swift
public mutating func removeAll(keepingCapacity keepCapacity: Bool = false)Parameters
keepingCapacityThis parameter is ignore as the underlying storage is fixed.
-
Returns a Boolean value that indicates whether the given element exists in the set.
Declaration
Swift
@inline(__always) public func contains(_ member: Int) -> BoolParameters
memberAn element to look for in the set.
Return Value
trueif member exists in the set; otherwise,false. -
Returns the minimum element in the sequence.
Declaration
Swift
@inline(__always) public func min() -> Int?Return Value
The sequence’s minimum element. If the sequence has no elements, returns
nil. -
Returns the maximum element in the sequence.
Declaration
Swift
@inline(__always) public func max() -> Int?Return Value
The sequence’s maximum element. If the sequence has no elements, returns
nil. -
Returns a Boolean value that indicates whether this set has no members in common with the given set.
Declaration
Swift
@inline(__always) public func isDisjoint(with other: `Self`) -> BoolParameters
otherAnother
NumberSetof the same type as the currentNumberSet.Return Value
trueif the set has no elements in common with other; otherwise,false. -
Returns a new set containing the elements of this set that do not occur in the given set.
Declaration
Swift
@inline(__always) public func subtracting(_ other: `Self`) -> NumberSet<T>Parameters
otherAnother
NumberSetof the same type as the currentNumberSet.Return Value
A new set.
-
Returns a new set with the elements of both this and the given set.
Declaration
Swift
@inline(__always) public func union(_ other: `Self`) -> NumberSet<T>Parameters
otherAnother
NumberSetof the same type as the currentNumberSet.Return Value
A new set with the unique elements of this set and
other. -
Returns a new set with the elements that are common to both this set and the given set.
Declaration
Swift
@inline(__always) public func intersection(_ other: `Self`) -> NumberSet<T>Parameters
otherAnother
NumberSetof the same type as the currentNumberSet.Return Value
A new set.
-
Returns a new set with the elements that are either in this set or in the given set, but not in both.
Declaration
Swift
@inline(__always) public func symmetricDifference(_ other: `Self`) -> NumberSet<T>Parameters
otherAnother
NumberSetof the same type as the currentNumberSet.Return Value
A new set.
-
Removes the elements of the given set from this set.
Declaration
Swift
@inline(__always) public mutating func subtract(_ other: `Self`)Parameters
otherAnother
NumberSetof the same type as the currentNumberSet. -
Adds the elements of the given set to the set.
Declaration
Swift
@inline(__always) public mutating func formUnion(_ other: `Self`)Parameters
otherAnother
NumberSetof the same type as the currentNumberSet. -
Removes the elements of this set that aren’t also in the given set.
Declaration
Swift
@inline(__always) public mutating func formIntersection(_ other: `Self`)Parameters
otherAnother
NumberSetof the same type as the currentNumberSet. -
Removes the elements of the set that are also in the given set and adds the members of the given set that are not already in the set.
Declaration
Swift
@inline(__always) public mutating func formSymmetricDifference(_ other: `Self`)Parameters
otherAnother
NumberSetof the same type as the currentNumberSet. -
Returns a Boolean value indicating whether two sets have equal elements.
Declaration
Swift
public static func == (lhs: `Self`, rhs: `Self`) -> BoolParameters
lhsA
NumberSetrhsAnother
NumberSetof the same type.Return Value
trueif thelhsandrhshave the same elements; otherwise,false. -
Returns an iterator over the members of the set..
Declaration
Swift
public func makeIterator() -> NumberSetIterator<T> -
A string that represents the contents of the set.
Declaration
Swift
public var description: String { get }
View on GitHub
NumberSet Structure Reference