Introduction For this project you will implement a Set using a hash based chain implementation (each bucket can contain multiple elements). template class SetT { public: SetT(); SetT(int numBucks);...

1 answer below »
Need debug and write a code to output the result. I left common on the sett.h and setdatastructure.cpp which shows the code is not work, need fix.


Introduction For this project you will implement a Set using a hash based chain implementation (each bucket can contain multiple elements). template class SetT { public: SetT(); SetT(int numBucks); ~SetT(); void Add(T elem); void Remove(T elem); bool Contains(T elem); void Union(SetT otherSet); void Difference(SetT otherSet); void Intersection(SetT otherSet); void Filter(bool fnc(T elem)); void Traverse(void visit(T& elem)); int Size() { return numElems }; SetT operator+(T elem); // Add SetT operator+(SetT& other); // Union SetT operator*(SetT& other); // Intersection SetT operator-(T elem); // Remove SetT operator-(SetT& other); // Difference private: forward_list** buckets; // An array of forward_list's (ie, each index is a forward_list pointer) int numBuckets; int getHashIndex(const T& elem); int numElems; // Any other private functions and variables you need }; Your job is to implement each of the methods in the header class. You are also responsible for creating a test driver (similar to Chunklist) that takes input files and generates output files. You only need to test using SetT sets. forward_list Each bucket contains should contain a linked list of elements of type T. For this assignment, you will be using a Standard Template Library (STL) implementation of a singly linked list. Here is a code snippet that should be useful: #include #include int main () { std::forward_list mylist; mylist.push_front(42); mylist.push_front(55); mylist.push_front(32); mylist.push_front(5); std::cout < "mylist="" contains:";="" for="" (="" auto="" it="mylist.begin();" it="" !="mylist.end();" ++it="" )="" std::cout="">< '="" '="">< *it;="" std::cout="">< '\n';="" return="" 0;="" }="" operator="" overloading="" operator="" overloading="" allows="" the="" programmer="" to="" override="" the="" behavior="" of="" operators="" such="" as="" +,="" -,="">, and<. while="" this="" may="" sound="" complicated,="" it="" actually="" straight="" forward.="" the="" following="" shows="" how="" we="" overload="" the="" +="" operator="" to="" add="" and="" element="" to="" the="" set:=""> SetT SetT::operator+(T elem) { this->Add(elem); return result; } Note that the operator behavior is based on the type of the argument on the right side. In this case, the argument is of type T. If the argument was of type SetT, then the + would indicate Union. Test Driver You should have a test driver similar to the list driver from the previous program. In particular, it should support the following commands: • Add (this should test add and the + operator) • Remove (this should test remove and the - operator) • Intersection (this should test intersection and the * operator) • Union (this should test union and the + operator) • Difference (this should test union and the + operator) • Size • Print Deliverables • Your template class implementations for SetT • Your test driver • Input to the test driver demonstrating functionality • The output file generated from the test driver Introduction forward_list Operator Overloading Test Driver Part 3: Questions Deliverables // Need a test drive, to output the result to txt file. #include "SetT.h" #include #include #include using namespace std; void display(int& item) { cout<><><"\n"; }="" int="" main()="" {="" fstream="" inputfile;="" fstream="" outputfile;="" outputfile.open("outfile.txt",="" ios::in);="" you="" should="" create="" your="" own="" test="" driver="" that="" takes="" input="" files="" and="" generates="" output="" files.="" the="" input/output="" should="" demonstrate="" functionality="" of="" the="" sett="" adt="" (the="" input="" below="" is="" not="" sufficient="" for="" full="" testing)=""> a; SetT b; SetT c; b.Add(7); b.Add(9); a.Add(1); a.Add(1); a.Add(2); a.Add(3); a.Add(4); a.Add(5); a+6; // Not work! a.Union(b); a + b; //Not work! a - b; //Not work! a.Traverse(display); cout<"\n"; a.difference(b); // not work! result is not correct. b.traverse(display); return 0; } a.difference(b);="" not="" work!="" result="" is="" not="" correct.="" b.traverse(display);="" return="" 0;="">
Answered Same DayNov 22, 2021

Answer To: Introduction For this project you will implement a Set using a hash based chain implementation (each...

Sudipta answered on Nov 23 2021
134 Votes
SETT.H FILE:
#include
#include
#include
#include
using namespace std;
template
class SetT
{
public:
SetT();
SetT(int numBucks);
~SetT();
void Add(T elem);
void Remov
e(T elem);
bool Contains(T elem);
void Union(SetT otherSet);
void Difference(SetT otherSet);
void Intersection(SetT otherSet);
void Filter(bool fnc(T elem)); // Extra Credit
void Traverse(void visit(T& elem));
int Size() { return numElems };
SetT operator+(T elem); // Add
SetT operator+(SetT& other); // Union
SetT operator*(SetT& other); // Intersection
SetT operator-(T elem); // Remove
SetT operator-(SetT& other); // Difference
private:
forward_list** buckets; // An array of forward_list's (ie, each index is a forward_list pointer)
int numBuckets;
int getHashIndex(const T& elem);
int numElems;
// Any other private functions and variables you need
};
template
int SetT::getHashIndex(const T& key)
{
// This is done... No touching!
unordered_map mapper;
typename unordered_map::hasher hashFunction = mapper.hash_function();
return static_cast(hashFunction(key) % numBuckets);
}
template
SetT::SetT()
{
// Create an array of forward_lists and initially set each bucket to null
buckets = new forward_list*[100];
for (int i = 0; i < 100; i++) {
buckets[i] = nullptr;
}
numBuckets = 100;
}
template
SetT::~SetT()
{
}
template
void SetT::Add(T elem)
{
// Add elem to the set if it doesn't already contain it
// Get the bucket index and check to see if the bucket
// contains elements. If the bucket has never had elements,
// then initialize the forward_list of that bucket. Add the
// element to the bucket.
}
template
void SetT::Remove(T elem)
{
}
template
bool SetT::Contains(T elem)
{
return false;
}
template
void SetT::Union(SetT otherSet)
{
}
template
void SetT::Difference(SetT otherSet)
{
}
template
void SetT::Intersection(SetT otherSet)
{
}
template
void SetT::Filter(bool fnc(T elem))
{
}
template
SetT::SetT(int numBucks)
{
}
template
inline SetT SetT::operator+(T elem)
{
this->Add(elem);
return result;
}
template
inline SetT SetT::operator+(SetT & other)
{
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here