A small tweak to binary search will force it to find the first of several
matches. So you want something like:
int iItem = -1;
int iItemStart = 0;
int iItemStop = numRecords;
while (iItemStop > iItemStart)
{
Int16 sgn;
int iItemMid = (iItemStart + iItemStop) / 2;
sgn = StrNCaselessCompare(recordString, searchString,
StrLen(searchString));
if (sgn < 0)
iItemStart = iItemMid + 1;
else
{
if (sgn == 0)
iItem = iItemMid; // Remember this match but keep looking
up to find first
iItemStop = iItemMid;
}
}
if (iItem >= 0)
// scroll to iItem
Dave Parker
TapPoint www.tappoint.com
"Dan" <[EMAIL PROTECTED]> wrote in message news:99028@;palm-dev-forum...
>
> Hi!
>
> I have developing an application (off & on) for a while that searched
> small PDBs. The key fields of the DB are text entries and the DBs are
> alphabetically sorted. My goal is to replicate the functionality that
> we all know and love.... namely : as the user enters successive
> characters of the target string,the "list from which to select" displays
> the available choices from the DB. (So the user enters a "b" and the
> list scrolls to "Banana" (as this is the FIRST element in the list that
> begins with a "B"). Then the user enters an "e" to make the target
> string "be" and the list scrolls to "beet" etc. etc.)
>
> All this worked great when I was using a linear "brute force" search,
> but then my DB got BIG! So I switched to a binary search... which
> works great... but (as you may have surmised) a binary search (in
> this particular example doesn't *quite* work since it may put you
> way PAST the intended target! For example, when the user enters a
> "b" the binary search way bounce around and end up matching the "b"
> with "bird" instead of the FIRST entry ("Banana") (I am only
> searching for a match on the first character since that's all I have).
>
> I have a sickening feeling that I am just being an idiot....
> Please prove me right! ;-)
>
> Thanks!
>
> Dan
>
>
>
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/support/forums/