Requirements
The assignment is to create a dynamic array implementation of a set (defined in set.h). Add the efficiency of each function to the documentation in the herder file. Use the test_set.cpp as your test program.
_______________________________________________________________________________________________________________________________________________________
set.h file
#ifndef _SET_H_
#define _SET_H_
#include <cstdlib>
#include <iostream>
class set
{
public:
typedef int value_type;
typedef std::size_t size_type;
set(size_type initial_capacity);
// postcondition: empty set with initial_capacity has been
created
~set();
// postcondition: all dynamically allocated memory has been
deallocated
set(const set& s);
// copy of s has been created;
set& operator= (const set& s);
// postcondition: exact copy of s has been assigned to the current
set object
bool erase(const value_type& target);
// postcondition: returned true if target was removed from set ow
false if target was not in the set
bool insert(const value_type& entry);
// postcondition: if entry was not in the set, then entry has been
added - ow nothing was done
void operator+=(const set& addend);
// postcondition: non-duplicating elements from addend have been
added to the set
size_type size() const;
// postcondition: number of elements in the set has been
returned
bool contains(const value_type& target) const;
// postcondition: returned whether target is in the set
friend std::ostream& operator<<(std::ostream& output,
const set& s);
private:
void reserve (size_type new_capacity);
// precondition: size() <= new_capacity
// postcondition: capacity is new_capacity
value_type* data; // The array to store items
size_type used; // How much of array is used
size_type capacity;
};
#endif // _SET_H_
_______________________________________________________________________________________________________________________________________________________
test_set.cpp
#include <iostream>
#include "set.h"
int main(int argc, char **argv)
{
set s(5);
s.insert (7);
std::cout << s << std::endl;
s.insert (8);
std::cout << s << std::endl;
s.insert(3);
std::cout << s << std::endl;
s.insert (2);
s.insert (1);
s.insert (22);
std::cout << s << std::endl;
set s1(s);
std::cout << s1 << std::endl;
set s2 (78);
s2 = s;
std::cout << s2 << std::endl;
}
Code
set.h
#ifndef _SET_H_
#define _SET_H_
#include <cstdlib>
#include <iostream>
class set
{
public:
typedef int value_type;
typedef std::size_t size_type;
set(size_type initial_capacity);
// postcondition: empty set with initial_capacity has
been created
~set();
// postcondition: all dynamically allocated memory has
been deallocated
set(const set& s);
// copy of s has been created;
set& operator= (const set& s);
// postcondition: exact copy of s has been assigned to
the current set object
bool erase(const value_type& target);
// postcondition: returned true if target was removed
from set ow false if target was not in the set
bool insert(const value_type& entry);
// postcondition: if entry was not in the set, then
entry has been added - ow nothing was done
void operator+=(const set& addend);
// postcondition: non-duplicating elements from addend
have been added to the set
size_type size() const;
// postcondition: number of elements in the set has
been returned
bool contains(const value_type& target)
const;
// postcondition: returned whether target is in the
set
friend std::ostream&
operator<<(std::ostream& output, const set& s);
private:
void reserve (size_type new_capacity);
// precondition: size() <= new_capacity
// postcondition: capacity is new_capacity
value_type* data; // The array to store items
size_type used; // How much of array is used
size_type capacity;
};
#endif // _SET_H_
set.cpp
#include"set.h"
set::set(size_type initial_capacity)
{
data= new value_type[initial_capacity];
used=0;
capacity=initial_capacity;
}
set::set(const set& s)
{
this->capacity=s.capacity;
this->used=s.used;
this->data=s.data;
}
bool set::erase(const value_type& target)
{
int count=0;
for(int i=0; i<used; i++)
{
if(data[i]==target)
{
for(int j=i;
j<(used-1); j++)
{
data[j]=data[j+1];
}
count++;
return
true;
}
}
return false;
}
bool set::insert(const value_type & entry)
{
if(used>=capacity)
{
size_type
newSize=capacity*1.5;
reserve(newSize);
capacity=newSize;
}
if(!contains(entry))
{
data[used]=entry;
used++;
return true;
}
return false;
}
set & set::operator = (const set &s)
{
// Check for self assignment
if(this != &s)
{
this->used=s.used;
this->data=s.data;
}
return *this;
}
bool set::contains(const value_type& target) const
{
for(int i=0;i<used;i++)
if(data[i]==target)
return
true;
return false;
}
void set::operator+=(const set& addend)
{
for(int i=0;i<addend.used;i++)
{
if(!contains(addend.data[i]))
{
if(used>=capacity)
{
size_type newSize=capacity*1.5;
reserve(newSize);
capacity=newSize;
}
data[used]=addend.data[i];
used++;
}
}
}
set::size_type set::size() const
{
return used;
}
std::ostream& operator<<(std::ostream& output, const
set& s)
{
for(int i=0;i<s.used-1;i++)
output<<s.data[i]<<",";
output<<s.data[s.used-1];
return output;
}
void set::reserve (size_type new_capacity)
{
if(capacity<=new_capacity)
{
value_type* temp = new
value_type[new_capacity];
for(int i=0;i<used;i++)
{
temp[i]=data[i];
}
delete [] data;
data = temp;
}
}
set::~set()
{
delete data;
}
test_set.cpp
#include <iostream>
#include "set.h"
int main(int argc, char **argv)
{
set s(5);
s.insert (7);
std::cout << s << std::endl;
s.insert (8);
std::cout << s << std::endl;
s.insert(3);
std::cout << s << std::endl;
s.insert (2);
s.insert (1);
s.insert (22);
std::cout << s << std::endl;
set s1(s);
std::cout << s1 << std::endl;
set s2 (78);
s2 = s;
std::cout << s2 << std::endl;
}
output
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.
Get Answers For Free
Most questions answered within 1 hours.