diff --git a/xwords4/common/dictnry.c b/xwords4/common/dictnry.c index be7d54747..9fc1580fd 100644 --- a/xwords4/common/dictnry.c +++ b/xwords4/common/dictnry.c @@ -863,20 +863,19 @@ dict_countWords( const DictionaryCtxt* dict ) static DictIndex placeWordClose( const DictionaryCtxt* dict, DictIndex position, - XP_U16 depth, const DictIndex* indices, const Tile* prefixes, - XP_U16 count, EdgeArray* result ) + XP_U16 depth, IndexData* data, EdgeArray* result ) { XP_S16 low = 0; - XP_S16 high = count - 1; + XP_S16 high = data->count - 1; XP_S16 index = -1; for ( ; ; ) { if ( low > high ) { break; } index = low + ( (high - low) / 2); - if ( position < indices[index] ) { + if ( position < data->indices[index] ) { high = index - 1; - } else if ( indices[index+1] <= position) { + } else if ( data->indices[index+1] <= position) { low = index + 1; } else { break; @@ -891,21 +890,23 @@ placeWordClose( const DictionaryCtxt* dict, DictIndex position, may be better off starting with the next if it's closer. The last index is a special case since we use lastWord rather than a prefix to init */ - if ( ( index + 1 < count ) - && (indices[index + 1] - position) < (position - indices[index]) ) { + if ( ( index + 1 < data->count ) + && (data->indices[index + 1] - position) + < (position - data->indices[index]) ) { ++index; } - XP_Bool success = findStartsWith( dict, &prefixes[depth*index], depth, result ) + XP_Bool success = findStartsWith( dict, &data->prefixes[depth*index], + depth, result ) && ( ISACCEPTING( dict, result->edges[result->nEdges-1] ) || nextWord( dict, result ) ); XP_ASSERT( success ); - return indices[index]; + return data->indices[index]; } /* placeWordClose */ static void indexOne( const DictionaryCtxt* dict, XP_U16 depth, Tile* tiles, - DictIndex* indices, Tile* prefixes, XP_U16* nextIndex, + IndexData* data, XP_U16* nextIndex, EdgeArray* prevEdges, DictIndex* prevIndex ) { EdgeArray curEdges = { .nEdges = 0 }; @@ -927,9 +928,9 @@ indexOne( const DictionaryCtxt* dict, XP_U16 depth, Tile* tiles, } } } - indices[*nextIndex] = *prevIndex; - if ( NULL != prefixes ) { - XP_MEMCPY( prefixes + (*nextIndex * depth), tiles, depth ); + data->indices[*nextIndex] = *prevIndex; + if ( NULL != data->prefixes ) { + XP_MEMCPY( data->prefixes + (*nextIndex * depth), tiles, depth ); } ++*nextIndex; } @@ -938,26 +939,24 @@ indexOne( const DictionaryCtxt* dict, XP_U16 depth, Tile* tiles, static void doOneDepth( const DictionaryCtxt* dict, const Tile* allTiles, XP_U16 nTiles, Tile* prefix, - XP_U16 curDepth, XP_U16 maxDepth, - DictIndex* indices, Tile* prefixes, XP_U16* nextEntry, - EdgeArray* prevEdges, DictIndex* prevIndex ) + XP_U16 curDepth, XP_U16 maxDepth, IndexData* data, + XP_U16* nextEntry, EdgeArray* prevEdges, DictIndex* prevIndex ) { XP_U16 ii; for ( ii = 0; ii < nTiles; ++ii ) { prefix[curDepth] = allTiles[ii]; if ( curDepth + 1 == maxDepth ) { - indexOne( dict, maxDepth, prefix, indices, prefixes, - nextEntry, prevEdges, prevIndex); + indexOne( dict, maxDepth, prefix, data, nextEntry, prevEdges, + prevIndex ); } else { doOneDepth( dict, allTiles, nTiles, prefix, curDepth+1, maxDepth, - indices, prefixes, nextEntry, prevEdges, prevIndex ); + data, nextEntry, prevEdges, prevIndex ); } } } -XP_U16 -dict_makeIndex( const DictionaryCtxt* dict, XP_U16 depth, - DictIndex* indices, Tile* prefixes, XP_U16 count ) +void +dict_makeIndex( const DictionaryCtxt* dict, XP_U16 depth, IndexData* data ) { XP_ASSERT( depth < MAX_COLS ); XP_U16 ii, needCount, nTiles; @@ -969,7 +968,7 @@ dict_makeIndex( const DictionaryCtxt* dict, XP_U16 depth, for ( ii = 1, needCount = nFaces; ii < depth; ++ii ) { needCount *= nFaces; } - XP_ASSERT( needCount <= count ); + XP_ASSERT( needCount <= data->count ); Tile allTiles[nFaces]; nTiles = 0; @@ -993,10 +992,10 @@ dict_makeIndex( const DictionaryCtxt* dict, XP_U16 depth, indicesToEdges( dict, &firstWord, &prevEdges ); doOneDepth( dict, allTiles, nFaces, prefix, 0, depth, - indices, prefixes, &nextIndex, &prevEdges, &prevIndex ); + data, &nextIndex, &prevEdges, &prevIndex ); } - return nextIndex; + data->count = nextIndex; } static void @@ -1064,8 +1063,7 @@ dict_getPrevWord( const DictionaryCtxt* dict, DictWord* word ) at the closer end. Then move as many steps as necessary to reach it. */ XP_Bool dict_getNthWord( const DictionaryCtxt* dict, DictWord* word, XP_U32 nn, - XP_U16 depth, DictIndex* indices, Tile* prefixes, - XP_U16 count ) + XP_U16 depth, IndexData* data ) { XP_U32 wordCount; XP_Bool validWord = 0 < word->nTiles; @@ -1093,9 +1091,8 @@ dict_getNthWord( const DictionaryCtxt* dict, DictWord* word, XP_U32 nn, if ( !success ) { EdgeArray edges; XP_U32 wordIndex; - if ( !!indices ) { - wordIndex = placeWordClose( dict, nn, depth, indices, - prefixes, count, &edges ); + if ( !!data ) { + wordIndex = placeWordClose( dict, nn, depth, data, &edges ); if ( !validWord ) { initWord( dict, word ); } diff --git a/xwords4/common/dictnry.h b/xwords4/common/dictnry.h index dd8140583..ad4f2f84a 100644 --- a/xwords4/common/dictnry.h +++ b/xwords4/common/dictnry.h @@ -217,16 +217,21 @@ typedef struct _DictWord { XP_U32 indices[MAX_COLS]; } DictWord; +typedef struct _IndexData { + DictIndex* indices; + Tile* prefixes; + XP_U16 count; /* in-out: must indicate others are large enough */ +} IndexData; + XP_U32 dict_countWords( const DictionaryCtxt* dict ); -XP_U16 dict_makeIndex( const DictionaryCtxt* dict, XP_U16 depth, - DictIndex* indices, Tile* prefixes, XP_U16 count ); +void dict_makeIndex( const DictionaryCtxt* dict, XP_U16 depth, + IndexData* data ); XP_Bool dict_firstWord( const DictionaryCtxt* dict, DictWord* word ); XP_Bool dict_lastWord( const DictionaryCtxt* dict, DictWord* word ); XP_Bool dict_getNextWord( const DictionaryCtxt* dict, DictWord* word ); XP_Bool dict_getPrevWord( const DictionaryCtxt* dict, DictWord* word ); XP_Bool dict_getNthWord( const DictionaryCtxt* dict, DictWord* word, XP_U32 nn, - XP_U16 depth, DictIndex* indices, - Tile* prefixes, XP_U16 count ); + XP_U16 depth, IndexData* data ); void dict_wordToString( const DictionaryCtxt* dict, const DictWord* word, XP_UCHAR* buf, XP_U16 buflen ); diff --git a/xwords4/linux/linuxmain.c b/xwords4/linux/linuxmain.c index 93d54aef9..0960d7ce0 100644 --- a/xwords4/linux/linuxmain.c +++ b/xwords4/linux/linuxmain.c @@ -895,8 +895,7 @@ tmp_noop_sigintterm( int XP_UNUSED(sig) ) #ifdef XWFEATURE_WALKDICT static void testGetNthWord( const DictionaryCtxt* dict, char** words, - XP_U16 depth, DictIndex* indices, - Tile* prefixes, XP_U16 nIndices ) + XP_U16 depth, IndexData* data ) { XP_U32 half = dict_getWordCount( dict ) / 2; XP_UCHAR buf[64]; @@ -905,13 +904,13 @@ testGetNthWord( const DictionaryCtxt* dict, char** words, XP_U32 interval = 100; for ( ii = 0, jj = half; ii < half; ii += interval, jj += interval ) { - if ( dict_getNthWord( dict, &word, ii, depth, indices, prefixes, nIndices ) ) { + if ( dict_getNthWord( dict, &word, ii, depth, data ) ) { dict_wordToString( dict, &word, buf, VSIZE(buf) ); XP_ASSERT( 0 == strcmp( buf, words[ii] ) ); } else { XP_ASSERT( 0 ); } - if ( dict_getNthWord( dict, &word, jj, depth, indices, prefixes, nIndices ) ) { + if ( dict_getNthWord( dict, &word, jj, depth, data ) ) { dict_wordToString( dict, &word, buf, VSIZE(buf) ); XP_ASSERT( 0 == strcmp( buf, words[jj] ) ); } else { @@ -979,16 +978,20 @@ walk_dict_test( const LaunchParams* params ) } } XP_ASSERT( count == jj ); - XP_LOGF( "finished comparing runs in both directions\n" ); + XP_LOGF( "finished comparing runs in both directions" ); XP_LOGF( "testing getNth" ); - testGetNthWord( dict, words, 0, NULL, NULL, 0 ); + testGetNthWord( dict, words, 0, NULL ); XP_U16 depth = 2; DictIndex indices[26*26]; /* pow(26,depth) */ Tile prefixes[depth*26*26]; - XP_U16 nIndices = dict_makeIndex( dict, depth, indices, - prefixes, VSIZE(indices) ); + IndexData data = { .indices = indices, + .prefixes = prefixes, + .count = VSIZE(indices) + }; + dict_makeIndex( dict, depth, &data ); + #if 0 for ( ii = 0; ii < nIndices; ++ii ) { if ( !dict_getNthWord( dict, &word, indices[ii] ) ) { @@ -1009,7 +1012,7 @@ walk_dict_test( const LaunchParams* params ) #endif XP_LOGF( "testing getNth WITH INDEXING" ); - testGetNthWord( dict, words, depth, indices, prefixes, nIndices ); + testGetNthWord( dict, words, depth, &data ); XP_LOGF( "done" ); exit( 0 );