boolean opbestfind (hdlheadrecord hfirst, bigstring bs, hdlheadrecord *hnode) { /* find the best approximation of the string in the siblings of hfirst. */ register hdlheadrecord nomad, nextnomad; bigstring bsnomad; bigstring bsfind; bigstring bsbest; register hdlheadrecord hbest = nil; /*no candidate found*/ *hnode = nil; /*default returned value*/ nomad = op1stsibling (hfirst); copystring (bs, bsfind); /*work on a copy*/ alllower (bsfind); /*unicase*/ while (true) { opgetheadstring (nomad, bsnomad); alllower (bsnomad); switch (comparestrings (bsnomad, bsfind)) { case 0: /*string equal*/ *hnode = nomad; return (true); case -1: /*key less than name*/ break; case +1: /*key greather than name*/ if ((hbest == nil) || (comparestrings (bsnomad, bsbest) == -1)) { copystring (bsnomad, bsbest); hbest = nomad; } } /*switch*/ nextnomad = (**nomad).headlinkdown; if (nextnomad == nomad) break; nomad = nextnomad; } /*while*/ if (hbest == nil) /*didn't find any item that would follow bsname*/ hbest = nomad; /*select last name*/ *hnode = hbest; return (false); } /*opbestfind*/
static short tablecomparenames (hdlhashtable ht, hdlhashnode hnode1, hdlhashnode hnode2) { bigstring bs1, bs2; gethashkey (hnode1, bs1); gethashkey (hnode2, bs2); alllower (bs1); /*comparison is unicase*/ alllower (bs2); return (comparestrings (bs1, bs2)); } /*tablecomparenames*/
int lookup(const struct entry d[], const char search[] , const int entries) { int low = 0; int high = entries - 1; int mid; int result; while (low <= high) { mid = (low + high) / 2; result = comparestrings(d[mid].word , search); if (result == -1) { low = mid + 1; } else if (result == 1) { high = mid - 1; } else { return mid; } } return -1; }
int lookup(const struct entry dictionary[],const char search[],const int entries) { int low=0; int high=entries-1; int mid,result; int comparestrings(const char s1[], const char s2[]); while(low<=high) { mid=(low+high)/2; result=comparestrings(dictionary[mid].word,search); if(result==-1) low=mid+1; else if(result==1) high=mid-1; else return mid; } return -1; }
static boolean opfindhead (hdlheadrecord hfirst, bigstring bs, hdlheadrecord *hnode) { /* search starting with hfirst and step through its siblings looking for a headline that exactly matches bs. */ register hdlheadrecord nomad, nextnomad; bigstring bsnomad; bigstring bsfind; bigstring bsbest; register hdlheadrecord hbest = nil; /*no candidate found*/ *hnode = nil; /*default returned value*/ nomad = op1stsibling (hfirst); copystring (bs, bsfind); /*work on a copy*/ alllower (bsfind); /*unicase*/ while (true) { getheadstring (nomad, bsnomad); alllower (bsnomad); switch (comparestrings (bsnomad, bsfind)) { case 0: /*string equal*/ *hnode = nomad; return (true); case -1: /*key less than name*/ break; case +1: /*key greather than name*/ if ((hbest == nil) || (comparestrings (bsnomad, bsbest) == -1)) { copystring (bsnomad, bsbest); hbest = nomad; } } nextnomad = (**nomad).headlinkdown; if (nextnomad == nomad) break; nomad = nextnomad; } /*while*/ if (hbest == nil) /*didn't find any item that would follow bsname*/ hbest = nomad; /*select last name*/ *hnode = hbest; return (false); } /*opfindhead*/