SetAlgebra
A type that provides mathematical set operations.
Declaration
protocol SetAlgebra<Element> : Equatable, ExpressibleByArrayLiteralOverview
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) == xx.intersection([]) == []x.union(x) == xx.union([]) == xx.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) == yx.isSuperset(of: y)impliesx.union(y) == xx.isSubset(of: y)if and only ify.isSuperset(of: x)x.isStrictSuperset(of: y)if and only ifx.isSuperset(of: y) && x != yx.isStrictSubset(of: y)if and only ifx.isSubset(of: y) && x != y
Topics
Creating a Set
Testing for Membership
Adding and Removing Elements
Combining Sets
union(_:)formUnion(_:)intersection(_:)formIntersection(_:)symmetricDifference(_:)formSymmetricDifference(_:)