SetAlgebra

A type that provides mathematical set operations.

You’re reading documentation from an older version of Swift 6.1.

A symbol with the same signature as this one exists in the latest stable release of Swift 6.1.

protocol SetAlgebra<Element> : Equatable, ExpressibleByArrayLiteral
Browse conforming types

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) implies x.union(y).contains(e)

  • x.union(y).contains(e) implies x.contains(e) || y.contains(e)

  • x.contains(e) && y.contains(e) if and only if x.intersection(y).contains(e)

  • x.isSubset(of: y) implies x.union(y) == y

  • x.isSuperset(of: y) implies x.union(y) == x

  • x.isSubset(of: y) if and only if y.isSuperset(of: x)

  • x.isStrictSuperset(of: y) if and only if x.isSuperset(of: y) && x != y

  • x.isStrictSubset(of: y) if and only if x.isSubset(of: y) && x != y