Set

Set is a container that holds a list of unsorted objects with no duplicate values. This section describes how to create, access, and modify sets, along with examples.

Creating Sets

Use the function createSet defined in the header <Util.h>. The following is the syntax to declare a set:

static Set* createSet(DATA_TYPE keyType, INDEX capacity);

Arguments

  • keyType: The data type for the elements of the created set, such as DT_INT, DT_DATE, etc.
  • capacity: The amount of memory (in terms of the number of elements) allocated to the set.

Examples

SetSP set = Util::createSet(DT_INT, 100);    //create a set of INT type with a reserved capacity of 100 elements

Operations on Sets

Listed below are some commonly utilized functions for operations on sets:

bool append(const ConstantSP& value);                                         //append 'value' to a set
bool remove(const ConstantSP& value);                                         //remove 'value' from a set
void contain(const ConstantSP& target, const ConstantSP& resultSP) const;     //determine if a set contains the element 'target' 
INDEX size() const;                                                           //get the number of elements in a set
void clear();                                                                 //remove all elements from a set

Examples

set->append(new Int(3));                      //(3)
set->append(new Int(1));                      //(3, 1)
set->append(new Int(2));                      //(3, 1, 2)
set->append(new Int(2));                      //(3, 1, 2)
ConstantSP result = new Bool;
set->contain(new Int(3), result);             //result : true
set->contain(new Int(4), result);             //result : false
set->remove(new Int(1));                      //(2, 3)
set->remove(new Int(4));                      //(2, 3)
std::cout << set->size();                     //2
set->clear();