SetAlgebra
A type that provides mathematical set operations.
protocol SetAlgebra<Element> : Equatable, ExpressibleByArrayLiteral
You use types that conform to the SetAlgebra
protocol when you need efficient membership tests or mathematical set operations such as intersection, union, and subtraction. In the standard library, you can use the Set
type with elements of any hashable type, or you can easily create bit masks with SetAlgebra
conformance using the OptionSet
protocol. See those types for more information.
Conforming to the SetAlgebra Protocol
When implementing a custom type that conforms to the SetAlgebra
protocol, you must implement the required initializers and methods. For the inherited methods to work properly, conforming types must meet the following axioms. Assume that S
is a custom type that conforms to the SetAlgebra
protocol, x
and y
are instances of S
, and e
is of type S.Element
—the type that the set holds.
S() == []
x.intersection(x) == x
x.intersection([]) == []
x.union(x) == x
x.union([]) == x
x.contains(e)
impliesx.union(y).contains(e)
x.union(y).contains(e)
impliesx.contains(e) || y.contains(e)
x.contains(e) && y.contains(e)
if and only ifx.intersection(y).contains(e)
x.isSubset(of: y)
impliesx.union(y) == y
x.isSuperset(of: y)
impliesx.union(y) == x
x.isSubset(of: y)
if and only ify.isSuperset(of: x)
x.isStrictSuperset(of: y)
if and only ifx.isSuperset(of: y) && x != y
x.isStrictSubset(of: y)
if and only ifx.isSubset(of: y) && x != y
Supertypes
protocol Equatable
A type that can be compared for value equality.
protocol ExpressibleByArrayLiteral
A type that can be initialized using an array literal.
Requirements
associatedtype Element
A type for which the conforming type provides a containment test.
init(
) Creates an empty set.
init<S>(S
) Creates a new set from a finite sequence of items.
var isEmpty: Bool
A Boolean value that indicates whether the set has no elements.
func contains(Self
.Element) -> Bool Returns a Boolean value that indicates whether the given element exists in the set.
func formIntersection(Self
) Removes the elements of this set that aren’t also in the given set.
func formSymmetricDifference(Self
) 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.
func formUnion(Self
) Adds the elements of the given set to the set.
func insert(Self
.Element) -> (inserted: Bool, memberAfterInsert: Self.Element) Inserts the given element in the set if it is not already present.
func intersection(Self
) -> Self Returns a new set with the elements that are common to both this set and the given set.
func isDisjoint(with: Self
) -> Bool Returns a Boolean value that indicates whether the set has no members in common with the given set.
func isSubset(of: Self
) -> Bool Returns a Boolean value that indicates whether the set is a subset of another set.
func isSuperset(of: Self
) -> Bool Returns a Boolean value that indicates whether the set is a superset of the given set.
func remove(Self
.Element) -> Self.Element? Removes the given element and any elements subsumed by the given element.
func subtract(Self
) Removes the elements of the given set from this set.
func subtracting(Self
) -> Self Returns a new set containing the elements of this set that do not occur in the given set.
func symmetricDifference(Self
) -> Self Returns a new set with the elements that are either in this set or in the given set, but not in both.
func union(Self
) -> Self Returns a new set with the elements of both this and the given set.
func update(with: Self.Element
) -> Self.Element? Inserts the given element into the set unconditionally.
Citizens in Swift
Members
func isStrictSubset(of: Self
) -> Bool Returns a Boolean value that indicates whether this set is a strict subset of the given set.
func isStrictSuperset(of: Self
) -> Bool Returns a Boolean value that indicates whether this set is a strict superset of the given set.
Subtypes
protocol OptionSet
A type that presents a mathematical set interface to a bit set.
Available in Cxx
Members
init<C>(C
) Creates a set containing the elements of a C++ container.
Extension in AsyncAlgorithms
Members
init<Source>(Source
) async rethrows Creates a new set from an asynchronous sequence of items.