/*
 * The UDS Collection
 * Copyright (C) 2000, 2001  Julian v. Bock
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *	(See file COPYING for more information)
 */

#ifndef _uds_stl_util_hh
#define _uds_stl_util_hh


// includes

#include <uds/stdexcpt.hh>
#include <pair.h>

using namespace std;


namespace uds
{

/** map access. if the entry does not exist, an exception is thrown
	\param map map
	\param key key
	\return value
	\author Julian v. Bock */
template < class _exception, class _map >
const typename _map::data_type&
MapFindC( const _map& map, const typename _map::key_type& key )
{
	typename _map::const_iterator i = map.find( key );
	if ( i != map.end() ) return i->second;
	throw _exception( "key not found in map\n" );
}

/** map access. if the entry does not exist, an exception is thrown
	\param map map
	\param key key
	\return value
	\author Julian v. Bock */
template < class _exception, class _map >
typename _map::data_type&
MapFind( _map& map, const typename _map::key_type& key )
{
	typename _map::iterator i = map.find( key );
	if ( i != map.end() ) return i->second;
	throw _exception( "key not found in map\n" );
}

/** queries for a map entry.
	\param map map
	\param key key
	\return address of the value or zero if the entry does not exist
	\author Julian v. Bock */
template < class _map >
const typename _map::data_type*
MapQueryC( const _map& map, const typename _map::key_type& key )
{
	typename _map::const_iterator i = map.find( key );
	return ( i == map.end() ) ? 0 : &i->second;
}

/** queries for a map entry.
	\param map map
	\param key key
	\return address of the value or zero if the entry does not exist
	\author Julian v. Bock */
template < class _map >
typename _map::data_type*
MapQuery( _map& map, const typename _map::key_type& key )
{
	typename _map::iterator i = map.find( key );
	return ( i == map.end() ) ? 0 : &i->second;
}

template < class Tp > inline
Tp
SafeDeref( Tp* x ) { return x ? *x : 0; }

/** inserts a key / value pair in a map. If the entry does already exist,
	an exception is thrown
	\param map map
	\param key key
	\param value value
	\author Julian v. Bock */
template < class _exception, class _map >
typename _map::iterator
MapInsert( _map& map, const typename _map::key_type& key,
	const typename _map::data_type& value )
{
	pair< typename _map::iterator, bool > res
		= map.insert( make_pair( key, value ) );
	if ( !res.second )
		throw _exception( "map entry does already exist\n" );
	
	return res.first;
}


/** creates and assigns an intersection between two sets
	\param s1 first set. It contains the result of the operation
	\param s2 second set */
template < class _set >
void
SetIntersection( _set& s1, const _set& s2 )
{
	// could be optimized
	for ( typename _set::iterator i = s1.begin(); i != s1.end(); ++i )
		if ( s2.find( *i ) == s2.end() ) s1.erase( i );
}


/** reads a value from an input stream
	\param s input stream
	\return value */
template < class Tp, class _stream >
Tp
StreamVar( _stream& s )
{
	Tp v;
	s>>v;
	return v;
}

}

#endif // _uds_stl_util_hh
