Example #1
0
void subtract(const Set& s1, const Set& s2, Set& result)
{
    Set A = s1;
    Set B = s2;
    Set C;
    result = C; //clear the contents of result by setting it equal to empty Set C
    
    for (int a = 0; a < B.size(); a++)
    {
        ItemType value;
        B.get(a, value);
        if (A.contains(value))
        {
            A.erase(value);     //if A contains a value that is also in B, delete it from A
        }
        
    }
    
    for (int d = 0; d < A.size(); d++)
    {
        ItemType value;
        A.get(d, value);
        result.insert(value);   //insert these into result, which now only contains values from A and not B
    }
}
Example #2
0
void test()
{
    Set uls;
    assert(uls.insert(10));
    assert(uls.insert(20));
    assert(uls.size() == 2);
    assert(uls.contains(20));
    ItemType x = 30;
    assert(uls.get(0, x)  &&  (x == 10  ||  x == 20));
}
void subtract(const Set& s1, const Set& s2, Set& result)
{
	result=s1; //assignment operator creates new copy of s1
	int loops=s2.size(); //want to check every element of s2
	for (int i=1;i<=loops;i++)
	{
		ItemType value;
		s2.get(i, value);
		result.erase(value);//won't do anything if value in s2 is not in s1
	}
}
void unite(const Set& s1, const Set& s2, Set& result)
{
	result=s1; //assignment operator creates new copy of s1
	int loops=s2.size(); //want to check every element of s2
	for (int i=1;i<=loops;i++)
	{
		ItemType value;
		s2.get(i, value);
		result.insert(value);//function doesn't do anything if value already in s1
	}
}
Example #5
0
void unite(const Set& s1, const Set& s2, Set& result)
{
    Set A = s1;
    Set B = s2;
    result = A;
         
    for (int a = 0; a < A.size(); a++)
    {
        ItemType value;
        A.get(a, value);
        if (B.contains(value))
        {
            
            B.erase(value);    //if set B contains a value in A, then erase it from B
        }
    }
    for (int c = 0; c < B.size(); c++)  
    {
        ItemType value;
        B.get(c, value);
        result.insert(value);       //now insert every value in B into result (these don't contain any DUPLICATES from A)
    }
    
}
Example #6
0
void test()
{
    Set<int> si;
    Set<string> ss;
    assert(si.empty());
    assert(ss.size() == 0);
    assert(si.insert(10));
    assert(ss.insert("hello"));
    assert(si.contains(10));
    assert(si.erase(10));
    string s;
    assert(ss.get(0, s)  &&  s == "hello");
    Set<string> ss2(ss);
    ss.swap(ss2);
    ss = ss2;
    unite(si,si,si);
    unite(ss,ss2,ss);
    subtract(si,si,si);
    subtract(ss,ss2,ss);
    cout << "Passed all tests" << endl;
}
Example #7
0
int main()
{
	//General mutators tests
	Set s1;
	s1.insert("focaccia");
	assert(!s1.contains(""));
	s1.insert("tortilla");
	s1.insert("");
	s1.insert("lavash");
	assert(s1.contains(""));
	s1.erase("focaccia");
	assert(s1.size() == 3 && s1.contains("lavash") && s1.contains("tortilla") &&
		s1.contains(""));
	Set setex;
	assert(setex.empty());
	setex.insert("first");
	assert(setex.size() == 1 && setex.contains("first") && !setex.contains("second"));
	setex.erase("first");
	assert(setex.size() == 0);



	//

	//swap test
	Set ss1;
	ss1.insert("injera");
	Set ss2;
	ss2.insert("matzo");
	ss2.insert("dosa");
	ss1.swap(ss2);
	assert(ss1.size() == 2 && ss1.contains("matzo") && ss1.contains("dosa") &&
		ss2.size() == 1 && ss2.contains("injera"));
	//end swap test

	//get tests
	Set ss;
	ss.insert("A");
	ss.insert("C");
	ss.insert("A");
	ss.insert("B");
	string all;
	for (int k = 0; k < ss.size(); k++)
	{
		string x;
		ss.get(k, x);
		all += x;
	}

	Set SS;
	SS.insert("pita");
	SS.insert("roti");
	string test;
	assert(SS.get(1, test) && (test == "roti" || test == "pita"));
	string s2;
	assert(SS.get(1, s2) && s2 == test);

	Set s;
	assert(s.empty());
	string x = "arepa";
	assert(!s.get(42, x) && x == "arepa"); // x unchanged by get failure
	s.insert("chapati");
	assert(s.size() == 1);
	assert(s.get(0, x) && x == "chapati");
	//end get tests

	cout << "Passed all tests" << endl;
}