At 10:46 AM -0700 5/21/99, Elia J. Freedman wrote:
>I would get back "Apple Pie" with find sort position when I need "Apple".

Yes.  DmFindSortPosition is written with the assumption that you want to
insert a new record, and not locate an old one.

We should probably rename that function for clarity, and add a 'search'
function that returns the leftmost of a set of matching entries.

In the mean time, it's easy to write your own search routine.  Binary
search is just a few lines of code...

                                --Bob

P.S.  Here's the core of binary search -- using this algorithm will save
you a ton of time and effort and debugging, I promise.  You'll have to add
in the data manager get/lock code yourself, but that should be
straightforward.

        lPos = 0;
        rPos = <number of records in DB> - 1;

        // keep going until the pointers cross, that way
        // you get the index of smallest (leftmost) elt >= item
        // NOTE: may return number of records in DB to indicate key is
        //       after the last record in the DB.
        while (lPos <= rPos)
        {
                mPos = (lPos + rPos) / 2;

                dbRec = <get db record at index mPos>
                if (compareFunc(<search key>, dbRec) > 0) // mid value <
search value
                        lPos = mPos + 1;
                else
                        rPos = mPos - 1;
        }

        lPos now holds the index of the leftmost matching record, or the
index of the leftmost record that has a key greater than the search key.


Reply via email to