SetAlgebraType

protocol SetAlgebraType

A generalized set whose distinct elements are not necessarily disjoint.

In a model of SetAlgebraType, some elements may subsume other elements, where

a subsumes b iff ([a] as Self).isSupersetOf([b])

In many models of SetAlgebraType such as Set<Element>, a subsumes b if and only if a == b, but that is not always the case. For example, option sets typically do not satisfy that property.

Two elements are disjoint when neither one subsumes the other.

See Also: OptionSetType.

  • Axioms, where S conforms to SetAlgebraType, x and y are of type S, and e is of type S.Element:

    • S() == []

    • x.intersect(x) == x
    • x.intersect([]) == []
    • 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) iff x.intersect(y).contains(e)
    • x.isSubsetOf(y) iff y.isSupersetOf(x)
    • x.isStrictSupersetOf(y) iff x.isSupersetOf(y) && x != y
    • x.isStrictSubsetOf(y) iff x.isSubsetOf(y) && x != y
Inheritance ArrayLiteralConvertible, Equatable View Protocol Hierarchy →
Associated Types
Element

A type for which Self provides a containment test.

Import import Swift

Initializers

init() Required

Creates an empty set.

  • Equivalent to [] as Self

Declaration

init()
init(_:)

Creates the set containing all elements of sequence.

Declaration

init<S : SequenceType where S.Generator.Element == Element>(_ sequence: S)
init(arrayLiteral:)

Create an instance initialized with elements.

Declaration

init(arrayLiteral elements: Self.Element...)

Declared In

ArrayLiteralConvertible

Instance Variables

var isEmpty: Bool

Returns true iff self.contains(e) is false for all e.

Declaration

var isEmpty: Bool { get }

Static Methods

static func element(_:isDisjointWith:)

Returns true iff a is disjoint with b.

Two elements are disjoint when neither one subsumes the other.

See Also: Self.element(, subsumes:_)

Declaration

static func element(a: Self.Element, isDisjointWith b: Self.Element) -> Bool
static func element(_:subsumes:)

Returns true iff a subsumes b.

  • Equivalent to ([a] as Self).isSupersetOf([b])

Declaration

static func element(a: Self.Element, subsumes b: Self.Element) -> Bool

Instance Methods

func ==(_:rhs:) Required

Returns true if lhs is equal to rhs.

Equality implies substitutability. When x == y, x and y are interchangeable in any code that only depends on their values.

Class instance identity as distinguished by triple-equals === is notably not part of an instance's value. Exposing other non-value aspects of Equatable types is discouraged, and any that are exposed should be explicitly pointed out in documentation.

Equality is an equivalence relation

  • x == x is true
  • x == y implies y == x
  • x == y and y == z implies x == z

Inequality is the inverse of equality, i.e. !(x == y) iff x != y.

Declaration

func ==(lhs: Self, rhs: Self) -> Bool

Declared In

Equatable
func contains(_:) Required

Returns true if self contains member.

  • Equivalent to self.intersect([member]) == [member]

Declaration

func contains(member: Self.Element) -> Bool
func exclusiveOr(_:) Required

Returns the set of elements contained in self or in other, but not in both self and other.

Declaration

func exclusiveOr(other: Self) -> Self
mutating func exclusiveOrInPlace(_:) Required

Replaces self with a set containing all elements contained in either self or other, but not both.

  • Equivalent to replacing self with self.exclusiveOr(other)

Declaration

mutating func exclusiveOrInPlace(other: Self)
mutating func insert(_:) Required

If member is not already contained in self, inserts it.

  • Equivalent to self.unionInPlace([member]) Postcondition: self.contains(member)

Declaration

mutating func insert(member: Self.Element)
func intersect(_:) Required

Returns the set of elements contained in both self and other.

Declaration

func intersect(other: Self) -> Self
mutating func intersectInPlace(_:) Required

Removes all elements of self that are not also present in other.

  • Equivalent to replacing self with self.intersect(other) Postcondition: self.isSubsetOf(other)

Declaration

mutating func intersectInPlace(other: Self)
func isDisjointWith(_:)

Returns true iff self.intersect(other).isEmpty.

Declaration

func isDisjointWith(other: Self) -> Bool
func isSubsetOf(_:)

Returns true iff every element of self is contained in other.

Declaration

func isSubsetOf(other: Self) -> Bool
func isSupersetOf(_:)

Returns true iff every element of other is contained in self.

Declaration

func isSupersetOf(other: Self) -> Bool
mutating func remove(_:) Required

If member is contained in self, removes and returns it. Otherwise, removes all elements subsumed by member and returns nil.

Postcondition: self.intersect([member]).isEmpty

Declaration

mutating func remove(member: Self.Element) -> Self.Element?
func subtract(_:)

Returns the set of elements contained in self but not in other.

Declaration

func subtract(other: Self) -> Self
mutating func subtractInPlace(_:)

Removes all elements of other from self.

  • Equivalent to replacing self with self.subtract(other).

Declaration

mutating func subtractInPlace(other: Self)
func union(_:) Required

Returns the set of elements contained in self, in other, or in both self and other.

Declaration

func union(other: Self) -> Self
mutating func unionInPlace(_:) Required

Insert all elements of other into self.

  • Equivalent to replacing self with self.union(other). Postcondition: self.isSupersetOf(other)

Declaration

mutating func unionInPlace(other: Self)

Default Implementations

init(_:)

Creates the set containing all elements of sequence.

Declaration

init<S : SequenceType where S.Generator.Element == Element>(_ sequence: S)
init(arrayLiteral:)

Creates a set containing all elements of the given arrayLiteral.

This initializer allows an array literal containing Self.Element to represent an instance of the set, wherever it is implied by the type context.

Declaration

init(arrayLiteral: Self.Element...)
var isEmpty: Bool

Returns true iff self.contains(e) is false for all e.

Declaration

var isEmpty: Bool { get }
static func element(_:isDisjointWith:)

Returns true iff a is disjoint with b.

Two elements are disjoint when neither one subsumes the other.

See Also: Self.element(, subsumes:_)

Declaration

static func element(a: Self.Element, isDisjointWith b: Self.Element) -> Bool
static func element(_:subsumes:)

Returns true iff a subsumes b.

  • Equivalent to ([a] as Self).isSupersetOf([b])

Declaration

static func element(a: Self.Element, subsumes b: Self.Element) -> Bool
func isDisjointWith(_:)

Returns true iff self.intersect(other).isEmpty.

Declaration

func isDisjointWith(other: Self) -> Bool
func isStrictSubsetOf(_:)

Returns true iff every element of self is contained in other and other contains an element that is not contained in self.

Declaration

func isStrictSubsetOf(other: Self) -> Bool
func isStrictSupersetOf(_:)

Returns true iff every element of other is contained in self and self contains an element that is not contained in other.

Declaration

func isStrictSupersetOf(other: Self) -> Bool
func isSubsetOf(_:)

Returns true iff every element of self is contained in other.

Declaration

func isSubsetOf(other: Self) -> Bool
func isSupersetOf(_:)

Returns true iff every element of other is contained in self.

Declaration

func isSupersetOf(other: Self) -> Bool
func subtract(_:)

Returns the set of elements contained in self but not in other.

Declaration

func subtract(other: Self) -> Self
mutating func subtractInPlace(_:)

Removes all elements of other from self.

  • Equivalent to replacing self with self.subtract(other).

Declaration

mutating func subtractInPlace(other: Self)