rue is returned if the invoking IntSet // is empty (i.e., an empty IntSet is always isSubsetOf // another IntSet, even if the other IntSet is also empty). // void DumpData(std::ostream& out) const // Pre: (none)
// CONSTANT
// static const int DEFAULT_CAPACITY = __10__
// IntSet::DEFAULT_CAPACITY is the initial capacity of an
// IntSet that is created by the default constructor (i.e.,
// IntSet::DEFAULT_CAPACITY is the highest # of distinct
// values "an IntSet created by the default constructor"
// can accommodate).
//
// CONSTRUCTOR
// IntSet(int initial_capacity = DEFAULT_CAPACITY)
// Post: The invoking IntSet is initialized to an empty
// IntSet (i.e., one containing no relevant elements);
// the initial capacity is given by initial_capacity if
// initial_capacity is >= 1, otherwise it is given by
// IntSet:DEFAULT_CAPACITY.
// Note: When the IntSet is put to use after construction,
// its capacity will be resized as necessary.
//
// CONSTANT MEMBER FUNCTIONS (ACCESSORS)
// int size() const
// Pre: (none)
// Post: Number of elements in the invoking IntSet is returned.
// bool isEmpty() const
// Pre: (none)
// Post: True is returned if the invoking IntSet has no relevant
// elements, otherwise false is returned.
// bool contains(int anInt) const
// Pre: (none)
// Post: true is returned if the invoking IntSet has anInt as an
// element, otherwise false is returned.
// bool isSubsetOf(const IntSet& otherIntSet) const
// Pre: (none)
// Post: True is returned if all elements of the invoking IntSet
// are also elements of otherIntSet, otherwise false is
// returned.
// By definition, true is returned if the invoking IntSet
// is empty (i.e., an empty IntSet is always isSubsetOf
// another IntSet, even if the other IntSet is also empty).
// void DumpData(std::ostream& out) const
// Pre: (none)
// Post: Contents of the invoking IntSet have been inserted into
// out with 2 spaces separating one item from another if
// if there are 2 or more items.
// IntSet unionWith(const IntSet& otherIntSet) const
// Pre: (none)
// Post: An IntSet representing the union of the invoking IntSet
// and otherIntSet is returned.
// Note: Equivalently (see postcondition of add), the IntSet
// returned is one that initially is an exact copy of the
// invoking IntSet but subsequently has all elements of
// otherIntSet added.
// IntSet intersect(const IntSet& otherIntSet) const
// Pre: (none)
// Post: An IntSet representing the intersection of the invoking
// IntSet and otherIntSet is returned.
// Note: Equivalently (see postcondition of remove), the IntSet
// returned is one that initially is an exact copy of the
// invoking IntSet but subsequently has all of its elements
// that are not also elements of otherIntSet removed.
// IntSet subtract(const IntSet& otherIntSet) const
// Pre: (none)
// Post: An IntSet representing the difference between the invoking
// IntSet and otherIntSet is returned.
// Note: Equivalently (see postcondition of remove), the IntSet
// returned is one that initially is an exact copy of the
// invoking IntSet but subsequently has all elements of
// otherIntSet removed.
//
// MODIFICATION MEMBER FUNCTIONS (MUTATORS)
// void reset()
// Pre: (none)
// Post: The invoking IntSet is reset to become an empty IntSet.
// (i.e., one containing no relevant elements).
// bool add(int anInt)
// Pre: (none)
// Post: If contains(anInt) returns false, anInt has been
// added to the invoking IntSet as a new element and
// true is returned, otherwise the invoking IntSet is
// unchanged and false is returned.
// bool remove(int anInt)
// Pre: (none)
// Post: If contains(anInt) returns true, anInt has been
// removed from the invoking IntSet and true is
// returned, otherwise the invoking IntSet is unchanged
// and false is returned.
//
// NON-MEMBER FUNCTIONS
// bool operator==(const IntSet& is1, const IntSet& is2)
// Pre: (none)
// Post: True is returned if is1 and is2 have the same elements,
// otherwise false is returned; for e.g.: {1,2,3}, {1,3,2},
// {2,1,3}, {2,3,1}, {3,1,2}, and {3,2,1} are all equal.
// Note: By definition, two empty IntSet's are equal.
//
// VALUE SEMANTICS
// Assignment and the copy constructor may be used with IntSet
// objects.
Leaving the .h file as it is, here is the CPP file commented version:
#include "IntSet.h"
#include <iostream>
#include <cassert>
using namespace std;
/*Resizes the size of the set.*/
void IntSet::resize(int new_capacity)
{
if (new_capacity < used) //If the required capacity is less than present capacity.
new_capacity = used; //Update the required capacity to present capacity.
if (new_capacity < 1) //If required capacity is less than 1.
new_capacity = 1; //Update the required capacity to 1.
capacity = new_capacity; //Now, update the capacity to required capacity.
int * newData = new int[capacity]; //Assign required number of integers to newData.
for (int i = 0; i < used; ++i) //Copy all the from previous set data, to newData.
newData[i] = data[i];
delete [] data; //Delete the previously assigned bytes.
data = newData; //Update the data with the newly assigned bytes address.
}
//Constructs a new set with specified capacity.
IntSet::IntSet(int initial_capacity) : capacity(initial_capacity), used(0)
{
if (initial_capacity < 1) //If specified capacity is less than 1.
capacity = DEFAULT_CAPACITY; //Update the capacity to DEFAULT_CAPACITY.
data = new int[initial_capacity]; //Assign required number of integers to data.
}
//Constructs a new set from the existing set src.
IntSet::IntSet(const IntSet& src) : capacity(src.capacity), used(src.used)
{
data = new int[capacity]; //Create a new set of integer space as per the given set capacity.
for (int i = 0; i < used; ++i) //Copy all integers from src to data.
data[i] = src.data[i];
}
//Destructor for Inset.
IntSet::~IntSet()
{
delete [] data; //Delete all space assigned for the set.
}
//= operator overloading. Assigns the passed set to current set.
IntSet& IntSet::operator=(const IntSet& rhs)
{
if (this != &rhs) //If passed set is not current set.
{
int* newData = new int[rhs.capacity]; //Creates new set of integers with space equal to given set capacity.
for (int i = 0; i < rhs.used; ++i) //Copies all data from given set to the newly created set.
newData[i] = rhs.data[i];
delete [] data; //Deletes current set.
data = newData; //Assigns the newly created set to current set.
capacity = rhs.capacity; //Updates the current capacity to given set capacity.
used = rhs.used; //Updates used to given set used.
}
return *this;
}
//Returns the size of current set.
int IntSet::size() const
{
return used;
}
//Returns true if current set is empty. False otherwise.
bool IntSet::isEmpty() const
{
if (used == 0)
return true;
else
return false;
}
//Checks whether given element is in the current set.
//Returns 1 if the element is found in current set. False otherwise.
int IntSet::contains(int anInt) const
{
for (int i = 0; i < used; i++) //For each element in the set.
{
if (data[i] == anInt) //If that element is equal to passed element.
return 1; //Return 1.
}
return false; //If element is not found return false.
}
//Checks whether the passed set is subset of current set.
//Returns true if so, and false otherwise.
bool IntSet::isSubsetOf(const IntSet& otherIntSet) const
{
for (int i = 0; i < used; i++) //For each element in current set.
{
if (otherIntSet.contains(data[i]) == 0) //If the element is not in passed other set.
{
return false; //Return false.
}
}
return true; //Return true otherwise.
}
/*int IntSet::newToThis(const IntSet& otherIntSet) const
{
IntSet temp = otherIntSet;
temp.subtract(*this);
return temp.size();
}
*/
//Dumps the data in current set to outStream.
void IntSet::DumpData(ostream& out) const
{ // already implemented ... DON'T change anything
if (used > 0) //If there are elements in current set.
{
out << data[0]; //Copy the first element, to output.
for (int i = 1; i < used; ++i) //For all the other elements.
out << " " << data[i]; //Send a space followed by element.
}
}
//Reset the current set, i.e., remove the elements, and set it to DEFAULT_CAPACITY.
void IntSet::reset()
{
used = 0; //Assigned used to zero.
delete [] data; //Delete the data.
int* newData = new int[DEFAULT_CAPACITY]; //Assign the data to DEFAULT_CAPACITY
data = newData;
capacity = 1; //Update the capacity to 1.
}
//Add the element to the current set, if its not in the set, and returns true.
//If the element is already in the set, return false.
bool IntSet::add(int anInt)
{
if (contains(anInt) == 0) //If the element is not in the set.
{
if (used > capacity) //If the capacity is exhausted.
resize(int(1.5*capacity) + 1); //resize the capacity.
data[used] = anInt; //Add the element to current set.
used++; //Increase the used variable by 1.
return true; //Return true.
}
return false; //Return false.
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps