Please send questions to
st10@humboldt.edu .
/************************************************************/
/* ADAPTED FROM: */
/* http://www.cs.colorado.edu/~main/chapter4/bag2.h */
/* */
/* Main and Savitch, "Data Structures and Other Objects */
/* using C++", 2nd edition, Addison-Wesley, Ch.4, p. 172 */
/************************************************************/
//-----------------------------------------------------------
// File: set.h
// Name: Michael Main, Walter Savitch
// (adapted by Sharon M. Tuttle)
// last modified: 3-10-04
//
// Class: set (a container class for a collection of items
// with NO duplicates)
//
// NONMEMBER FUNCTIONS for the set class:
//
// postcondition: the set returned is the union of
// s1 and s2.
// set operator +(const set& s1, const set& s2)
//
// VALUE SEMANTICS for the set class:
// Assignments and the copy constructor may be used
// with set objects.
//
// DYNAMIC MEMORY USAGE by the set:
// If there is insufficient dynamic memory, then the
// following functions throw bad_alloc:
// The constructors, reserve, insert, operator +=,
// operator +, and the assignment operator.
//-----------------------------------------------------------
#ifndef SET_H
#define SET_H
#include <cstdlib> // Provides size_t
using namespace std;
class set
{
public:
/****************************************************/
/* TYPEDEFS and MEMBER CONSTANTS for the set class */
/****************************************************/
// set::value_type is the data type of the items in the
// set. It may be any of the C++ built-in types
// (int, char, etc.), or a class with a default
// constructor, an assignment operator, and
// operators to test for equality (x == y) and
// non-equality (x != y).
//
// (CHANGE to desired type...)
//
typedef int value_type;
// set::size_type is the data type of any variable that
// keeps track of how many items are left in a set.
//
typedef size_t size_type;
// set::DEFAULT_CAPACITY is the initial capacity of a
// set that is created by the default constructor.
//
static const size_type DEFAULT_CAPACITY = 30;
/****************************************************/
/* CONSTRUCTORS and DESTRUCTOR */
/****************************************************/
// assumption regarding set capacity:
// The insert member function will work efficiently
// (without allocating new memory) until this
// capacity is reached.
// postcondition: creates an empty set instance with an
// initial capacity of DEFAULT_CAPACITY
//
set();
// postcondition: creates an empty set instance with an
// initial capacity of initial_capacity
//
set(size_type initial_capacity);
// copy constructor
//
set(const set& source);
// destructor
//
~set( );
/*************************************************************/
/* ACCESSORS and other constant member functions (observers) */
/*************************************************************/
// postcondition: returns the number of items in the set.
//
size_type size( ) const
{
return used;
}
// postcondition: returns true if the target is in the set,
// and returns false if it is not.
//
bool contains(const value_type& target) const;
// postcondition: returns the current capacity of this
// set
//
size_type curr_capacity() const
{
return capacity;
}
/****************************************************/
/* MODIFIERS and other modifying member functions */
/****************************************************/
// postcondition: if entry is not already in the set,
// then add it to the set; otherwise, set is unchanged.
//
void insert(const value_type& entry);
// postcondition: if target was in the set, remove it and
// return true; otherwise, return false (and the set is
// unchanged)
//
bool remove(const value_type& target);
// postconditions:
// * if new_capacity < used, set's capacity is
// changed to used (will not make the capacity
// less than the number of items already in
// the set).
// * otherwise, the set's current capacity is
// changed to new_capacity.
//
void reserve(size_type new_capacity);
/****************************************************/
/* OVERLOADED OPERATORS */
/****************************************************/
// postcondition: for each item in addend:
// * if that item is not already in this set, it is
// added to this set.
// * if that item is already in this set, then
// this set is unchanged.
//
void operator +=(const set& addend);
// postcondition: the set that activated this function
// has the same items and capacity as source.
//
void operator =(const set& source);
private:
value_type *data; // Pointer to partially filled dynamic array
size_type used; // How much of array is being used
size_type capacity; // Current capacity of the set
};
/*************************************************/
/* NONMEMBER FUNCTIONS for the set class */
/*************************************************/
set operator +(const set& s1, const set& s2);
#endif