/*==========================================================================*/
/* Copyright (c) 1999 - Sykes Enterprises Inc. - All rights reserved        */
/*--------------------------------------------------------------------------*/
/* Sykes grants you ("Licensee") a non-exclusive, royalty free, license to  */
/* use, modify and redistribute this software in source and binary code     */
/* form, provided that i) this copyright notice and license appear on all   */
/* copies of the software; and ii) Licensee does not utilize the software   */
/* in a manner which is disparaging to Sykes.                               */
/*                                                                          */
/* This software is provided "AS IS", without a warranty of any kind. ALL   */
/* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING */
/* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR        */
/* PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SYKES AND ITS          */
/* LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A  */
/* RESULT OF USING, MODIFYING OR DISTRIBUTING THE SOFTWARE OR ITS           */
/* DERIVATIVES. IN NO EVENT WILL SYKES OR ITS LICENSORS BE LIABLE FOR ANY   */
/* LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,          */
/* CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND        */
/* REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR      */
/* INABILITY TO USE SOFTWARE, EVEN IF SYKES HAS BEEN ADVISED OF THE         */
/* POSSIBILITY OF SUCH DAMAGES.                                             */
/*==========================================================================*/

#include "okLiteTMQuery.h"


/*--------------------------------------------------------------------------*/

okLiteTMQuery::okLiteTMQuery ()
{
	m_nThreshold = 75;
	m_bConcordance = false;
}


/*--------------------------------------------------------------------------*/

okLiteTMQuery::~okLiteTMQuery ()
{
	Close();
};


/*--------------------------------------------------------------------------*/

void okLiteTMQuery::Close ()
{
	m_Tm.Close();
};


/*--------------------------------------------------------------------------*/

long okLiteTMQuery::Query
(
	const char*          p_pszQuery
)
{
	// Clean the results of the previous query
	if ( m_lstResults.size() ) m_lstResults.clear();

	m_Tm.Query(p_pszQuery, m_nThreshold, m_bConcordance, &m_lstResults);
	m_IterRes = m_lstResults.begin();

	// Sort the results
	m_lstResults.sort();

	// Return the sorted results
	return(m_lstResults.size());
};


/*--------------------------------------------------------------------------*/

int okLiteTMQuery::GetFirstMatch
(
	okLiteTMRecord&      p_rData,
	long*                p_plPos
)
{
	// Sorted in ascending order: so we start with last
	if ( !m_lstResults.size() ) return(0);

	m_IterRes = m_lstResults.end(); m_IterRes--;
	if ( p_plPos != NULL ) *p_plPos = m_IterRes->m_lPos;

	if ( !m_Tm.Get(p_rData, m_IterRes->m_lPos) ) return(0);
	return((int)(m_IterRes->m_fScore*100));
}


/*--------------------------------------------------------------------------*/

int okLiteTMQuery::GetNextMatch
(
	okLiteTMRecord&      p_rData,
	long*                p_plPos
)
{
	if ( m_IterRes == m_lstResults.begin() ) return(0);

	m_IterRes--;
	if ( p_plPos != NULL ) *p_plPos = m_IterRes->m_lPos;
	if ( !m_Tm.Get(p_rData, m_IterRes->m_lPos) ) return(0);

	return((int)(m_IterRes->m_fScore*100));
}


/*--------------------------------------------------------------------------*/

long okLiteTMQuery::GetExactMatchCount ()
{
	long                 lCount = 0;
	okLiteTMResultIter   Iter;


	if ( !m_lstResults.size() ) return(lCount);

	Iter = m_lstResults.end(); Iter--;
	do
	{
		if ( Iter->m_fScore == 1.0 ) lCount++;
		else return(lCount); // No more exact match (they are sorted)
		Iter--;
	}
	while ( Iter != m_lstResults.begin() );

	return(lCount);
}

