Set
Set is an associative container that contains a sorted set of unique objects. We can use Set with a group of built-in functions:
CreateWe can create a set with _fun("
set_create", values...), values is the initial data, it can be any number, any data type. It returns a handle if creating is successful.
set=_fun("set_create");//create an empty Set
set2=_fun("set_create",3,2,2,23,"good");//create a Set with data 2,3,23,good, the second 2 will be ignored
InsertWe can insert item into set with _fun("
set_insert", set, value), set is the handle, value is the item we need to insert. it returns true if inserting is successful.
_fun("set_insert",set,11);
_fun("set_insert",set,22);
_fun("set_insert",set,22);//here 22 will not be inserted because there is an existing one
FindWe can find item in set with _fun("
set_find",
set, value), set is the handle, value is the item we need to
find. it returns the found item if finding is successful.
ret=_fun("set_find",set,"good");
if(ret)
{
print(ret);//print the found value
}
EraseWe can delete the item from set with _fun("
set_erase", set, value), set is the handle, value is the item we need to delete, it returns true if deletion is successful.
We can also delete the item which is refered by
iterator with _fun("
set_erase", set, iter), iter is the
iterator, it returns the next iterator if deletion is successful.
_fun("set_erase",set,2);//delete the item 2 in Set
i=_fun("set_begin",set);//return the iterator points to the first item
i=_fun("set_erase",set,i);//delete the first item, and return the iterator points to the next item
ClearWe can clear all the items in set with _fun("
set_clear", set), set is the handle.
SizeWe can get the count of items in set with _fun("
set_size", set), se is the handle. It returns 0 if the set is empty.
TraverseSet is similar to Dictionary, it cannot be accessed by index and we can use a group of functions to loop through it:
_fun("
set_begin", set)
, _fun("
set_next", set, iter)
, _fun("
set_get", set, iter)
main()
{
set=_fun("set_create",3,11,2,2,23,"good","ok");
i=_fun("set_begin",set);//return the iterator points to the first item
while(i)
{
print(_fun("set_get",set,i));//get the item with iterator and print it
i=_fun("set_next",set,i);//return the iterator points to the next item, return null if it is end
}
}
Result:
good
ok
2
3
11
23
FreeWe need to free the set with _fun("
set_free", set) when it is not used anymore.
set=_fun("set_create",3,2);//create a set
_fun("set_erase",set,2);//delete the item 2 in set
_fun("set_free",set);//free the set
UnionUnion is to construct a new set consisting of the items in one or both set1 and set2 by using _fun("
set_union", set1, set2):
main()
{
set1=_fun("set_create",3,11,2,2,23,"good","ok");
set2=_fun("set_create",2,32,"ok",2.35);
set3=_fun("set_union",set1,set2);//compute the union of set1 and set2
//traverse set3
for(i=_fun("set_begin",set3);i!=null;i=_fun("set_next",set3,i))
print(_fun("set_get",set3,i));
_fun("set_free",set1);
_fun("set_free",set2);
_fun("set_free",set3);//we also need to free the new set
}
Result:
good
ok
2
3
11
23
32
2.350000
IntersectionIntersection is to construct a new set consisting of the items in both set1 and set2 by using _fun("
set_intersection", set1, set2):
main()
{
set1=_fun("set_create",3,11,2,2,23,"good","ok");
set2=_fun("set_create",2,32,"ok",2.35);
set3=_fun("set_intersection",set1,set2);//compute the intersection of set1 and set2
//traverse set3
for(i=_fun("set_begin",set3);i!=null;i=_fun("set_next",set3,i))
print(_fun("set_get",set3,i));
_fun("set_free",set1);
_fun("set_free",set2);
_fun("set_free",set3);//we also need to free the new set
}
Result:
ok
2
DifferenceDifference is to construct a new set consisting of the items from set1 which are not found in set2 by using _fun("
set_difference", set1, set2):
main()
{
set1=_fun("set_create",3,11,2,2,23,"good","ok");
set2=_fun("set_create",2,32,"ok",2.35);
set3=_fun("set_difference",set1,set2);//compute the difference between set1 and set2
//traverse set3
for(i=_fun("set_begin",set3);i!=null;i=_fun("set_next",set3,i))
print(_fun("set_get",set3,i));
_fun("set_free",set1);
_fun("set_free",set2);
_fun("set_free",set3);//we also need to free the new set
}
Result:
good
3
11
23