home *** CD-ROM | disk | FTP | other *** search
-
- #if !defined(_SourceSet_h)
- #define _SourceSet_h
-
- #pragma hdrstop
-
- #include <_null.h>
-
- template <class T>
- class typeSet;
-
- ///////////////////////////////////////////////////////////////////////////////
- // Class node
- // ======
- // Attributes-----
- // private
- // data Field to contain information kept by the node.
- // link Field to link node to other nodes of same type.
- //
- // Methods--------
- // private
- // CONSTRUCTOR: Assign values for data and link.
- // DESTRUCTOR: none.
- //
- template<class T>
- class node {
- friend class typeSet<T>;
-
- public:
- node (T item, node* nodePtr = NULL) {
- data = item;
- link = nodePtr;
- } // end node;
- ~node() {}
-
- T data; // Data to be stored in the node.
- node* link; // Link to the next node.
-
- }; // end node
-
-
- ///////////////////////////////////////////////////////////////////////////////
- // Class typeSet
- // ===========
- // Attributes-----
- // private
- // frontPtr Contains pointer to first node in the list.
- // count The number of nodes contained in the list.
- //
- // Methods--------
- // private
- // pointto Point to ith item in a list.
- //
- // public
- // CONSTRUCTOR: Assign NULL value to new set.
- // set count to zero (0).
- // DESTRUCTOR: Call function clear (deallocate all nodes
- // and set count to zero).
- // search Search the list for a node (return -1 if not found).
- // clear Delete all the nodes, set count to zero.
- // isEmpty Return true or false depending on if the list is empty.
- // isMember Return true if node is in the list.
- // isSubset Return true if set A is a subset of set B.
- // isProperSubset Return true if set A is a subset of set B.
- // extentOf Return the number of nodes in the list.
- // operator - Return the difference of two sets.
- // operator || Return the union of a two sets.
- // operator && Return the intersection of two sets.
- // operator = set one set equal to another set.
- // operator += Add an item to the set.
- // operator -= Delete an item from the set.
- // operator == Return true or false depending on sets equality.
- // operator << Display the list.
- //
- template<class T>
- class typeSet {
- private:
- node<T>* frontPtr; // Pointer to front of the set.
- unsigned int count; // Number of nodes in the set.
-
- // Point to the ith node in the set.
- node<T>* pointto(unsigned int);
-
- public:
- typeSet () {
- frontPtr = NULL;
- count = 0;
- } // end list constructor
- ~typeSet() { clear(); }
-
- bool isEmpty();
- bool isMember(T);
- int search(T);
- unsigned extentOf() { return count; }
- void clear();
- typeSet<T>& operator +=(T);
- typeSet<T>& operator -=(T);
- typeSet<T>& operator =(typeSet<T>&);
- friend bool isSubset(typeSet<T>&, typeSet<T>&);
- friend bool isProperSubset(typeSet<T>&, typeSet<T>&);
- friend bool operator ==(typeSet<T>&, typeSet<T>&);
- friend typeSet<T> operator -(typeSet<T>&, typeSet<T>&);
- friend typeSet<T> operator ||(typeSet<T>&, typeSet<T>&);
- friend typeSet<T> operator &&(typeSet<T>&, typeSet<T>&);
- friend ostream& operator <<(ostream& os, typeSet<T>&);
-
- }; // end class typeSet
-
-
- ////////////////////////////////////////////////////////////////////////////
- // typeSet operator ==
- // =====
- // Test the equality of the set.
- //
- template<class T>
- bool operator ==(typeSet<T>& s1, typeSet<T>& s2) {
- node<T> *chaser = s1.frontPtr;
-
- // Test each member of set A to see if it is in set B
- while (chaser != NULL) {
- // If a member from s1 is not in s2 the sets are not equal.
- if (!s2.isMember(chaser-> data))
- return false;
- chaser = chaser-> link;
- } // end while
-
- // The sets are equal.
- return true;
- } // end operator ==
-
-
- ////////////////////////////////////////////////////////////////////////////
- // typeSet operator <<
- // =====
- // Display the set.
- //
- template<class T>
- ostream& operator <<(ostream& os, typeSet<T>& s) {
- node<T>* chaser = s.frontPtr;
- // Output the right bracket.
- os << "{";
-
- // If the set is not empty output it's members.
- if (!s.isEmpty()) {
- os << chaser-> data;
- chaser = chaser-> link;
- while (chaser != NULL) {
- os << "," << chaser-> data;
- chaser = chaser-> link;
- } // end while
- } // end if
-
- // Output the left bracket.
- os << "}";
-
- return os;
- } // end operator <<
-
-
- ////////////////////////////////////////////////////////////////////////////
- // typeSet operator =
- // =====
- // Make this set equal to another set.
- //
- template<class T>
- typeSet<T>& typeSet<T>::operator =(typeSet<T>& s) {
- node<T> *chaser = s.frontPtr;
-
- clear();
- while (chaser!= NULL) {
- // Insert the new member from source.
- *this += chaser-> data;
- // Get the next member.
- chaser = chaser->link;
- } // end while
-
- return *this;
- } // end operator =
-
-
- ////////////////////////////////////////////////////////////////////////////
- // typeSet operator =-
- // =====
- // Remove a set member.
- //
- template<class T>
- typeSet<T>& typeSet<T>::operator -=(T item) {
- node<T> *chaser = frontPtr, *prevLink = NULL;
-
- // Search for the item.
- while (chaser!=NULL) {
- // Check equality of item vs node data.
- if (chaser->data == item) {
- // Item found, now delete it.
-
- // Remove the front of the list.
- if (prevLink == NULL)
- frontPtr = chaser-> link;
-
- // Remove from the back of the list.
- else if (chaser-> link == NULL)
- prevLink-> link = NULL;
-
- // Remove from middle of the list.
- else
- prevLink-> link = chaser-> link;
-
- // Delete the node.
- delete chaser;
-
- // Update the elememt counter.
- count--;
-
- return *this;
- } // end if
- prevLink=chaser; // Remember the previous node.
- chaser = chaser-> link;
- } // end while
-
- return *this;
-
- } // end operator =-
-
-
- ////////////////////////////////////////////////////////////////////////////
- // typeSet clear
- // =====
- // Delete all members in the set.
- //
- template<class T>
- void typeSet<T>::clear() {
- node<T>* ptr;
-
- // If set is empty set count to zero and exit.
- if (frontPtr == NULL) {
- count = 0;
- return;
- } // end if
-
- // Delete the front pointer.
- ptr = frontPtr;
- frontPtr = ptr-> link;
- delete ptr;
-
- // Recurse until all nodes are deleted.
- clear();
- } // end clear
-
-
- ////////////////////////////////////////////////////////////////////////////
- // typeSet isEmpty
- // =====
- // Return true or false depending if set is empty.
- //
- template<class T>
- bool typeSet<T>:: isEmpty() {
-
- // If the front pointer is null the set is empty.
- if (frontPtr == NULL) return true;
- return false;
- } // end isEmpty
-
-
- ////////////////////////////////////////////////////////////////////////////
- // typeSet isMember
- // =====
- // Return true or false depending if set is empty.
- //
- template<class T>
- bool typeSet<T>::isMember(T item) {
- if (search(item)==-1)
- return false;
- else
- return true;
- } // end isMember
-
-
- ////////////////////////////////////////////////////////////////////////////
- // typeSet operator ||
- // =====
- // Perform the union of two sets.
- //
- template<class T>
- typeSet<T> operator ||(typeSet<T>& s1, typeSet<T>& s2) {
- node<T> *chaser = s2.frontPtr;
- while (chaser != NULL) {
- s1 += chaser-> data;
- chaser = chaser-> link;
- } // end while
-
- return s1;
- } // end operator ||
-
-
- ////////////////////////////////////////////////////////////////////////////
- // typeSet operator &&
- // =====
- // Perform the intersection of two sets.
- //
- template<class T>
- typeSet<T> operator &&(typeSet<T>& s1, typeSet<T>& s2) {
- typeSet<T> temp = s1;
- s1.clear();
-
- node<T> *chaser = s2.frontPtr;
-
- while (chaser != NULL) {
- if (temp.isMember(chaser-> data))
- s1 += chaser-> data;
- chaser = chaser-> link;
- } // end while
-
- return s1;
- } // end operator &&
-
-
- ////////////////////////////////////////////////////////////////////////////
- // typeSet isSubset
- // =====
- // Return true if a given set is a subset of this set.
- //
- template<class T>
- bool isSubset(typeSet<T>& s1, typeSet<T>& s2) {
- node<T> *chaser = s2.frontPtr;
-
- if (s2.isEmpty()) return false;
-
- while (chaser != NULL) {
- if(!s1.isMember(chaser-> data))
- return false;
- chaser = chaser-> link;
- } // end while
-
- return true;
- } // end isSubset
-
-
- ////////////////////////////////////////////////////////////////////////////
- // typeSet isProperSubset
- // =====
- // Return true if a given set is a proper subset of this set.
- //
- template<class T>
- bool isProperSubset(typeSet<T>& s1, typeSet<T>& s2) {
- // Check if the two sets are equal
- if (!(s1 == s2))
- return isSubset(s1, s2);
- else
- return false;
- } // end isSubset
-
-
- ////////////////////////////////////////////////////////////////////////////
- // typeSet operator -
- // =====
- // Perform the difference of two sets.
- //
- template<class T>
- typeSet<T> operator -(typeSet<T>& s1, typeSet<T>& s2) {
- node<T> *chaser = s2.frontPtr;
-
- while (chaser != NULL) {
- if (s1.isMember(chaser-> data))
- s1 -= chaser-> data;
- chaser = chaser-> link;
- } // end while
-
- return s1;
- } // end operator &&
-
-
- ////////////////////////////////////////////////////////////////////////////
- // typeSet search
- // =====
- // Search for and return the index of a member. Return -1 if not found.
- //
- template<class T>
- int typeSet<T>::search(T item) {
- node<T> *chaser = frontPtr;
-
- // Search through the set until a item is found.
- for (int p = 0; p < count; p++) {
- // Check equality of item vs node data.
- if (chaser-> data == item) return p;
- chaser = chaser-> link;
- } // end for
-
- // Item not found return -1.
- return -1;
-
- } // end search
-
-
- ////////////////////////////////////////////////////////////////////////////
- // typeSet operator +=
- // =====
- // Add a member to the set.
- //
- template<class T>
- typeSet<T>& typeSet<T>::operator +=(T item) {
- node<T> *tempPtr;
-
- if (!isMember(item)) {
- // Create a new node with link being assigned frontPtr.
- tempPtr = new node<T>(item, frontPtr);
-
- // Assign front pointer to point to the new node.
- frontPtr = tempPtr;
-
- // Increment the node counter.
- count++;
- } // end if
-
- return *this;
- } // end operator +=
-
- #endif
-
-
-
- Copyright 1998,Jeremy Petter. All rights reserved worldwide. The Source shall not be liable in the event of incidental or consequential
- damages arising from the use of information supplied herein.
-