/*
		 * This function takes the difference of a set with the current set (A-B).
		 * @param b: the set with which the difference operation is to be done (B).
		 * @return c: the set formed after the difference operation (A-B).
		 */
		set set_difference(set b)
		{
			set c;

			/* for each member, checking if it is present in current set, and not in set b. */
			for(int i=range_start; i<=range_end; i++)
				if(contains(i) && !b.contains(i))
					c.add(i);

			return c;
		}
		/*
		 * This function takes the intersection of a set with the current set.
		 * @param b: the set with which the intersection operation is to be done.
		 * @return c: the set formed after the intersection operation.
		 */
		set set_intersection(set b)
		{
			set c;

			/* for each number in range check if it is present in b and and in the current set, then add. */
			for(int i=range_start; i<=range_end; i++)
			{
				if(b.contains(i) && contains(i))
					c.add(i);
			}

			return c;
		}
		/*
		 * This function takes the union of a set with the current set.
		 * @param b: the set with which the union operation is to be done.
		 * @return c: the set formed after the union operation.
		 */
		set set_union(set b)
		{
			set c;

			/* for each number in range check if it is present in b or in current set, then add. */
			for(int i=range_start; i<=range_end; i++)
			{
				if(b.contains(i) || contains(i))
					c.add(i);
			}

			return c;
		}
/*
 * This function writes the contents of a set to a file.
 * @param filename: name of the file to be written to.
 * @param s: set that has to be written to file.
 * @return void: doesnt return anything.
 */
void write_set(string filename, set s)
{
	int num;

	/* Opening the given file. */
	FILE *output = fopen(filename.c_str(), "w");

	/* Error while opening file. */
	if(output==NULL)
	{
		cout<<"ERROR: could not open file "<<filename<<".\n";
		exit(0);
	}

	/* if the set contains a number in the range, write to file. */
	for(int i=range_start; i<=range_end; i++)
		if(s.contains(i))
			fprintf(output, "%d\n", i);

	/* closing the file. */
	fclose(output);
}
Example #5
0
static int n_instancecount;
class instancecount {
public:
	instancecount() { n_instancecount++; }
	
	bool operator==(const instancecount& other) const { return true; }
	size_t hash() const { return 0; }
};

test("set", "array", "set")
{
	{
		set<int> item;
		uint32_t set_flags;
		
		assert(!item.contains(1));
		assert(!item.contains(2));
		
		set_flags = 0;
		for (int m : item) { assert(!(set_flags & (1<<m))); set_flags |= 1<<m; }
		assert_eq(set_flags, 0);
		
		item.add(1);
		assert(item.contains(1));
		assert(!item.contains(2));
		
		set_flags = 0;
		for (int m : item) { assert(!(set_flags & (1<<m))); set_flags |= 1<<m; }
		assert_eq(set_flags, 1<<1);
		
		item.add(2);