/****************************************************************************************

		InlineOps.h

		Copyright: (c) 2001 Eovia Corp. All rights reserved.

		Revision History

		when		who			what & why
		==========	==========	======================================================
		1/19/97		link		Removed if-def around pragma once.
		2/17/97		Alec		added macroIsOdd/Even
		12/27/97	Alec		added macroLogicalXor
		12/18/96	Alec		new today - repository for general macros/inlines
									
*****************************************************************************************/

#ifndef	H_INLINEOPS
#define	H_INLINEOPS

#pragma once


// This is a collection of general macros and inlines pertinant to a wide variety
// of applications.  Added calls should be small.  Please denote const macro
// parameters with 'k'.


// clamp a value between a min and max
#define macroClampValue( value, kMin, kMax )	\
		{										\
			if ( (value) < (kMin) )				\
				(value) = (kMin);				\
			else if ( (value) > (kMax) )		\
				(value) = (kMax);				\
		}

// clamp a value between a min and max
#define macroClampNewValue( newValue, kValue, kMin, kMax )	\
		{													\
			if ( (kValue) < (kMin) )						\
				(newValue) = (kMin);						\
			else if ( (kValue) > (kMax) )					\
				(newValue) = (kMax);						\
			else											\
				(newValue) = (kValue);						\
		}
		
// update the min/max given unknown min/max values (possibly two value changes)
#define macroUpdateMinMax( kValue, min, max )	\
		{										\
			if ( (min) > (kValue) )				\
				 (min) = (kValue);				\
			if ( (max) < (kValue) )				\
				 (max) = (kValue);				\
		}
		
// update the min/max values if value exceeds them (only one value change)
#define macroUpdateValidMinMax( kValue, min, max ) 	\
		{											\
			if ( (max) < (kValue) )					\
				(max) = (kValue);					\
			else if ( (min) > (kValue) )			\
				(min) = (kValue);					\
		}
		
// return the min/max between the two values		
#define macroMin( kVal0, kVal1 ) \
		( ((kVal0) < (kVal1)) ? kVal0 : kVal1 )	
#define macroMax( kVal0, kVal1 ) \
		( ((kVal0) > (kVal1)) ? kVal0 : kVal1 )

// swap two values given a similar value for swap space
#define macroSwap( val0, val1, swapSpace ) \
		{ (swapSpace) = (val0), (val0) = (val1), (val1) = (swapSpace); }

// swap a and b (see Gem by Wyvill), only works for integer data
#define macroSwapInt( val0, val1 )	\
		{ (val0) ^= (val1); (val1) ^= (val0); (val0) ^= (val1); }

// a logical exclusive or (returns 1 if only one of the args is true)
#define macroLogicalXor( kVal0, kVal1 ) \
		(( kVal0 ) ? !(kVal1) : (kVal1)) 

// check whether an integer value is odd or even
#define macroIsOdd( kVal ) 	\
		(((kVal) % 2) == 1 )
#define macroIsEven( kVal ) \
		(((kVal) % 2) == 0 )		

// check wether a value is within tolerance to another
#define macroIsClose( kVal0, kVal1, kTolerance ) \
		((((kVal0) - (kVal1))>0)?((kVal0) - (kVal1)) <= (kTolerance) :((kVal1) - (kVal0)) <= (kTolerance))

#endif // H_INLINEOPS