예제 #1
0
void subtract(const Multiset& ms1, const Multiset& ms2, Multiset& result)
{
	//make sure result is empty
	if (result.size() != 0)
	{
		Multiset temp;
		result = temp;
	}

	for (int m1 = 0; m1 < ms1.uniqueSize(); m1++)
	{
		ItemType m1_val;
		int n1 = ms1.get(m1, m1_val);
		for (int m2 = 0; m2 < ms2.uniqueSize(); m2++)
		{
			ItemType m2_val;
			int n2 = ms2.get(m2, m2_val);

			if (m1_val == m2_val && n1 > n2)
			{
				for (int count = n1-n2; count > 0; count--) //insert n1-n2 number of iterations
					result.insert(m1_val);
			}
		}
	}
}
예제 #2
0
inline void test_multiset_insert(Multiset c)
{
    using phx::arg_names::arg1;
    using phx::arg_names::arg2;
    using phx::arg_names::arg3;

    typename Multiset::value_type const value = *c.begin();
    typename Multiset::iterator c_begin = c.begin();
    std::size_t old_size = c.size();
    // wrapper for
    // iterator insert(iterator where, const value_type& val);
    typename Multiset::iterator it =
        phx::insert(arg1, arg2, arg3)(c, c_begin, value);

    if (test(*it != value || c.size() != old_size + 1)) {
        cerr << "Failed " << typeid(Multiset).name()
       << " test_multiset_insert 1\n";
        return;
    }

    // wrapper for
    // iterator insert(const value_type& val);
    typename Multiset::value_type const value2(1400);
    it = phx::insert(arg1, arg2)(c, value2);
    if (test(it == c.end())) {
        cerr << "Failed " << typeid(Multiset).name()
       << " test_multiset_insert 2\n";
        return;
    }

    // wrapper for
    // template<class InIt>
    // void insert(InIt first, InIt last);
    Multiset const const_c = build_assoc<Multiset>();
    typename Multiset::size_type size = c.size();
    phx::insert(arg1, const_c.begin(), const_c.end())(c);
    if (test(c.size() != size + const_c.size())) {
        cerr << "Failed " << typeid(Multiset).name()
       << " test_multiset_insert 3\n";
        return;
    }
}
예제 #3
0
void test()
{
    Multiset ulms;
    assert(ulms.insert(20));
    assert(ulms.insert(10));
    assert(ulms.insert(20));
    assert(ulms.insert(30));
    assert(ulms.insert(20));
    assert(ulms.insert(10));
    assert(ulms.size() == 6  &&  ulms.uniqueSize() == 3);
    assert(ulms.count(10) == 2);
    assert(ulms.count(20) == 3);
    assert(ulms.count(30) == 1);
    assert(ulms.count(40) == 0);
}