Skip to content

Instantly share code, notes, and snippets.

@MattIPv4
Created April 16, 2020 12:04
Show Gist options
  • Save MattIPv4/e4d23f69574ce79b856661152ea9f485 to your computer and use it in GitHub Desktop.
Save MattIPv4/e4d23f69574ce79b856661152ea9f485 to your computer and use it in GitHub Desktop.
/**
* Exercise 3
* This project will serve as a utility to present SET operations. The investigated operations are:
*/
namespace SetOps
{
/**
* Determine if an integer is contained within a vector.
*
* @param items the vector of integers to search.
* @param item the item to search for.
* @return if the item was found, as a boolean.
*/
bool belongs(const vector<int> &items, int item)
{
// Linear search isn't very efficient, but it works
for (auto &it : items)
if (it == item)
return true;
return false;
}
/**
* Determine if the given inner is a complete subset of the outer.
*
* @param outer the vector outer to check has a subset of the inner.
* @param inner the inner to check is a subset of the outer.
* @return if the inner is a subset of the outer, as a boolean.
*/
bool is_subset(const vector<int> &outer, const vector<int> &inner)
{
for (auto &it : inner)
if (!belongs(outer, it))
return false;
return true;
}
/**
* Find the union of two given vectors (all unique items found in the two vectors).
* This assumes that the given vectors are sets (contain only unique items).
*
* @param one the first set of items to consider in the union.
* @param two the second set of items for the union.
* @return the union of the two set, as a vector.
*/
vector<int> do_union(const vector<int> &one, const vector<int> &two)
{
vector<int> result = one;
for (auto &it : two)
if (!belongs(result, it))
result.push_back(it);
return result;
}
/**
* Find the intersection of two vectors (items found in both vectors).
*
* @param one the first set of items to consider in the intersection.
* @param two the second set of items for the intersection.
* @return the intersection of the two set, as a vector.
*/
vector<int> do_intersect(const vector<int> &one, const vector<int> &two)
{
vector<int> result;
for (auto &it : one)
if (belongs(two, it))
result.push_back(it);
return result;
}
/**
* Find the relative complement of two in one (items in the second vector not in the first).
*
* @param one the first set of items to consider in the complement.
* @param two the second set of items for the complement.
* @return the relative complement of the second set in the first, as a vector.
*/
vector<int> do_relative_complement(const vector<int> &one, const vector<int> &two)
{
vector<int> result;
for (auto &it : two)
if (!belongs(one, it))
result.push_back(it);
return result;
}
/**
* Find the symmetric difference of two vectors (items in each vector that are not shared).
*
* @param one the first set of items to consider in the difference.
* @param two the second set of items for the difference.
* @return the symmetric difference of the two sets, as a vector.
*/
vector<int> do_symmetric_difference(const vector<int> &one, const vector<int> &two)
{
vector<int> result;
for (auto &it : one)
if (!belongs(two, it))
result.push_back(it);
for (auto &it : two)
if (!belongs(one, it))
result.push_back(it);
return result;
}
/**
* Output a vector of integers in a formatted fashion for debugging.
*
* @param items the vector of integers to output.
*/
void output(const vector<int> &items)
{
const int end = items.back();
cout << "{ ";
for (auto &it : items)
cout << it << (it == end ? "" : ", ");
cout << " }" << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment