AI-MegaHAL
view release on metacpan or search on metacpan
libmegahal.c view on Meta::CPAN
middle=(min+max)/2;
compar=wordcmp(word, dictionary->entry[dictionary->index[middle]]);
/*
* If it is equal then we have found the element. Otherwise we
* can halve the search space accordingly.
*/
if(compar==0) {
position=middle;
goto found;
} else if(compar>0) {
if(max==middle) {
position=middle+1;
goto notfound;
}
min=middle+1;
} else {
if(min==middle) {
position=middle;
goto notfound;
}
max=middle-1;
}
}
found:
*find=TRUE;
return(position);
notfound:
*find=FALSE;
return(position);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Find_Word
*
* Purpose: Return the symbol corresponding to the word specified.
* We assume that the word with index zero is equal to a
* NULL word, indicating an error condition.
*/
BYTE2 find_word(DICTIONARY *dictionary, STRING word)
{
int position;
bool found;
position=search_dictionary(dictionary, word, &found);
if(found==TRUE) return(dictionary->index[position]);
else return(0);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Wordcmp
*
* Purpose: Compare two words, and return an integer indicating whether
* the first word is less than, equal to or greater than the
* second word.
*/
int wordcmp(STRING word1, STRING word2)
{
unsigned int i;
unsigned int bound;
bound=MIN(word1.length,word2.length);
for(i=0; i<bound; ++i)
if(toupper(word1.word[i])!=toupper(word2.word[i]))
return((int)(toupper(word1.word[i])-toupper(word2.word[i])));
if(word1.length<word2.length) return(-1);
if(word1.length>word2.length) return(1);
return(0);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Free_Dictionary
*
* Purpose: Release the memory consumed by the dictionary.
*/
void free_dictionary(DICTIONARY *dictionary)
{
if(dictionary==NULL) return;
if(dictionary->entry!=NULL) {
free(dictionary->entry);
dictionary->entry=NULL;
}
if(dictionary->index!=NULL) {
free(dictionary->index);
dictionary->index=NULL;
}
dictionary->size=0;
}
/*---------------------------------------------------------------------------*/
void free_model(MODEL *model)
{
if(model==NULL) return;
if(model->forward!=NULL) {
free_tree(model->forward);
}
if(model->backward!=NULL) {
free_tree(model->backward);
}
if(model->context!=NULL) {
free(model->context);
}
if(model->dictionary!=NULL) {
free_dictionary(model->dictionary);
free(model->dictionary);
}
free(model);
}
( run in 1.676 second using v1.01-cache-2.11-cpan-39bf76dae61 )