# Set¶

Usage

use Set;


or

import Set;


This module contains the implementation of the set type.

A set is a collection of unique elements. Sets are unordered and unindexed.

The highly parallel nature of Chapel means that great care should be taken when performing operations that may invalidate references to set elements. Adding or removing an element from a set may invalidate references to elements contained in the set.

All references to set elements are invalidated when the set is cleared or deinitialized.

Sets are not parallel safe by default, but can be made parallel safe by setting the param formal ‘parSafe to true in any set constructor. When constructed from another set, the new set will inherit the parallel safety mode of its originating set.

record set

A set is a collection of unique elements. Attempting to add a duplicate element to a set has no effect.

The set type supports a test for membership via the contains method, along with free functions for calculating the union, difference, intersection, and symmetric difference of two sets. The set type also defines the (proper) subset and (proper) superset operations by overloading common comparison operators.

Sets can be iterated over. The set type makes no guarantee of a consistent iteration order.

A set can be default initialized (containing no elements), or it may be initialized with elements that are copies of those contained by any type that supports an iterator.

The set type is not parallel safe by default. For situations in which such protections are desirable, parallel safety can be enabled by setting parSafe = true in any set constructor. A set constructed from another set inherits the parallel safety mode of that set by default.

type eltType

The type of the elements contained in this set.

param parSafe = false

If true, this set will perform parallel safe operations.

proc init(type eltType, param parSafe = false)

Initializes an empty set containing elements of the given type.

Arguments: eltType – The type of the elements of this set. parSafe – If true, this set will use parallel safe operations.
proc init(type eltType, iterable, param parSafe = false)

Initialize this set with a unique copy of each element contained in iterable. If an element from iterable is already contained in this set, it will not be added again. The formal iterable must be a type with an iterator named “these” defined for it.

Arguments: iterable – A collection of elements to add to this set. parSafe – If true, this set will use parallel safe operations.
proc init=(const ref other: ?tset?)

Initialize this set with a copy of each of the elements contained in the set other. This set will inherit the parSafe value of the set other.

Arguments: other – A set to initialize this set with.
proc add(in x: eltType)

Add a copy of the element x to this set. Does nothing if this set already contains an element equal to the value of x.

Arguments: x – The element to add to this set.
proc contains(const ref x: eltType): bool

Returns true if the given element is a member of this set, and false otherwise.

Arguments: x – The element to test for membership. Whether or not the given element is a member of this set. bool
proc isDisjoint(const ref other: eltTypeset?): bool

Returns true if this set shares no elements in common with the set other, and false otherwise.

Arguments: other – The set to compare against. Whether or not this set and other are disjoint. bool
proc isIntersecting(const ref other: eltTypeset?): bool

Returns true if this set and other have at least one element in common, and false otherwise.

Arguments: other – The set to compare against. Whether or not this set and other intersect. bool
proc remove(const ref x: eltType): bool

Attempt to remove the item from this set with a value equal to x. If an element equal to x was removed from this set, return true, else return false if no such value was found.

Warning

Removing an element from this set may invalidate existing references to the elements contained in this set.

Arguments: x – The element to remove. Whether or not an element equal to x was removed. bool
proc clear()

Clear the contents of this set.

Warning

Clearing the contents of this set will invalidate all existing references to the elements contained in this set.

iter these()

Iterate over the elements of this set.

Warning

Set iterators are currently not threadsafe. Attempting to mutate the state of a set while it is being iterated over is considered undefined behavior.

Yields: A reference to one of the elements contained in this set.
proc writeThis(ch: channel) throws

Write the contents of this set to a channel.

Arguments: ch – A channel to write to.
proc isEmpty(): bool

Returns true if this set contains zero elements.

Returns: true if this set is empty. bool
proc size

The current number of elements contained in this set.

proc toArray(): [] eltType

Returns a new DefaultRectangular array containing a copy of each of the elements contained in this set. The elements of the returned array are not guaranteed to follow any particular ordering.

Returns: An array containing a copy of each of the elements in this set. [] eltType
proc =(ref lhs: ?tset?, const ref rhs: tset?)

Clear the contents of the set lhs, then iterate through the contents of rhs and add a copy of each element to lhs.

Warning

This will invalidate any references to elements previously contained in the set lhs.

Arguments: lhs – The set to assign to. rhs – The set to assign from.
proc |(const ref a: ?tset?, const ref b: tset?): set(t)

Return a new set that contains the union of two sets.

Arguments: a – A set to take the union of. b – A set to take the union of. A new set containing the union between a and b. set(?t, ?)
proc |=(ref lhs: ?tset?, const ref rhs: tset?)

Assign to the set lhs the set that is the union of lhs and rhs.

Warning

This will invalidate any references to elements previously contained in the set lhs.

Arguments: lhs – A set to take the union of and then assign to. rhs – A set to take the union of.
proc +(const ref a: ?tset?, const ref b: tset?): tset?

Return a new set that contains the union of two sets. Alias for the | operator.

Arguments: a – A set to take the union of. b – A set to take the union of. A new set containing the union between a and b. set(?t, ?)
proc +=(ref lhs: ?tset?, const ref rhs: tset?)

Assign to the set lhs the set that is the union of lhs and rhs.

Warning

This will invalidate any references to elements previously contained in the set lhs.

Arguments: lhs – A set to take the union of and then assign to. rhs – A set to take the union of.
proc -(const ref a: ?tset?, const ref b: tset?): set(t)

Return a new set that contains the difference of two sets.

Arguments: a – A set to take the difference of. b – A set to take the difference of. A new set containing the difference between a and b. set(t)
proc -=(ref lhs: ?tset?, const ref rhs: tset?)

Assign to the set lhs the set that is the difference of lhs and rhs.

Warning

This will invalidate any references to elements previously contained in the set lhs.

Arguments: lhs – A set to take the difference of and then assign to. rhs – A set to take the difference of.
proc &(const ref a: ?tset?, const ref b: tset?): set(t)

Return a new set that contains the intersection of two sets.

Arguments: a – A set to take the intersection of. b – A set to take the intersection of. A new set containing the intersection of a and b. set(t)
proc &=(ref lhs: ?tset?, const ref rhs: tset?)

Assign to the set lhs the set that is the intersection of lhs and rhs.

Warning

This will invalidate any references to elements previously contained in the set lhs.

Arguments: lhs – A set to take the intersection of and then assign to. rhs – A set to take the intersection of.
proc ^(const ref a: ?tset?, const ref b: tset?): set(t)

Return the symmetric difference of two sets.

Arguments: a – A set to take the symmetric difference of. b – A set to take the symmetric difference of. A new set containing the symmetric difference of a and b. set(?t, ?)
proc ^=(ref lhs: ?tset?, const ref rhs: tset?)

Assign to the set lhs the set that is the symmetric difference of lhs and rhs.

Warning

This will invalidate any references to elements previously contained in the set lhs.

Arguments: lhs – A set to take the symmetric difference of and then assign to. rhs – A set to take the symmetric difference of.
proc ==(const ref a: ?tset?, const ref b: tset?): bool

Return true if the sets a and b are equal. That is, they are the same size and contain the same elements.

Arguments: a – A set to compare. b – A set to compare. true if two sets are equal. bool
proc !=(const ref a: ?tset?, const ref b: tset?): bool

Return true if the sets a and b are not equal.

Arguments: a – A set to compare. b – A set to compare. true if two sets are not equal. bool
proc <(const ref a: ?tset?, const ref b: tset?): bool

Return true if a is a proper subset of b.

Arguments: a – A set to compare. b – A set to compare. true if a is a proper subset of b. bool
proc <=(const ref a: ?tset?, const ref b: tset?): bool

Return true if a is a subset of b.

Arguments: a – A set to compare. b – A set to compare. true if a is a subset of b. bool
proc >(const ref a: ?tset?, const ref b: tset?): bool

Return true if a is a proper superset of b.

Arguments: a – A set to compare. b – A set to compare. true if a is a proper superset of b. bool
proc >=`(const ref a: ?tset?, const ref b: tset?): bool

Return true if a is a superset of b.

Arguments: a – A set to compare. b – A set to compare. true if a is a superset of b. bool