SetAlgebra
A type that provides mathematical set operations.
protocol SetAlgebra<Element> : Equatable, ExpressibleByArrayLiteral
Overview
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