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)

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

// 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.

When calling resize (while implementing some of the member functions) to increase the capacity of the dynamic arrays, use the following resizing rule (unless the new capacity has to be something else higher as dictated by other overriding factors):
"new capacity" is "roughly 1.5 * old capacity" and at least "old capacity + 1".
The latter (at least "old capacity + 1") is a simple way to take care of the subtle case where "1.5*old capacity" evaluates (with truncation) to the same as "old capacity".
(TIP: "int(1.5*old capacity) + 1" is a formula you can use to rather painlessly compute "new capacity" satisfying the above.)
Transcribed Image Text:When calling resize (while implementing some of the member functions) to increase the capacity of the dynamic arrays, use the following resizing rule (unless the new capacity has to be something else higher as dictated by other overriding factors): "new capacity" is "roughly 1.5 * old capacity" and at least "old capacity + 1". The latter (at least "old capacity + 1") is a simple way to take care of the subtle case where "1.5*old capacity" evaluates (with truncation) to the same as "old capacity". (TIP: "int(1.5*old capacity) + 1" is a formula you can use to rather painlessly compute "new capacity" satisfying the above.)
You are to provide the implementation for a second specification/design for the new data type described in Assignment 1. You are to use a dynamic (run-time), resizable array (instead of a compile-time, fixed-sized array) in the internal representation of the data type. This design removes the
upper-bound limitation (with respect to the number of distinct values that can be accommodated) associated with the compile-time, fixed-sized array version. The main changes in class specification (relative to the Assignment 1 version) are summarized below:
■
Constants replaced.
The constant MAX_SIZE has been replaced by DEFAULT_CAPACITY.
▶
The replacing constant represents the dimension of the dynamic array initially allocated by default for an IntSet object.
In other words, by default, an IntSet object initially can be used to represent a collection of up to DEFAULT_CAPACITY distinct values. When the IntSet object is put into use, its dynamic array can be (and should be) resized as needed to enable it to represent a collection of any
size (limited only by the resources of the system used).
Public members added/replaced.
The default constructor has been replaced with a one-parameter constructor that has a default value for the parameter. (Note that the default value is the constant DEFAULT_CAPACITY described above.) This constructor is able to take up the roles of two different constructors:
The (parameterless) default constructor
The constructor takes up this role if the client does not specify a value for initial_capacity (the highest number of distinct values that an IntSet object initially should be able to accommodate).
# Example client code: IntSet is;
The IntSet object constructed will initially be able to represent a collection of up to DEFAULT_CAPACITY distinct values.
O
A one-parameter constructor
O
The constructor takes up this role if the client does specify a value for initial_capacity (the highest number of distinct values that an IntSet object initially should be able to accommodate).
# Example client code: IntSet is1 (5), is2(-5), is3(0);
The IntSet object constructed will initially be able to represent a collection of up to the client-specified number of distinct values (except when the specified number is invalid, in which case the number is replaced with DEFAULT_CAPACITY).
# NOTE:
To specify an invalid initial capacity (such as when declaring is2 and is3 in the preceding example) is silly but it shouldn't break our code.
Because this version of the IntSet class uses dynamic memory, additional custom-defined public member functions (the "gang of three") are required (because the automatic versions are no longer adequate), namely:
Copy constructor,
O
Destructor, and
Overloaded assignment operator.
Private members.
The private data members data is now a pointer variable. Given an IntSet object is, the pointer member is.data will serve as the name for referencing the dynamic array (that stores the distinct values) of the collection represented by is.
NOTE:
The private data member data in Assignment 1 (which uses a compile-time array) is a pointer constant (immutable) that is initialized (by the system) to point to a fixed-sized stack-storage indigenously allocated (also by the system) at compile time. In
Assignment 2 (which uses a dynamic or run-time array), data is mutable and must be re-set whenever appropriate (by the programmer) to point to a "resizable" heap-storage exogenously allocated (also by the programmer) at run time.
A private integer variable named capacity is added for keeping track of the current highest number of distinct values that an IntSet object can accommodate.
NOTE:
In Assignment 1, the (fixed-sized, compile-time) arrays used by IntSet objects are all of the same and constant dimension, so we need only the class-level constant (MAX_SIZE) to track the dimension. In Assignment 2, the ("resizable", run-time) arrays used
by IntSet objects are (in general) of different and variable dimensions, so we need the instance-level variable (capacity) to track the dimension of each object's array as we use/manipulate the object.
A private helper function (also called utility or facilitator or auxiliary function) named resize is also added. This function is used, when necessary, by other member functions to change the dimension (thus the highest number of distinct values that an IntSet object can
accommodate) of the dynamic array referenced by data.
This function is made private (not public) because it is meant to be used by other member functions of the class, not clients of the class.
NOTE:
Non-member replaced.
The equal function has been replaced with operator==
Transcribed Image Text:You are to provide the implementation for a second specification/design for the new data type described in Assignment 1. You are to use a dynamic (run-time), resizable array (instead of a compile-time, fixed-sized array) in the internal representation of the data type. This design removes the upper-bound limitation (with respect to the number of distinct values that can be accommodated) associated with the compile-time, fixed-sized array version. The main changes in class specification (relative to the Assignment 1 version) are summarized below: ■ Constants replaced. The constant MAX_SIZE has been replaced by DEFAULT_CAPACITY. ▶ The replacing constant represents the dimension of the dynamic array initially allocated by default for an IntSet object. In other words, by default, an IntSet object initially can be used to represent a collection of up to DEFAULT_CAPACITY distinct values. When the IntSet object is put into use, its dynamic array can be (and should be) resized as needed to enable it to represent a collection of any size (limited only by the resources of the system used). Public members added/replaced. The default constructor has been replaced with a one-parameter constructor that has a default value for the parameter. (Note that the default value is the constant DEFAULT_CAPACITY described above.) This constructor is able to take up the roles of two different constructors: The (parameterless) default constructor The constructor takes up this role if the client does not specify a value for initial_capacity (the highest number of distinct values that an IntSet object initially should be able to accommodate). # Example client code: IntSet is; The IntSet object constructed will initially be able to represent a collection of up to DEFAULT_CAPACITY distinct values. O A one-parameter constructor O The constructor takes up this role if the client does specify a value for initial_capacity (the highest number of distinct values that an IntSet object initially should be able to accommodate). # Example client code: IntSet is1 (5), is2(-5), is3(0); The IntSet object constructed will initially be able to represent a collection of up to the client-specified number of distinct values (except when the specified number is invalid, in which case the number is replaced with DEFAULT_CAPACITY). # NOTE: To specify an invalid initial capacity (such as when declaring is2 and is3 in the preceding example) is silly but it shouldn't break our code. Because this version of the IntSet class uses dynamic memory, additional custom-defined public member functions (the "gang of three") are required (because the automatic versions are no longer adequate), namely: Copy constructor, O Destructor, and Overloaded assignment operator. Private members. The private data members data is now a pointer variable. Given an IntSet object is, the pointer member is.data will serve as the name for referencing the dynamic array (that stores the distinct values) of the collection represented by is. NOTE: The private data member data in Assignment 1 (which uses a compile-time array) is a pointer constant (immutable) that is initialized (by the system) to point to a fixed-sized stack-storage indigenously allocated (also by the system) at compile time. In Assignment 2 (which uses a dynamic or run-time array), data is mutable and must be re-set whenever appropriate (by the programmer) to point to a "resizable" heap-storage exogenously allocated (also by the programmer) at run time. A private integer variable named capacity is added for keeping track of the current highest number of distinct values that an IntSet object can accommodate. NOTE: In Assignment 1, the (fixed-sized, compile-time) arrays used by IntSet objects are all of the same and constant dimension, so we need only the class-level constant (MAX_SIZE) to track the dimension. In Assignment 2, the ("resizable", run-time) arrays used by IntSet objects are (in general) of different and variable dimensions, so we need the instance-level variable (capacity) to track the dimension of each object's array as we use/manipulate the object. A private helper function (also called utility or facilitator or auxiliary function) named resize is also added. This function is used, when necessary, by other member functions to change the dimension (thus the highest number of distinct values that an IntSet object can accommodate) of the dynamic array referenced by data. This function is made private (not public) because it is meant to be used by other member functions of the class, not clients of the class. NOTE: Non-member replaced. The equal function has been replaced with operator==
Expert Solution
Step 1

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

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Exception Handling Keywords
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education