// ----------------------------------------------------------------------------------------
//
//  misc.h
//
//  Bob Cook Development, Robotics Library
//  http://www.bobcookdev.com/rl
//    
//  This file provides miscellaneous useful, generic functions and macros.
//
//  Copyright (C) 2007 Bob Cook
//
//      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., 
//      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
// ----------------------------------------------------------------------------------------


#if !defined( BCDRL_COMMON_UTIL_MISC_H )
#define BCDRL_COMMON_UTIL_MISC_H


#include <stdint.h>


// ----------------------------------------------------------------------------------------
//  array_sizeof
//
//      Return the number of elements in a static array variable.
//

#define array_sizeof( a )   ( sizeof( (a) ) / sizeof( (a)[0] ) )


// ----------------------------------------------------------------------------------------
//  util_max
//
//      Return the larger of the two given values.
//

template< typename TYPE >
inline const TYPE util_max( const TYPE a, const TYPE b )
{
    return ( a > b ? a : b );
}

// ----------------------------------------------------------------------------------------
//  util_min
//
//      Return the lesser of the two given values.
//

template< typename TYPE >
inline const TYPE util_min( const TYPE a, const TYPE b )
{
    return ( a < b ? a : b );
}

// ----------------------------------------------------------------------------------------
//  util_clamp
//
//      Return the given value if its between the two boundries, or the lower boundry
//      if the value is less, or the larger boundry if its greater.
//

template< typename TYPE >
inline const TYPE util_clamp( const TYPE a, const TYPE val, const TYPE b )
{
    return util_min( util_max( val, a ), b );
}


// ----------------------------------------------------------------------------------------
//  util_abs
//
//      Return the absolute value of the given value.
//

template< typename TYPE >
inline const TYPE util_abs( const TYPE a )
{
    return ( a < 0 ? -a : a );
}


#endif  // #if !defined( BCDRL_COMMON_UTIL_MISC_H )
// ----------------------------------------------------------------------------------------
//  end of misc.h
// ----------------------------------------------------------------------------------------

