// © Copyright 1995. Joseph Bergin. All rights reserved.

#ifndef __Association__
#define __Association__

#include <iostream.h>
#include "Dictionary.h"

//#pragma template_access private

// Implements ordered pairs (key,value) with a special ordering / equality 
//	meaning.  Two associations are == if their keys are ==. Two associations are
//	< if their keys are <.  
//	Dictionaries (which see) are sets of associations.  As such, they implement
//	finite functions.  
// Require type K (key) supports == and < and an ostream << operation. 
//	Pointer types are not appropriate for keys because of the
//	inherent meaning of < 
//	Any type may be used for values. To maximize flexibility in this type, when
//	an association is printed, its "value" field is not printed.  
//	Although there is a default constructor, it returns an uninitialized
//	association.  It is hazardous to use such an association. It should be
//	immediately assigned a value from another properly initialized association. 

template <class K, class V>
class Association{
	public:
		Association(const K& key, const V& value)
		:	_key(key), 
			_value(value)
		{
		}

		V value() const{ return _value; };

		K key() const{return _key;};
		
/*		
		Association(const Association &a)
		:	_key(a._key),
			_value(a._value)
		{
		}

		~Association()
		{	//nothing
		}
		
		Association & operator = (const Association &a)
		{	_key = a._key;
			_value = a._value;
			return *this;
		}
*/		
		int operator <  (const Association &a) const
		{	return _key < a._key;
		}
		
		int operator == (const Association &a)
		{	return _key == a._key;
		}
		
//		ostream& outOnStream(ostream& out)
//		{	out << " < " << _key << " , " <<  " a.value " << " > ";
//	 		return out;
//		}
		
		Association(){} // DANGER: returns an UNINITIALIZED association;
	private:
		K _key;
		V _value;
	friend class Dictionary<K,V >;
//	friend 
//	ostream& operator <<(ostream& out, Association<K,V>& a);
};

// The following requires an output << for key and valaue.
	template <class K, class V>
	ostream& operator <<(ostream& out, Association<K,V>& a)
	{	out << " < " << a.key() << " , " <<  a.value() << " > ";
		return out;
	}
		
#endif


