Set Operations – How to Create and Modify Sets

This article covers set operations.

A set is a container and defined as a (mathematical) set of type T (set< T >), where T may be an enumeration type or one of the primitive types; it may even be a node or edge or graph type, then we speak of storages. Sets only store unique values, i.e. if one would add the same value (or element) multiple times to a set only one unique entry would be stored in the set.

Learn more about other container types here.

For handling sets there are different opreations or calls. This article covers the following set operations:

Query method calls

The query method calls on sets are:

Method CallDescription
.size()returns the number of elements in the set, as int
.empty()returns whether the set is empty, as boolean
.peek(num)returns the element which comes at position num:int in the sequence of enumeration, as T for set; the higher the number, the longer retrieval takes
.asArray()returns an array of the set content, as array for set, in enumeration order

The operator x in s denotes set membership, i.e. whether the set s contains the
given element x, and returns the result as 
boolean (true or false). Furthermore, the container may
be iterated over with a 
for loop, e.g. for(x:string in s)…, looping over the string set s with the parameter x

Update method calls

The update method calls on are:

Method CallDescription
s.add(v)Set addition: adds the value v to the set s.
s.rem(v) Set removal: removes the value v from the set s.
s.clear()Set clearing: removes all values from the set s.

Binary and comparative set operators

Boolean set operators are:

Binary OperatorDescription
| Set union (contained in resulting set as soon as contained in one of the sets)
&Set intersection (contained in resulting set only if contained in both of the sets)
\Set difference (contained in resulting set if contained in left but not right set)

Operators for set comparisons are:

Binary OperatorDescription
A == B True, if A and B are identical.
A != B True, if A and B are not identical.
A < B True, if A is a subset of B, but A and B are not identical.
A > B True, if A is a superset of B, but A and B are not identical.
A <= B True, if A is a subset of B or A and B are identical.
A >= B True, if A is a superset of B or A and B are identical.

Copy operator

To copy a set use copy(set<T>):set<T> as shown in the example below.

Example

An example explaining some operations.

function setExample(ref si:set<int>) : boolean
{
   def ref s:set<int> = set<int>{};  //create an empty integer set s
   def ref s1:set<int> = set<int>{};  //create an empty integer set s1
   s.add(1);                     // Add 1 to s { 1 }
   s.add(2);                     // Add 2 to s { 1, 2 }
   s.add(3);                     // Add 3 to s { 1, 2, 3 }, s.size()==3
   s.add(2);                     // Add 2 to s, does not change set as 2 is already contained
   s1 = copy(s);                 // Copy s to s1, s1 = { 1, 2, 3 }
   s.rem(2);                     // Remove 2 from s { 1, 3 }
   for(v:int in s) {             // Loop over all elements in the set
      si.add(v);                 // add each element to the input set si
   }                             // s == si == { 1, 3 }, assuming input was {}
   def var i:int = s.size();     // Create an integer variable and assign the set size, i==2
   if(!(i in s)) {               // if i==2 is not an element of set s 
      s.add(i);                  // then add i to the set
   }                             // results in { 1, 2, 3 }
   s.add(3);                     // IMPORTANT: still { 1, 2, 3 }, sets only contain unique values
   return (2 in (s | set<int>{ 4,5 }));    // Union results in {1,2,3,4,5}, thus true
}

Was this article helpful?

Related Articles