/*****************************************************************************
 * $Id: eng_debug.hh,v 1.21 2006/11/02 02:38:02 devel Exp $
 * @file
 * @author	Jim E. Brooks  http://www.jimbrooks.org
 * @brief	Debug/profiling support.
 *//*
 * LEGAL:	COPYRIGHT (C) 2004 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE (GPL).
 *****************************************************************************/

#if DEBUG
#ifndef ENG_DEBUG_HH
#define ENG_DEBUG_HH 1

void
EngineAssertHandler( bool cond, const char* fileName, int fileLine );

/*****************************************************************************
 * @return Fraction that indicates how much an STL container is sorted.
 * Returns 1.0 if entirely sorted.
 *****************************************************************************/
template<typename CONTAINER>
fp
MeasureSortedContainer( const CONTAINER& con )
{
    if ( con.size() <= 1 )
        return 1.0;

    uint sorted = 0;  // counts each pair that sorted WRT each other
    uint comparisons = 0;
    typename CONTAINER::const_iterator itr = con.begin();
    for (;;++itr)
    {
        typename CONTAINER::const_iterator itr2 = itr;
        ++itr2;
        if ( itr != con.end() && itr2 != con.end() )
        {
            ++comparisons;
            sorted += ! ((*itr2) < (*itr));  // itr2 >= itr
        }
        else
        {
            break;
        }
    }
    return fp(sorted) / fp(comparisons);
}

#endif // ENG_DEBUG_HH
#endif // DEBUG
