/*
 * 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_ext_hh
#define _uds_stl_ext_hh


namespace uds
{

/** deletes an object
	\param obj object to be deleted */
template < class C > inline
void
Delete( C* obj )
{
	delete obj;
}

/** deletes an object
	\param obj object to be deleted
	\return zero */
template < class C > inline
C*
NullDelete( C* obj )
{
	delete obj;
	return 0;
}


/** deletes an array
	\param obj array to be deleted */
template < class C > inline
void
VecDelete( C* a )
{
	delete [] a;
}

/** deletes an array
	\param obj array to be deleted
	\return zero */
template < class C > inline
C*
VecNullDelete( C* a )
{
	delete [] a;
	return 0;
}


/** does nothing */
template < class C > inline
void
NullOp( C ) {}

}


namespace std
{

/** apply a function to every element of a range if a condition is fulfilled */
template < class In, class Op, class Pred >
Op
for_each_if( In beg, In end, Op op, Pred p )
{
	for ( ; beg != end; ++beg ) if ( p( *beg ) ) op( *beg );
	return op;
}

/** apply a function to every element of a range if a condition is fulfilled.
	Otherwise call another function */
template < class In, class Op_true, class Op_false, class Pred >
void
for_each_if_else( In beg, In end, Op_true op_true, Op_false op_false, Pred p )
{
	for ( ; beg != end; ++beg )
	{
		if ( p( *beg ) ) op_true( *beg );
		else op_false( *beg );
	}	
	return op;
}

/** copy_if algorithm that was removed from the standard library */
template < class In, class Out, class Pred >
Out
copy_if( In beg, In end, Out res, Pred p )
{
	for ( ; beg != end; ++beg ) if ( p( *beg ) ) *res++ = *beg;
	return res;
}

}

#endif _uds_stl_ext_hh
