protocol
SetAlgebra
Inheritance |
Equatable, ExpressibleByArrayLiteral
View Protocol Hierarchy →
|
---|---|
Associated Types |
A type for which the conforming type provides a containment test. |
Import |
|
Initializers
Creates an empty set.
This initializer is equivalent to initializing with an empty array
literal. For example, you create an empty Set
instance with either
this initializer or with an empty array literal.
Declaration
init
()
Creates a new set from a finite sequence of items.
Use this initializer to create a new set from an existing sequence, like an array or a range:
let
validIndices
=
Set
(
0
..
<
7
).
subtracting
([
2
,
4
,
5
])
(
validIndices
)
// Prints "[6, 0, 1, 3]"
sequence
: The elements to use as members of the new set.
Declaration
init
<
S
:
Sequence
where
S
.
Iterator
.
Element
==
Element
>
(
_
sequence
:
S
)
Creates an instance initialized with the given elements.
Declaration
init
(
arrayLiteral
elements
:
Self
.
Element
...)
Declared In
ExpressibleByArrayLiteral
1 inherited item hidden. (Show all)
Instance Variables
A Boolean value that indicates whether the set has no elements.
Declaration
var
isEmpty
:
Bool
{
get
}
Instance Methods
Returns a Boolean value indicating whether two values are equal.
Equality is the inverse of inequality. For any values a
and b
,
a == b
implies that a != b
is false
.
Parameters: lhs: A value to compare. rhs: Another value to compare.
Declaration
func
==(
lhs
:
Self
,
rhs
:
Self
) -
>
Bool
Declared In
Equatable
Returns a Boolean value that indicates whether the given element exists in the set.
This example uses the contains(_:)
method to test whether an integer is
a member of a set of prime numbers.
let
primes
:
Set
= [
2
,
3
,
5
,
7
,
11
,
13
,
17
,
19
,
23
,
29
,
31
,
37
]
let
x
=
5
if
primes
.
contains
(
x
) {
(
"\(
x
) is prime!"
)
}
else
{
(
"\(
x
). Not prime."
)
}
// Prints "5 is prime!"
member
: An element to look for in the set.
Returns: true
if member
exists in the set; otherwise, false
.
Declaration
func
contains
(
_
member
:
Self
.
Element
) -
>
Bool
Removes the elements of this set that aren't also in the given set.
In the following example, the elements of the employees
set that are
not also members of the neighbors
set are removed. In particular, the
names "Alicia"
, "Chris"
, and "Diana"
are removed.
other
: A set of the same type as the current set.
Declaration
mutating
func
formIntersection
(
_
other
:
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.
In the following example, the elements of the employees
set that are
also members of neighbors
are removed from employees
, while the
elements of neighbors
that are not members of employees
are added to
employees
. In particular, the names "Alicia"
, "Chris"
, and
"Diana"
are removed from employees
while the names "Forlani"
and
"Greta"
are added.
other
: A set of the same type.
Declaration
mutating
func
formSymmetricDifference
(
_
other
:
Self
)
Adds the elements of the given set to the set.
In the following example, the elements of the visitors
set are added to
the attendees
set:
If the set already contains one or more elements that are also in
other
, the existing members are kept.
var
initialIndices
=
Set
(
0
..
<
5
)
initialIndices
.
formUnion
([
2
,
3
,
6
,
7
])
(
initialIndices
)
// Prints "[2, 4, 6, 7, 0, 1, 3]"
other
: A set of the same type as the current set.
Declaration
mutating
func
formUnion
(
_
other
:
Self
)
Inserts the given element in the set if it is not already present.
If an element equal to newMember
is already contained in the set, this
method has no effect. In this example, a new element is inserted into
classDays
, a set of days of the week. When an existing element is
inserted, the classDays
set does not change.
enum
DayOfTheWeek
:
Int
{
case
sunday
,
monday
,
tuesday
,
wednesday
,
thursday
,
friday
,
saturday
}
var
classDays
:
Set
<
DayOfTheWeek
>
= [.
wednesday
, .
friday
]
(
classDays
.
insert
(.
monday
))
// Prints "(true, .monday)"
(
classDays
)
// Prints "[.friday, .wednesday, .monday]"
(
classDays
.
insert
(.
friday
))
// Prints "(false, .friday)"
(
classDays
)
// Prints "[.friday, .wednesday, .monday]"
newMember
: An element to insert into the set.
Returns: (true, newMember)
if newMember
was not contained in the
set. If an element equal to newMember
was already contained in the
set, the method returns (false, oldMember)
, where oldMember
is the
element that was equal to newMember
. In some cases, oldMember
may
be distinguishable from newMember
by identity comparison or some
other means.
Declaration
mutating
func
insert
(
_
newMember
:
Self
.
Element
) -
>
(
inserted
:
Bool
,
memberAfterInsert
:
Self
.
Element
)
Returns a new set with the elements that are common to both this set and the given set.
In the following example, the bothNeighborsAndEmployees
set is made up
of the elements that are in both the employees
and neighbors
sets.
Elements that are in either one or the other, but not both, are left out
of the result of the intersection.
other
: A set of the same type as the current set.
Returns: A new set.
Note: if this set and other
contain elements that are equal but
distinguishable (e.g. via ===
), which of these elements is present
in the result is unspecified.
Declaration
func
intersection
(
_
other
:
Self
) -
>
Self
Returns a Boolean value that indicates whether the set has no members in common with the given set.
In the following example, the employees
set is disjoint with the
visitors
set because no name appears in both sets.
other
: A set of the same type as the current set.
Returns: true
if the set has no elements in common with other
;
otherwise, false
.
Declaration
func
isDisjoint
(
with
other
:
Self
) -
>
Bool
Returns a Boolean value that indicates whether the set is a subset of another set.
Set A is a subset of another set B if every member of A is also a member of B.
other
: A set of the same type as the current set.
Returns: true
if the set is a subset of other
; otherwise, false
.
Declaration
func
isSubset
(
of
other
:
Self
) -
>
Bool
Returns a Boolean value that indicates whether the set is a superset of the given set.
Set A is a superset of another set B if every member of B is also a member of A.
other
: A set of the same type as the current set.
Returns: true
if the set is a superset of possibleSubset
;
otherwise, false
.
Declaration
func
isSuperset
(
of
other
:
Self
) -
>
Bool
Removes the given element and any elements subsumed by the given element.
member
: The element of the set to remove.
Returns: For ordinary sets, an element equal to member
if member
is
contained in the set; otherwise, nil
. In some cases, a returned
element may be distinguishable from newMember
by identity comparison
or some other means.
For sets where the set type and element type are the same, like
OptionSet
types, this method returns any intersection between the set
and [member]
, or nil
if the intersection is empty.
Declaration
mutating
func
remove
(
_
member
:
Self
.
Element
) -
>
Self
.
Element
?
Removes the elements of the given set from this set.
In the following example, the elements of the employees
set that are
also members of the neighbors
set are removed. In particular, the
names "Bethany"
and "Eric"
are removed from employees
.
other
: A set of the same type as the current set.
Declaration
mutating
func
subtract
(
_
other
:
Self
)
Returns a new set containing the elements of this set that do not occur in the given set.
In the following example, the nonNeighbors
set is made up of the
elements of the employees
set that are not elements of neighbors
:
other
: A set of the same type as the current set.
Returns: A new set.
Declaration
func
subtracting
(
_
other
:
Self
) -
>
Self
Returns a new set with the elements that are either in this set or in the given set, but not in both.
In the following example, the eitherNeighborsOrEmployees
set is made up
of the elements of the employees
and neighbors
sets that are not in
both employees
and neighbors
. In particular, the names "Bethany"
and "Eric"
do not appear in eitherNeighborsOrEmployees
.
other
: A set of the same type as the current set.
Returns: A new set.
Declaration
func
symmetricDifference
(
_
other
:
Self
) -
>
Self
Returns a new set with the elements of both this and the given set.
In the following example, the attendeesAndVisitors
set is made up
of the elements of the attendees
and visitors
sets:
let
attendees
:
Set
= [
"Alicia"
,
"Bethany"
,
"Diana"
]
let
visitors
= [
"Marcia"
,
"Nathaniel"
]
let
attendeesAndVisitors
=
attendees
.
union
(
visitors
)
(
attendeesAndVisitors
)
// Prints "["Diana", "Nathaniel", "Bethany", "Alicia", "Marcia"]"
If the set already contains one or more elements that are also in
other
, the existing members are kept.
let
initialIndices
=
Set
(
0
..
<
5
)
let
expandedIndices
=
initialIndices
.
union
([
2
,
3
,
6
,
7
])
(
expandedIndices
)
// Prints "[2, 4, 6, 7, 0, 1, 3]"
other
: A set of the same type as the current set.
Returns: A new set with the unique elements of this set and other
.
Note: if this set and other
contain elements that are equal but
distinguishable (e.g. via ===
), which of these elements is present
in the result is unspecified.
Declaration
func
union
(
_
other
:
Self
) -
>
Self
Inserts the given element into the set unconditionally.
If an element equal to newMember
is already contained in the set,
newMember
replaces the existing element. In this example, an existing
element is inserted into classDays
, a set of days of the week.
newMember
: An element to insert into the set.
Returns: For ordinary sets, an element equal to newMember
if the set
already contained such a member; otherwise, nil
. In some cases, the
returned element may be distinguishable from newMember
by identity
comparison or some other means.
For sets where the set type and element type are the same, like
OptionSet
types, this method returns any intersection between the
set and [newMember]
, or nil
if the intersection is empty.
Declaration
mutating
func
update
(
with
newMember
:
Self
.
Element
) -
>
Self
.
Element
?
1 inherited item hidden. (Show all)
Default Implementations
Creates a new set from a finite sequence of items.
Use this initializer to create a new set from an existing sequence, like an array or a range:
let
validIndices
=
Set
(
0
..
<
7
).
subtracting
([
2
,
4
,
5
])
(
validIndices
)
// Prints "[6, 0, 1, 3]"
sequence
: The elements to use as members of the new set.
Declaration
init
<
S
:
Sequence
where
S
.
Iterator
.
Element
==
Element
>
(
_
sequence
:
S
)
Creates a set containing the elements of the given array literal.
Do not call this initializer directly. It is used by the compiler when you use an array literal. Instead, create a new set using an array literal as its value by enclosing a comma-separated list of values in square brackets. You can use an array literal anywhere a set is expected by the type context.
Here, a set of strings is created from an array literal holding only strings:
let
ingredients
:
Set
= [
"cocoa beans"
,
"sugar"
,
"cocoa butter"
,
"salt"
]
if
ingredients
.
isSuperset
(
of
: [
"sugar"
,
"salt"
]) {
(
"Whatever it is, it's bound to be delicious!"
)
}
// Prints "Whatever it is, it's bound to be delicious!"
arrayLiteral
: A list of elements of the new set.
Declaration
init
(
arrayLiteral
:
Self
.
Element
...)
A Boolean value that indicates whether the set has no elements.
Declaration
var
isEmpty
:
Bool
{
get
}
Returns a Boolean value that indicates whether the set has no members in common with the given set.
In the following example, the employees
set is disjoint with the
visitors
set because no name appears in both sets.
other
: A set of the same type as the current set.
Returns: true
if the set has no elements in common with other
;
otherwise, false
.
Declaration
func
isDisjoint
(
with
other
:
Self
) -
>
Bool
Returns a Boolean value that indicates whether this set is a strict subset of the given set.
Set A is a strict subset of another set B if every member of A is also a member of B and B contains at least one element that is not a member of A.
other
: A set of the same type as the current set.
Returns: true
if the set is a strict subset of other
; otherwise,
false
.
Declaration
func
isStrictSubset
(
of
other
:
Self
) -
>
Bool
Returns a Boolean value that indicates whether this set is a strict superset of the given set.
Set A is a strict superset of another set B if every member of B is also a member of A and A contains at least one element that is not a member of B.
other
: A set of the same type as the current set.
Returns: true
if the set is a strict superset of other
; otherwise,
false
.
Declaration
func
isStrictSuperset
(
of
other
:
Self
) -
>
Bool
Returns a Boolean value that indicates whether the set is a subset of another set.
Set A is a subset of another set B if every member of A is also a member of B.
other
: A set of the same type as the current set.
Returns: true
if the set is a subset of other
; otherwise, false
.
Declaration
func
isSubset
(
of
other
:
Self
) -
>
Bool
Returns a Boolean value that indicates whether the set is a superset of the given set.
Set A is a superset of another set B if every member of B is also a member of A.
other
: A set of the same type as the current set.
Returns: true
if the set is a superset of other
; otherwise,
false
.
Declaration
func
isSuperset
(
of
other
:
Self
) -
>
Bool
Removes the elements of the given set from this set.
In the following example, the elements of the employees
set that are
also members of the neighbors
set are removed. In particular, the
names "Bethany"
and "Eric"
are removed from employees
.
other
: A set of the same type as the current set.
Declaration
mutating
func
subtract
(
_
other
:
Self
)
Returns a new set containing the elements of this set that do not occur in the given set.
In the following example, the nonNeighbors
set is made up of the
elements of the employees
set that are not elements of neighbors
:
other
: A set of the same type as the current set.
Returns: A new set.
Declaration
func
subtracting
(
_
other
:
Self
) -
>
Self
A type that provides mathematical set operations.
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 theSet
type with elements of any hashable type, or you can easily create bit masks withSetAlgebra
conformance using theOptionSet
protocol. See those types for more information.Note: Unlike ordinary set types, the
Element
type of anOptionSet
is identical to theOptionSet
type itself. TheSetAlgebra
protocol is specifically designed to accommodate both kinds of set.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 thatS
is a custom type that conforms to theSetAlgebra
protocol,x
andy
are instances ofS
, ande
is of typeS.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)
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
See Also:
OptionSet
,Set