/**------------------------------------------------------------------------
 * @file
 *
 * Copyright 2001 by the University of Toronto (UT).
 *
 *                       All Rights Reserved
 *
 * Permission to copy and modify this software and its documentation
 * only for internal use in your organization is hereby granted,
 * provided that this notice is retained thereon and on all copies.
 * UT makes no representations as to the suitability and operability
 * of this software for any purpose.  It is provided "as is" without
 * express or implied warranty.
 *
 * UT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT
 * SHALL UT BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 * No other rights, including for example, the right to redistribute
 * this software and its documentation or the right to prepare
 * derivative works, are granted unless specifically provided in a
 * separate license agreement.
 *
 * Author: Maxim Trokhimtchouk (trokhim@cs.utoronto.ca)
 *
 * $Log: matchingdatabase.hxx,v $
 * Revision 1.1.1.1  2002/03/10 07:38:07  dmac
 * Imported sources
 *
 * Revision 1.1  2001/10/03 20:19:57  dmac
 * Added file
 *
 *
 *-----------------------------------------------------------------------*/

template<class baseclass>
DAGTypeDatabase<baseclass>::DAGTypeDatabase() : baseclass()
{
	views_seen = 0;
}

template<class baseclass>
DAGTypeDatabase<baseclass>::~DAGTypeDatabase()
{
}

template<class baseclass>
ID DAGTypeDatabase<baseclass>::Find(string& name)
{
	typename map<string, ID>::iterator i;

	i = namemap.find(name);
	
	if(i == namemap.end())
	{
		return NULLID;
	}
	else
	{
		return i->second;
	}
}

template<class baseclass>
ID DAGTypeDatabase<baseclass>::Add(const DAGType& record)
{
	ID id = baseclass::Add(record);

	namemap.insert(pair<string,ID>(record.name, id));

	return id;
}

template<class baseclass>
void DAGTypeDatabase<baseclass>::Remove(ID id)
{
	pair<DAGType, bool> item;
	typename map<string, ID>::iterator i;

	if(Contained(id))
	{
		i = namemap.find(records[id]->name);	
		namemap.erase(i);

		baseclass::Remove(id);
	}
}

template<class baseclass>
void DAGTypeDatabase<baseclass>::RemoveAll()
{
	baseclass::RemoveAll();
	namemap.clear();
}

template<class baseclass>
ostream& DAGTypeDatabase<baseclass>::Write(ostream& os)
{
	typename map<string,ID>::iterator i;

	baseclass::Write(os);

	os << views_seen;	

	for(i = namemap.begin();i != namemap.end();i++)
	{
		os << i->first;
		os << i->second;
	}

	os << string("");
	os << NULLID;
}

template<class baseclass>
istream& DAGTypeDatabase<baseclass>::Read(istream& is)
{
	typename map<string,ID>::iterator i;
	pair<string, ID> p;

	baseclass::Read(is);

	is >> views_seen;
	is >> p.first;
	is >> p.second;

	while(p.second != NULLID)
	{
		namemap.insert(p);

		is >> p.first;
		is >> p.second;
	}
}
