Bag

From Minor Miracle Software
Jump to: navigation, search

Bag

// Jesse B. Dooley
// 26 May 1995

// template bag class
// The following overloaded operators are
// assumed overloads or conversions for the S BagData type.
// <  LESS THAN
// >  greater than
// == equals
// << OUTPUT STREAM
// >> input stream
// =  assignment

// bag.cc

#ifndef LIST_CC
  #include "list.cc"
#endif


#ifndef BAG_H
#define BAG_H

// prototype class

template <CLASS B> class bag;
// prototype input function

template <CLASS B> istream& operator >> ( istream&, bag<B>& );
// prototype output function

template <CLASS B> ostream& operator << ( OSTREAM& , BAG<B>& );

template<CLASS B> class bag {

	list<B> BagData;

friend istream& operator >> ( istream&, bag<B>& );
// Read from input stream. Place nodes into list in accending ASCII order

friend ostream& operator << ( OSTREAM&, BAG<B>& );
// Sends all nodes in list to output stream in acending ASCII order


public:

bag()
{
}


~bag()
{
}



bag( const bag<B> &P )
{
	BagData = P.BagData;
}


int operator += ( bag<B> & );
// bag1 += bag2, with duplicates in list2 added to list1
// returns TRUE if sucessful, FALSE if any node in list2 is added to list1.

int operator += ( B );
// add <B> to *this list keeping duplicates

int operator -= ( bag<B> & );
// bag1 -= bag2, where matches between bag1 and bag2 are deleted from bag1
// returns TRUE if sucessful, FALSE if failure.


int operator -= ( B );
// bag1 -= BagData, where BagData, if found, is deleted from bag1
// returns TRUE if sucessful, FALSE if failure.

bag<B> operator = ( const bag<B> &);
// overload with copy constructor

unsigned long census();
// return BagData.census();

int isempty();
// return Bagdata.isempty();

int locate( B );
// return how many Bs are in the bag

};



template<CLASS B> int bag<B>::operator += ( bag<B> &d)
{
	return BagData += d.BagData;
}



template<CLASS B> int bag<B>::operator += ( B d)
{
    return BagData.AddToList( d );
}



template<CLASS B> int bag<B>::operator -= ( bag<B> &D )
{
    return BagData -= D.BagData;
}



template<CLASS B> int bag<B>::operator -= ( B d )
{
    return BagData -= d;
}


template<CLASS B> ostream& operator << (OSTREAM& OS, BAG<B> &A)
{
	OS << A.BAGDATA;
	RETURN OS;
}


TEMPLATE<CLASS B> istream& operator >> (istream& IS, bag<B> &A)
{
    IS >> A.BagData;
    return IS;
}

template<CLASS B> bag<B> operator=(const bag<B> &P )
{
    BagData = P.BagData;
    return BagData;
}

template<CLASS B> unsigned long bag<B>::census()
{
    return BagData.census();
}

template<CLASS B> int bag<B>::isempty()
{
    return BagData.isempty();
}

template<CLASS B> int bag<B>::locate( B  K )
{
    return BagData.locatenum( K );
}
#endif

Internal Links

Parent Article: Main Page