rendered paste body/*The key class keeps track of comparisons of an algorithm
by overloading operators, and incrementing a counter.
Templated so, it can take any standard datatype.
*/
template <class T>
class Key {
public:
static double comparisons;
Key<T> (T x=0);
T the_key() const;
friend bool operator == <T> (const Key<T> &x,const Key<T> &y);
friend bool operator > <T> (const Key<T> &x,const Key<T> &y);
friend bool operator < <T> (const Key<T> &x,const Key<T> &y);
friend bool operator >= <T> (const Key<T> &x,const Key<T> &y);
friend bool operator <= <T> (const Key<T> &x,const Key<T> &y);
friend bool operator != <T> (const Key<T> &x,const Key<T> &y);
T operator = <T> (const Key<T> &x,const Key<T> &y);
//To be friend or not to be friend.
//friend ostream& operator << (ostream& outs, const Key &x);
//Precondition: if outs is a file output stream,
//it has already been connected to a file
//Overloads << operator to output values of type Key
private:
T key; //Key is a templated type
};
/* hmm doesn't work
ERROR:
c:\Stuff\School\Visual Studio Projects\Prog6\key.h(20) : error C2143: syntax error : missing ';' before '<'
c:\Stuff\School\Visual Studio Projects\Prog6\key.h(30) : see reference to class template instantiation 'Key<T>' being compiled
c:\Stuff\School\Visual Studio Projects\Prog6\key.h(20) : error C2238: unexpected token(s) preceding ';'
c:\Stuff\School\Visual Studio Projects\Prog6\key.h(39) : error C2801: 'operator =' must be a non-static member
*/
template <class T>
T operator = (const Key<T> &x, const Key<T> &y)
{
Key<T>::comparisons++;
return y.the_key();
}
template <class T>
double Key<T>::comparisons=0;//This seems like a weird place for this.
template <class T>
ostream& operator << (ostream& outs, const Key<T> &x)
{
return outs << x.the_key();
}
template <class T>
Key<T>::Key(T x)
{
key = x;
}
template <class T>
T Key<T>::the_key() const
{
return key;
}
template <class T>
bool operator == (const Key<T> &x, const Key<T> &y)
{
Key<T>::comparisons++;
return x.the_key() == y.the_key();
}
template <class T>
bool operator > (const Key<T> &x, const Key<T> &y)
{
Key<T>::comparisons++;
if (debug > 0) cout << ">";
return x.the_key() > y.the_key();
}
template <class T>
bool operator < (const Key<T> &x, const Key<T> &y)
{
Key<T>::comparisons++;
if (debug > 0) cout << "<";
return x.the_key() < y.the_key();
}
template <class T>
bool operator >= (const Key<T> &x, const Key<T> &y)
{
Key<T>::comparisons++;
return x.the_key() >= y.the_key();
}
template <class T>
bool operator <= (const Key<T> &x, const Key<T> &y)
{
Key<T>::comparisons++;
return x.the_key() <= y.the_key();
}
template <class T>
bool operator != (const Key<T> &x, const Key<T> &y)
{
Key<T>::comparisons++;
return x.the_key() != y.the_key();
}