From 8c1ad5e219719bafa41bf36446f298ac71001b8e Mon Sep 17 00:00:00 2001 From: H.G.Muller Date: Sun, 7 Sep 2014 13:32:49 +0200 Subject: [PATCH] Fix GameListHighlight WB The routine to highlight a game line in the Game List listbox of WinBoard used an extremely inefficient linear search to find the game amongst the selected games. This has now been replaced by bisection, after finding an upper limit to the number of entries in the listbox by doubling a trial size. --- winboard/wgamelist.c | 18 +++++++++++++----- 1 files changed, 13 insertions(+), 5 deletions(-) diff --git a/winboard/wgamelist.c b/winboard/wgamelist.c index 17e78f1..196c837 100644 --- a/winboard/wgamelist.c +++ b/winboard/wgamelist.c @@ -437,14 +437,22 @@ VOID GameListPopDown(void) VOID GameListHighlight(int index) { char buf[MSG_SIZ]; - int i, res = 0; + int i, j, k, n, res = 0; if (gameListDialog == NULL) return; - for(i=0; res != LB_ERR; i++) { + for(i=64; ; i+=i) { res = SendDlgItemMessage( gameListDialog, OPT_GameListText, LB_GETTEXT, i, (LPARAM)buf ); - if(index <= atoi( buf )) break; + if(res == LB_ERR || index < atoi( buf )) break; } - SendDlgItemMessage(gameListDialog, OPT_GameListText, - LB_SETCURSEL, i, 0); + j = i/2; + while(i-j > 1) { + n = (i + j) >> 1; + res = SendDlgItemMessage( gameListDialog, OPT_GameListText, LB_GETTEXT, n, (LPARAM)buf ); + if(res == LB_ERR || index < (k = atoi( buf ))) i = n; else { + j = n; + if(index == k) break; + } + } + SendDlgItemMessage(gameListDialog, OPT_GameListText, LB_SETCURSEL, j, 0); } -- 1.7.0.4